package codeAes;
import javax.swing.*;
public class AES
{
private static char[] alphabet={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
public static void main(String[] args)
{
String[] possibleValues={"加密","解密","退出"};
String str=(String)JOptionPane.showInputDialog(null,
"请选择您要进行的操作","AES",JOptionPane.INFORMATION_MESSAGE,
null,possibleValues,possibleValues[0]);
if(str.equals("退出"))
{
return;
}
if(str.equals("加密"))
{
int[][] cipherKey=KeyExpansion.getKeyExpansion();
str=JOptionPane.showInputDialog("请输入明文(32的倍数个16进制字符(0~9、a~f)):\n");
System.out.println("您输入的明文为:");
printStr(str);
System.out.println();
if(str.length()%32!=0)
{
int len=str.length();
for(int i=0;i<32-len%32;i++)
{
str+=" ";
}
}
str=str.toLowerCase();
char[] ch=str.toCharArray();
int[] plain=new int[str.length()/2];
String s=new String("");
for(int i=0;i<str.length();i++)
{
for(int j=3;j>=0;j--)
{
s+=KeyExpansion.getNum(ch[i])>>j&1;
}
}
for(int i=0;i<plain.length;i++)
{
plain[i]=Integer.valueOf(s.substring(i*8,i*8+8),2);
}
for(int i=0;i<plain.length/16;i++)
{
printArray("明文初始状态:",plain);
printArray("密钥:",cipherKey[0]);
plain=AddRoundKey.addRoundKey(plain,cipherKey[0]);
printArray("AddRoundKey:",plain);
System.out.println();
for(int j=0;j<cipherKey.length-2;j++)
{
System.out.println("N="+(j+1));
for(int h=0;h<plain.length;h++)
{
plain[h]=SubByte.getSubByte(plain[h]);
}
printArray("SubBytes:",plain);
plain=ShiftRows.shiftRows(plain);
printArray("ShiftRows:",plain);
plain=MixColumn.mixColumn(plain);
printArray("MixColumn:",plain);
printArray("轮密钥:",cipherKey[j+1]);
plain=AddRoundKey.addRoundKey(plain,cipherKey[j+1]);
printArray("AddRoundKey:",plain);
System.out.println();
}
System.out.println("N="+(cipherKey.length-1));
for(int h=0;h<plain.length;h++)
{
plain[h]=SubByte.getSubByte(plain[h]);
}
printArray("SubBytes:",plain);
plain=ShiftRows.shiftRows(plain);
printArray("ShiftRows:",plain);
printArray("轮密钥:",cipherKey[cipherKey.length-1]);
plain=AddRoundKey.addRoundKey(plain,cipherKey[cipherKey.length-1]);
printArray("AddRoundKey:",plain);
printArray("密文为:",plain);
System.out.println();
}
}
if(str.equals("解密"))
{
int[][] key=KeyExpansion.getKeyExpansion();
int[][] cipherKey=new int[key.length][16];
String str1=new String("");
byte[] buf=new byte[2];
cipherKey[0]=key[0];
for(int i=1;i<key.length-1;i++)
{
cipherKey[i]=MixColumn.invMixColumn(key[i]);
}
cipherKey[key.length-1]=key[key.length-1];
printArray("N=1:",cipherKey[key.length-1]);
System.out.println();
str=JOptionPane.showInputDialog("请输入密文(32的倍数个16进制字符(0~9、a~f)):\n");
if(str.length()%32!=0)
{
System.out.println("错误!密文长度必须为32的倍数");
return;
}
System.out.println("您输入的密文为:");
printStr(str);
System.out.println();
str=str.toLowerCase();
char[] ch=str.toCharArray();
int[] cipher=new int[str.length()/2];
String s=new String("");
for(int i=0;i<str.length();i++)
{
for(int j=3;j>=0;j--)
{
s+=KeyExpansion.getNum(ch[i])>>j&1;
}
}
for(int i=0;i<cipher.length;i++)
{
cipher[i]=Integer.valueOf(s.substring(i*8,i*8+8),2);
}
for(int i=0;i<cipher.length/16;i++)
{
printArray("密文初始状态:",cipher);
printArray("密钥:",cipherKey[cipherKey.length-1]);
cipher=AddRoundKey.addRoundKey(cipher,cipherKey[cipherKey.length-1]);
printArray("AddRoundKey:",cipher);
System.out.println();
for(int j=cipherKey.length-2;j>0;j--)
{
System.out.println("N="+(j+1));
for(int h=0;h<cipher.length;h++)
{
cipher[h]=SubByte.getByteSub(cipher[h]);
}
printArray("InvSubBytes:",cipher);
cipher=ShiftRows.invShiftRows(cipher);
printArray("InvShiftRows:",cipher);
cipher=MixColumn.invMixColumn(cipher);
printArray("InvMixColumn:",cipher);
printArray("轮密钥:",cipherKey[j]);
cipher=AddRoundKey.addRoundKey(cipher,cipherKey[j]);
printArray("AddRoundKey:",cipher);
System.out.println();
}
System.out.println("N=1");
for(int h=0;h<cipher.length;h++)
{
cipher[h]=SubByte.getByteSub(cipher[h]);
}
printArray("InvSubBytes:",cipher);
cipher=ShiftRows.invShiftRows(cipher);
printArray("InvShiftRows:",cipher);
printArray("轮密钥:",cipherKey[0]);
cipher=AddRoundKey.addRoundKey(cipher,cipherKey[0]);
printArray("AddRoundKey:",cipher);
printArray("明文为: ",cipher);
System.out.println();
}
}
}
public static void printStr(String str)
{
System.out.println(str);
}
public static void printArray(String s,int[] array)
{
System.out.print(s);
for(int i=0;i<array.length;i++)
{
String string=new String("");
byte[] buf=new byte[2];
string=SubByte.getStr(array[i]);
buf[0]=Byte.valueOf(string.substring(0,4),2);
buf[1]=Byte.valueOf(string.substring(4,8),2);
System.out.print(alphabet[buf[0]]+""+alphabet[buf[1]]);
}
System.out.println();
}
}