Class AES

java.lang.Object
  extended by AES
All Implemented Interfaces:
AbstractAES

public class AES
extends java.lang.Object
implements AbstractAES

Java AES implementation which takes a 128-, 192- or 256-bit key and encrypts or decrypts the given text.

Author:
David Young dayoung@csupomona.edu

Field Summary
protected  int current
          The index of the current state in this AES cipher.
protected static int[] invSBox
          The inverse S-Box substitution table.
protected  int[] key
          The cipher key.
protected static int Nb
          The number of 32-bit words comprising the plaintext and columns comprising the state matrix of an AES cipher.
protected  int Nk
          The number of 32-bit words comprising the cipher key in this AES cipher.
protected  int Nr
          The number of rounds in this AES cipher.
protected static int[] rCon
          Rcon
protected  int[][][] s
          The state matrices in this AES cipher.
protected static int[] sBox
          The S-Box substitution table.
protected  int[] w
          The key schedule in this AES cipher.
 
Constructor Summary
protected AES()
          Protected constructor.
  AES(byte[] input)
          Constructs an AES cipher using a specific key.
 
Method Summary
protected  int[][] addRoundKey(int[][] s, int round)
          Adds the key schedule for a round to a state matrix.
protected  int[][] cipher(int[][] in, int[][] out)
          Encrypts the cipher text.
 byte[] decrypt(byte[] y)
          Decrypts a 128-bit (16-byte) ciphertext block using this AES cipher
 byte[] encrypt(byte[] x)
          Encrypts a 128-bit (16-byte) plaintext block using this AES cipher
protected  int[][] invCipher(int[][] in, int[][] out)
          Decrypts the cipher text.
protected  int[][] invMixColumns(int[][] s)
          Unmixes each column of a state matrix.
protected  int[][] invShiftRows(int[][] s)
          Applies an inverse cyclic shift to the last 3 rows of a state matrix.
protected  int[][] invSubBytes(int[][] s)
          Applies inverse S-Box substitution to each byte of a state matrix.
protected static int invSubWord(int w)
          Applies inverse S-box substitution to each byte of a 4-byte word.
protected  int[] keyExpand()
           
static void main(java.lang.String[] argv)
          Test Driver.
protected  int[][] mixColumns(int[][] s)
          Mixes each column of a state matrix.
protected static int mult(int a, int b)
          Multiplies two polynomials a(x), b(x) in GF(2^8) modulo the irreducible polynomial m(x) = x^8+x^4+x^3+x+1.
protected static int rotWord(int w)
          Applies a cyclic permutation to a 4-byte word.
protected  int[][] shiftRows(int[][] s)
          Applies a cyclic shift to the last 3 rows of a state matrix.
protected  int[][] subBytes(int[][] s)
          Applies S-Box substitution to each byte of a state matrix.
protected static int subWord(int w)
          Applies S-box substitution to each byte of a 4-byte word.
protected static int xtime(int b)
          Multiplies x times a polynomial b(x) in GF(2^8) modulo the irreducible polynomial m(x) = x^8+x^4+x^3+x+1.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

current

protected int current
The index of the current state in this AES cipher.


invSBox

protected static final int[] invSBox
The inverse S-Box substitution table.


rCon

protected static final int[] rCon
Rcon


Nb

protected static int Nb
The number of 32-bit words comprising the plaintext and columns comprising the state matrix of an AES cipher.


Nk

protected int Nk
The number of 32-bit words comprising the cipher key in this AES cipher.


Nr

protected int Nr
The number of rounds in this AES cipher.


s

protected int[][][] s
The state matrices in this AES cipher.


sBox

protected static final int[] sBox
The S-Box substitution table.


w

protected int[] w
The key schedule in this AES cipher.


key

protected int[] key
The cipher key.

Constructor Detail

AES

public AES(byte[] input)
Constructs an AES cipher using a specific key.

Parameters:
input - A 128-, 192- or 256-bit (i.e. 16-, 24- or 32-byte) secret key.
Throws:
java.lang.IllegalArgumentException - if unknown key size is passed.

AES

protected AES()
Protected constructor. Needed for unit tests.

Method Detail

addRoundKey

protected int[][] addRoundKey(int[][] s,
                              int round)
Adds the key schedule for a round to a state matrix.

Parameters:
s - A state matrix having Nb columns and 4 rows.
round - A round of the key schedule w to be added.
Returns:
s, after adding the key schedule for round.

cipher

protected int[][] cipher(int[][] in,
                         int[][] out)
Encrypts the cipher text.

Parameters:
in - array of text to encrypt
out - array to store the encrypted text
Returns:
out

invCipher

