Package edu.boisestate.lowry.crypto
Class CTRMode
java.lang.Object
edu.boisestate.lowry.crypto.CTRMode
- All Implemented Interfaces:
SymmetricCipher
An implementation of the Counter (CTR) mode of operation.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final BlockCipherThe underlying block cipher used.protected final intThe underlying block cipher's block size. -
Constructor Summary
ConstructorsConstructorDescriptionCTRMode(BlockCipher cipher) Creates a CTRMode instance with the given block cipher as the underlying cipher. -
Method Summary
Modifier and TypeMethodDescriptionbyte[]decrypt(byte[] ciphertext, byte[] key) Decrypts the provided ciphertext using a symmetric key.byte[]encrypt(byte[] plaintext, byte[] key) Encrypts the provided plaintext using a symmetric key.protected voidincrementBlock(byte[] input, int start, int end) Increments a specific sub-block of the given bytes, treating it as an unsigned integer (big endian).protected voidrunCTR(byte[] text, byte[] key, byte[] iv) Runs the CTR operation on the given text, using a secret key and initial counter.
-
Field Details
-
blockCipher
The underlying block cipher used. -
blockSize
The underlying block cipher's block size.
-
-
Constructor Details
-
CTRMode
Creates a CTRMode instance with the given block cipher as the underlying cipher.- Parameters:
cipher- The underlying block cipher for this instance.
-
-
Method Details
-
encrypt
Description copied from interface:SymmetricCipherEncrypts the provided plaintext using a symmetric key.- Specified by:
encryptin interfaceSymmetricCipher- Parameters:
plaintext- The data to be encrypted.key- The secret key for encryption.- Returns:
- The resulting ciphertext.
-
decrypt
Description copied from interface:SymmetricCipherDecrypts the provided ciphertext using a symmetric key.- Specified by:
decryptin interfaceSymmetricCipher- Parameters:
ciphertext- The encrypted data to be decrypted.key- The secret key for decryption.- Returns:
- The resulting plaintext.
-
runCTR
Runs the CTR operation on the given text, using a secret key and initial counter. The data in text is modified in-place, becoming the encrypted or decrypted text.- Parameters:
text- The plaintext or ciphertext.key- The secret key.iv- The initial counter.
-
incrementBlock
Increments a specific sub-block of the given bytes, treating it as an unsigned integer (big endian). This is essentially a ripple-carry addition of the sub-block and 1.- Parameters:
input- The input block containing the sub-block to increment.start- The index of the most significant byte of the sub-block, inclusive.end- The index of the least significant byte of the sub-block, exclusive.- Throws:
IllegalArgumentException- If the input is null, the indices are out of bounds, or the ending index is before the starting index.IllegalStateException- If the entire block overflows.
-