Package edu.boisestate.lowry.crypto
Class RC6Cipher
java.lang.Object
edu.boisestate.lowry.crypto.RC6Cipher
- All Implemented Interfaces:
BlockCipher
An implementation of the RC6-32/20/b block cipher. This implementation
is configured for word size w = 32 and r = 20 rounds. It also supports
variable secret key lengths 'b' (16, 24, or 32 bytes), corresponding to
requirements for the AES-128, AES-192, and AES-256.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final intThe block size in bytes.private final KeySizeThe key size, corresponding to the RC6 'b' parameter.private static final intThe number of registers.private static final intThe number of rounds for encryption and decryption.private int[]The round keys S[0, ..., 2r + 3]. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate int[]bytesToRegisters(byte[] bytes) Converts an array of bytes to an array of 32-bit registers (words).byte[]decipher(byte[] ciphertext, byte[] key) Deciphers a fixed-length block of ciphertext.byte[]encipher(byte[] plaintext, byte[] key) Enciphers a fixed-length block of plaintext.intReturns the fixed block size this cipher operates on.voidinitKey(byte[] key) Preemptively calls the key schedule with the given key for the purpose of performing bulk encryption.private int[]keySchedule(byte[] key) Derives 2r + 4 round keys from the given key, loaded into an array of 32-bit registers.private byte[]registersToBytes(int[] registers) Converts an array of registers to an array of bytes in a little endian fashion.voidresetKey()Resets the stored key to nothing.private voidrotateRegistersLeft(int[] registers) Rotate/permute the registers to the left.private voidrotateRegistersRight(int[] registers) Rotate/permute the registers to the left.
-
Field Details
-
BLOCK_SIZE_BYTES
The block size in bytes. In RC6, the block size is defined as four words (4w). For w = 32 bits (4 bytes), the block size is 16 bytes (128 bits).- See Also:
-
NUM_REGISTERS
The number of registers.- See Also:
-
NUM_ROUNDS
The number of rounds for encryption and decryption.- See Also:
-
keySize
The key size, corresponding to the RC6 'b' parameter. -
roundKeys
The round keys S[0, ..., 2r + 3].
-
-
Constructor Details
-
RC6Cipher
Creates an instance of this cipher, configured for the given key size.- Parameters:
keySize- The key size for this cipher.
-
-
Method Details
-
encipher
Description copied from interface:BlockCipherEnciphers a fixed-length block of plaintext.- Specified by:
encipherin interfaceBlockCipher- Parameters:
plaintext- A block of data with length equivalent to getBlockSize().key- The secret key for enciphering.- Returns:
- The resulting enciphered block.
-
decipher
Description copied from interface:BlockCipherDeciphers a fixed-length block of ciphertext.- Specified by:
decipherin interfaceBlockCipher- Parameters:
ciphertext- A block of encrypted data with length equivalent to getBlockSize().key- The secret key for deciphering.- Returns:
- The resulting deciphered block.
-
getBlockSize
Description copied from interface:BlockCipherReturns the fixed block size this cipher operates on.- Specified by:
getBlockSizein interfaceBlockCipher- Returns:
- The block size in bytes.
-
initKey
Preemptively calls the key schedule with the given key for the purpose of performing bulk encryption. A call to this will cause they key parameter for encipher() and decipher() to be ignored.- Parameters:
key- The key to initialize.
-
resetKey
Resets the stored key to nothing. A call to this will cause the key parameter for encipher() and decipher() to be usable following a call to initKey(). -
bytesToRegisters
Converts an array of bytes to an array of 32-bit registers (words). Bytes are placed in a little-endian fashion. The length of bytes is assumed to be a multiple of four.- Parameters:
bytes- The array of bytes.- Returns:
- an array of 32-bit registers.
- Implementation Note:
- We use an int array because ints are always 32 bits in Java, exactly what is needed for the w = 32 parameter. Loading the registers little endian corresponds to, for example, how the placement of bytes into A, B, C, D is described, and the same for the array L in the key schedule.
-
registersToBytes
Converts an array of registers to an array of bytes in a little endian fashion.- Parameters:
registers- The array of registers.- Returns:
- an array of bytes.
-
rotateRegistersLeft
Rotate/permute the registers to the left. This is the same operation as the parallel assignment step (A, B, C, D) = (B, C, D, A) for enciphering.- Parameters:
registers- The registers.
-
rotateRegistersRight
Rotate/permute the registers to the left. This is the same operation as the parallel assignment step (A, B, C, D) = (D, A, B, C) for deciphering.- Parameters:
registers- The registers.
-
keySchedule
Derives 2r + 4 round keys from the given key, loaded into an array of 32-bit registers.- Parameters:
key- The private key.- Returns:
- An array of the round keys as registers.
-