protected int[][] invCipher(int[][] in,
                            int[][] out)
Decrypts the cipher text.

Parameters:
in - array of encrypted text to decrypt
out - array to store the decrypted text
Returns:
out

decrypt

public byte[] decrypt(byte[] y)
Description copied from interface: AbstractAES
Decrypts a 128-bit (16-byte) ciphertext block using this AES cipher

Specified by:
decrypt in interface AbstractAES
Parameters:
y - The 128-bit (16-byte) ciphertext block to be decrypted.
Returns:
The 128-bit (16-byte) plaintext block produced by decryption.

encrypt

public byte[] encrypt(byte[] x)
Description copied from interface: AbstractAES
Encrypts a 128-bit (16-byte) plaintext block using this AES cipher

Specified by:
encrypt in interface AbstractAES
Parameters:
x - The 128-bit (16-byte) plaintext block to be encrypted.
Returns:
The 128-bit (16-byte) ciphertext block produced by encryption.

invMixColumns

protected int[][] invMixColumns(int[][] s)
Unmixes each column of a state matrix. Multiplies each column--a polynomial in GF(GF(2^8)^4)--times {0b}x^3+{0d}^2+{09}x+{0e} modulo x^4+1.

Parameters:
s - A state matrix having Nb columns and 4 rows.
Returns:
s, after unmixing each column.

invShiftRows

protected int[][] invShiftRows(int[][] s)
Applies an inverse cyclic shift to the last 3 rows of a state matrix.

Parameters:
s - A state matrix having Nb columns and 4 rows.
Returns:
s, after an inverse cyclic shift is applied to each row.

invSubBytes

protected int[][] invSubBytes(int[][] s)
Applies inverse S-Box substitution to each byte of a state matrix.

Parameters:
s - A state matrix having Nb columns and 4 rows.
Returns:
s, after inverse S-box substitution is applied to each byte.

invSubWord

protected static int invSubWord(int w)
Applies inverse S-box substitution to each byte of a 4-byte word.

Parameters:
w - A 4-byte word.
Returns:
w, after inverse S-box substitution is applied to each byte.

keyExpand

protected int[] keyExpand()

mixColumns

protected int[][] mixColumns(int[][] s)
Mixes each column of a state matrix. Multiplies each column--a polynomial in GF(GF(2^8)^4)--times {03}x^3+{01}x^2+{01}x+{02} modulo x^4+1.

Parameters:
s - A state matrix having Nb columns and 4 rows.
Returns:
s, after mixing each column.

mult

protected static int mult(int a,
                          int b)
Multiplies two polynomials a(x), b(x) in GF(2^8) modulo the irreducible polynomial m(x) = x^8+x^4+x^3+x+1. (i.e. m(x) = 0x11b).

Parameters:
a - A polynomial a(x) = a7x^7+a6x^6+a5x^5+a4x^4+a3x^3+a2x^2+a1x+a0 in GF(2^8).
b - A polynomial b(x) = b7x^7+b6x^6+b5x^5+b4x^4+b3x^3+b2x^2+b1x+b0 in GF(2^8).
Returns:
a(x)b(x) modulo x^8+x^4+x^3+x+1.

rotWord

protected static int rotWord(int w)
Applies a cyclic permutation to a 4-byte word.

Parameters:
w - A 4-byte word.
Returns:
w, after cyclic permutation is applied.

shiftRows

protected int[][] shiftRows(int[][] s)
Applies a cyclic shift to the last 3 rows of a state matrix.

Parameters:
s - A state matrix having Nb columns and 4 rows.
Returns:
s, after a cyclic shift is applied to each row.

subBytes

protected int[][] subBytes(int[][] s)
Applies S-Box substitution to each byte of a state matrix.

Parameters:
s - A state matrix having Nb columns and 4 rows.
Returns:
s, after S-box substitution is applied to each byte.

subWord

protected static int subWord(int w)
Applies S-box substitution to each byte of a 4-byte word.

Parameters:
w - A 4-byte word.
Returns:
w, after S-box substitution is applied to each byte.

xtime

protected static int xtime(int b)
Multiplies x times a polynomial b(x) in GF(2^8) modulo the irreducible polynomial m(x) = x^8+x^4+x^3+x+1. (i.e. m(x) = 0x11b).

Parameters:
b - A polynomial b(x) = b7x^7+b6x^6+b5x^5+b4x^4+b3x^3+b2x^2+b1x+b0 in GF(2^8).
Returns:
xb(x) mod x8+x4+x3+x+1.

main

public static void main(java.lang.String[] argv)
                 throws java.io.IOException
Test Driver.

Parameters:
argv -
Throws:
java.io.IOException