Class AESFast
- java.lang.Object
-
- it.ipzs.cieid.AESFast
-
public class AESFast extends java.lang.Objectan implementation of the AES (Rijndael), from FIPS-197.For further details see: http://csrc.nist.gov/encryption/aes/. This implementation is based on optimizations from Dr. Brian Gladman's paper and C code at http://fp.gladman.plus.com/cryptography_technology/rijndael/ There are three levels of tradeoff of speed vs memory Because java has no preprocessor, they are written as three separate classes from which to choose The fastest uses 8Kbytes of static tables to precompute round calculations, 4 256 word tables for encryption and 4 for decryption. The middle performance version uses only one 256 word table for each, for a total of 2Kbytes, adding 12 rotate operations per round to compute the values contained in the other tables from the contents of the first The slowest version uses no static tables at all and computes the values in each round
This file contains the fast version with 8Kbytes of static tables for round precomputation
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description classAESFast.AESException
-
Field Summary
Fields Modifier and Type Field Description private static intBLOCK_SIZEstatic intBLOCK_SIZE_128private intC0private intC1private intC2private intC3private booleanforEncryptionstatic intKEY_SIZE_128static intKEY_SIZE_160static intKEY_SIZE_192static intKEY_SIZE_224static intKEY_SIZE_256private static intm1private static intm2private static intm3private static int[]rconprivate intROUNDSprivate static byte[]Sprivate static byte[]Siprivate static int[]T0private static int[]T1private static int[]T2private static int[]T3private static int[]Tinv0private static int[]Tinv1private static int[]Tinv2private static int[]Tinv3private int[][]WorkingKey
-
Constructor Summary
Constructors Constructor Description AESFast()default constructor - 128 bit block size.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private voiddecryptBlock(int[][] KW)byte[]decryptCBC(byte[] ciphertext, int nOffset, byte[] iv)static byte[]decryptCBC(byte[] ciphertext, int nOffset, byte[] key, int keylen, byte[] iv, int nBlockLen)Encrypt a palintext.byte[]decryptECB(byte[] ciphertext, int nOffset)Encrypt a palintext.static byte[]decryptECB(byte[] ciphertext, int nOffset, byte[] key, int keylen, int nBlockLen)Decrypt a ciphertext.private voidencryptBlock(int[][] KW)private intFFmulX(int x)private int[][]generateWorkingKey(byte[] key, boolean forEncryption)Calculate the necessary round keys The number of calculations depends on key size and block size AES specified a fixed block size of 128 bits and key sizes 128/192/256 bits This code is written assuming those are the only possible valuesjava.lang.StringgetAlgorithmName()intgetBlockSize()voidinit(boolean forEncryption, byte[] key)initialise an AES cipher.private intinv_mcol(int x)static byte[]makeSafeKey(byte[] k, int keysize)private voidpackBlock(byte[] bytes, int off)intprocessBlock(byte[] in, int inOff, byte[] out, int outOff)voidreset()private intshift(int r, int shift)private intsubWord(int x)private voidunpackBlock(byte[] bytes, int off)private byte[]unpad(byte[] plaintext)private voidxor(byte[] block, int offsetblok, byte[] iv, int offsetiv, byte[] xoredblock, int offserxored, int blocklen)
-
-
-
Field Detail
-
KEY_SIZE_256
public static final int KEY_SIZE_256
- See Also:
- Constant Field Values
-
KEY_SIZE_224
public static final int KEY_SIZE_224
- See Also:
- Constant Field Values
-
KEY_SIZE_160
public static final int KEY_SIZE_160
- See Also:
- Constant Field Values
-
KEY_SIZE_192
public static final int KEY_SIZE_192
- See Also:
- Constant Field Values
-
KEY_SIZE_128
public static final int KEY_SIZE_128
- See Also:
- Constant Field Values
-
BLOCK_SIZE_128
public static final int BLOCK_SIZE_128
- See Also:
- Constant Field Values
-
S
private static final byte[] S
-
Si
private static final byte[] Si
-
rcon
private static final int[] rcon
-
T0
private static final int[] T0
-
T1
private static final int[] T1
-
T2
private static final int[] T2
-
T3
private static final int[] T3
-
Tinv0
private static final int[] Tinv0
-
Tinv1
private static final int[] Tinv1
-
Tinv2
private static final int[] Tinv2
-
Tinv3
private static final int[] Tinv3
-
m1
private static final int m1
- See Also:
- Constant Field Values
-
m2
private static final int m2
- See Also:
- Constant Field Values
-
m3
private static final int m3
- See Also:
- Constant Field Values
-
ROUNDS
private int ROUNDS
-
WorkingKey
private int[][] WorkingKey
-
C0
private int C0
-
C1
private int C1
-
C2
private int C2
-
C3
private int C3
-
forEncryption
private boolean forEncryption
-
BLOCK_SIZE
private static final int BLOCK_SIZE
- See Also:
- Constant Field Values
-
-
Method Detail
-
shift
private int shift(int r, int shift)
-
FFmulX
private int FFmulX(int x)
-
inv_mcol
private int inv_mcol(int x)
-
subWord
private int subWord(int x)
-
generateWorkingKey
private int[][] generateWorkingKey(byte[] key, boolean forEncryption)Calculate the necessary round keys The number of calculations depends on key size and block size AES specified a fixed block size of 128 bits and key sizes 128/192/256 bits This code is written assuming those are the only possible values
-
init
public void init(boolean forEncryption, byte[] key)initialise an AES cipher.- Parameters:
forEncryption- whether or not we are for encryption.params- the parameters required to set up the cipher.- Throws:
java.lang.IllegalArgumentException- if the params argument is inappropriate.
-
getAlgorithmName
public java.lang.String getAlgorithmName()
-
getBlockSize
public int getBlockSize()
-
processBlock
public int processBlock(byte[] in, int inOff, byte[] out, int outOff)
-
reset
public void reset()
-
unpackBlock
private final void unpackBlock(byte[] bytes, int off)
-
packBlock
private final void packBlock(byte[] bytes, int off)
-
encryptBlock
private final void encryptBlock(int[][] KW)
-
decryptBlock
private final void decryptBlock(int[][] KW)
-
decryptECB
public byte[] decryptECB(byte[] ciphertext, int nOffset)Encrypt a palintext.- Parameters:
ciphertext- The ciphertext.nOffset- Index of in from which to start considering data.sessionKey- The session key to use for encryption.blockSize- The block size in bytes of this Rijndael.- Returns:
- The plaintext generated from ciphertext using the session key.
-
decryptCBC
public byte[] decryptCBC(byte[] ciphertext, int nOffset, byte[] iv)
-
unpad
private byte[] unpad(byte[] plaintext)
-
xor
private void xor(byte[] block, int offsetblok, byte[] iv, int offsetiv, byte[] xoredblock, int offserxored, int blocklen)
-
decryptECB
public static byte[] decryptECB(byte[] ciphertext, int nOffset, byte[] key, int keylen, int nBlockLen)Decrypt a ciphertext.- Parameters:
ciphertext- The ciphertext.nOffset- Index of in from which to start considering data.key- The session key to use for encryption.blockSize- The block size in bytes of this Rijndael.- Returns:
- The plaintext generated from ciphertext using the session key.
-
decryptCBC
public static byte[] decryptCBC(byte[] ciphertext, int nOffset, byte[] key, int keylen, byte[] iv, int nBlockLen)Encrypt a palintext.- Parameters:
ciphertext- The ciphertext.nOffset- Index of in from which to start considering data.key- The session key to use for encryption.blockSize- The block size in bytes of this Rijndael.- Returns:
- The plaintext generated from ciphertext using the session key.
-
makeSafeKey
public static byte[] makeSafeKey(byte[] k, int keysize)
-
-