You are on page 1of 4

package aes;

import
import
import
import
import
import

java.io.BufferedReader;
java.io.FileNotFoundException;
java.io.FileReader;
java.io.IOException;
java.io.PrintStream;
java.util.ArrayList;

/**
*
* @author Saif Khawaja
*/
//Advance key expansion
public class aes {
//input array

w[0]
w[1]
w[2
w[3]
public String [][]w={{"3A","2B","25","2C"},{"4C","3D","37","3B"},{"55","4B",
"48","1A"},{"73","12","1A","5F"}};
]

//function for hexdecimal XOR


public String xorHex(String a, String b) {
char[] chars = new char[a.length()];
for (int i = 0; i < chars.length; i++) {
chars[i] = toHex(fromHex(a.charAt(i)) ^ fromHex(b.charAt(i)));
}
return new String(chars);
}
//function for converting hexdecimal in to number
private static int fromHex(char c) {
if (c >= '0' && c <= '9') {
return c - '0';
}
if (c >= 'A' && c <= 'F') {
return c - 'A' + 10;
}
if (c >= 'a' && c <= 'f') {
return c - 'a' + 10;
}
throw new IllegalArgumentException();
}
//function for converting number to hexdecimal
private char toHex(int num) {
if (num < 0 || num > 15) {
throw new IllegalArgumentException();
}
return "0123456789ABCDEF".charAt(num);
}
public ArrayList getsBox(String fileName) throws FileNotFoundException, IOExcept
ion
{
BufferedReader br = new BufferedReader(new FileReader(fileName));
ArrayList arrayList=new ArrayList();
try {
StringBuilder sb = new StringBuilder();

String line="";
while ((line = br.readLine()) != null) {
String[] spliteda = line.split(" ");
for(int i=0;i<spliteda.length;i++)
arrayList.add((spliteda[i]));
}
String read = sb.toString();
} finally {
br.close();
}
return arrayList;
}
public String sbox(String w) throws IOException
{
//just change to your directory and place a sbox.txt there
ArrayList arrayList=getsBox("C:\\Users\\Saif Khawaja\\Documents\\NetBeansP
rojects\\aes\\src\\aes\\sbox.txt");
String result="";
for(int i=0;i<w.length();i+=2)
{
int row = Integer.parseInt(w.charAt(i)+"", 16 );
int col = Integer.parseInt(w.charAt(i+1)+"", 16 );
result+=arrayList.get((16*row)+(col));
}
return result;
}

public void aesexpansion() throws IOException


{
//
w[4]
w[5]
w[6]
w[7]
String [][]wnew={{"","","",""},{"","","",""},{"","","",""},{"","","",""}};
//g function
//*perform 1 byte left circular rotation
String temp;
temp=w[3][0];
for(int i=0;i<=2;i++)
{
w[3][i]=w[3][i+1];
}
w[3][3]=temp;
//*perform substitution for each byte
String []wsubstitute={"","","",""};
//*round constant for round 1
String []roundconstant={"01","00","00","00"};
//array for rconstant XOR with w[3]
String []w3rconstant={"","","",""};
//array for w substitute XOR with w3rconstant
String []subconstant={"","","",""};

//g(w[3])
//subconstant XOR with rconstant
String []rsubconstant={"","","",""};
//rconstant XOR with w[3]
String a,b,c,d;

for(int i=0;i<=3;i++)
{
a=w[3][i];
d=sbox(a);
wsubstitute[i]=d;
b=roundconstant[i];
c=xorHex(a,b);
w3rconstant[i]=c;
//System.out.println(c);
}
//w substitute XOR with roundconstant
for(int i=0;i<=3;i++)
{
a=wsubstitute[i];
b=roundconstant[i];
c=xorHex(a,b);
subconstant[i]=c;
//System.out.println(c);
}
//subconstant XOR with rconstant
//*g[w[3]]
for(int i=0;i<=3;i++)
{
a=subconstant[i];
b=w3rconstant[i];
c=xorHex(a,b);
rsubconstant[i]=c;
//System.out.println(c);
}
//w[4]=w[0] XOR with g[w[3]]
for(int i=0;i<=3;i++)
{
a=w[0][i];
b=rsubconstant[i];
c=xorHex(a,b);
wnew[0][i]=c;
}
//w[5]=w[1] XOR with w[4]
for(int i=0;i<=3;i++)
{

a=w[1][i];
b=wnew[0][i];
c=xorHex(a,b);
wnew[1][i]=c;
}
//w[6]=w[2] XOR with w[5]
for(int i=0;i<=3;i++)
{
a=w[2][i];
b=wnew[1][i];
c=xorHex(a,b);
wnew[2][i]=c;
}
//w[7]=w[3] XOR with w[6]
for(int i=0;i<=3;i++)
{
a=w[3][i];
b=wnew[2][i];
c=xorHex(a,b);
wnew[3][i]=c;
}
//for print array
for(int i=0;i<=3;i++)
{
System.out.print("w["+(i+4)+"]=");
for(int j=0;j<=3;j++)
{
System.out.print(wnew[i][j]+",");
}
System.out.println("");
}
}
public static void main(String[] args) throws IOException
{aes s=new aes();
s.aesexpansion();
}
}