📄 aes.java
字号:
for(int i=0;i<State.length;i++)
{
State[i] = subword(State[i],nis);
}
}
}
static void linechange()
{
char a,b,c,d;
if(jiemi==0)
{
for(int i=0;i<State.length;i++)
{
a = State[i][2];
b = State[i][3];
State[i][2] = State[i][10];
State[i][3] = State[i][11];
State[i][10] = State[i][18];
State[i][11] = State[i][19];
State[i][18] = State[i][26];
State[i][19] = State[i][27];
State[i][26] = a;
State[i][27] = b;
a = State[i][4];
b = State[i][5];
c = State[i][12];
d = State[i][13];
State[i][4] = State[i][20];
State[i][5] = State[i][21];
State[i][12] = State[i][28];
State[i][13] = State[i][29];
State[i][20] = a;
State[i][21] = b;
State[i][28] = c;
State[i][29] = d;
a = State[i][30];
b = State[i][31];
State[i][30] = State[i][22];
State[i][31] = State[i][23];
State[i][22] = State[i][14];
State[i][23] = State[i][15];
State[i][14] = State[i][6];
State[i][15] = State[i][7];
State[i][6] = a;
State[i][7] = b;
}
}
else
{
for(int i=0;i<State.length;i++)
{
a = State[i][26];
b = State[i][27];
State[i][26] = State[i][18];
State[i][27] = State[i][19];
State[i][18] = State[i][10];
State[i][19] = State[i][11];
State[i][10] = State[i][2];
State[i][11] = State[i][3];
State[i][2] = a;
State[i][3] = b;
a = State[i][20];
b = State[i][21];
c = State[i][28];
d = State[i][29];
State[i][20] = State[i][4];
State[i][21] = State[i][5];
State[i][28] = State[i][12];
State[i][29] = State[i][13];
State[i][4] = a;
State[i][5] = b;
State[i][12] = c;
State[i][13] = d;
a = State[i][6];
b = State[i][7];
State[i][6] = State[i][14];
State[i][7] = State[i][15];
State[i][14] = State[i][22];
State[i][15] = State[i][23];
State[i][22] = State[i][30];
State[i][23] = State[i][31];
State[i][30] = a;
State[i][31] = b;
}
}
}
static char[] xor4(char[]ah,char[]bh,char[]ch,char[]dh) //right
{
char[] aah;
aah = xor(xor(xor(ah,bh),ch),dh);
return aah;
}
static void rowchange() //列混淆(right)
{
char[][] t = new char[16][2];
char[][] s;
int i;
for( i = 0;i<State.length;i++)
{
for(int j=0;j<State[i].length;j+=2)
{
t[j/2][0] = State[i][j];
t[j/2][1] = State[i][j+1];
}
s = t.clone();
if(jiemi==0)
{
t[0] = xor4(fun2(s[0].clone()),fun3(s[1].clone()),s[2].clone(),s[3].clone());
t[1] = xor4(s[0].clone(),fun2(s[1].clone()),fun3(s[2].clone()),s[3].clone());
t[2] = xor4(s[0].clone(),s[1].clone(),fun2(s[2].clone()),fun3(s[3].clone()));
t[3] = xor4(fun3(s[0]),s[1],s[2],fun2(s[3]));
t[4] = xor4(fun2(s[4].clone()),fun3(s[5].clone()),s[6].clone(),s[7].clone());
t[5] = xor4(s[4].clone(),fun2(s[5].clone()),fun3(s[6].clone()),s[7].clone());
t[6] = xor4(s[4].clone(),s[5].clone(),fun2(s[6].clone()),fun3(s[7].clone()));
t[7] = xor4(fun3(s[4]),s[5],s[6],fun2(s[7]));
t[8] = xor4(fun2(s[8].clone()),fun3(s[9].clone()),s[10].clone(),s[11].clone());
t[9] = xor4(s[8].clone(),fun2(s[9].clone()),fun3(s[10].clone()),s[11].clone());
t[10] = xor4(s[8].clone(),s[9].clone(),fun2(s[10].clone()),fun3(s[11].clone()));
t[11] = xor4(fun3(s[8]),s[9],s[10],fun2(s[11]));
t[12] = xor4(fun2(s[12].clone()),fun3(s[13].clone()),s[14].clone(),s[15].clone());
t[13] = xor4(s[12].clone(),fun2(s[13].clone()),fun3(s[14].clone()),s[15].clone());
t[14] = xor4(s[12].clone(),s[13].clone(),fun2(s[14].clone()),fun3(s[15].clone()));
t[15] = xor4(fun3(s[12]),s[13],s[14],fun2(s[15]));
}
else
{
t[0] = xor4(funE(s[0].clone()),funB(s[1].clone()),funD(s[2].clone()),fun9(s[3].clone()));
t[1] = xor4(fun9(s[0].clone()),funE(s[1].clone()),funB(s[2].clone()),funD(s[3].clone()));
t[2] = xor4(funD(s[0].clone()),fun9(s[1].clone()),funE(s[2].clone()),funB(s[3].clone()));
t[3] = xor4(funB(s[0]),funD(s[1]),fun9(s[2]),funE(s[3]));
t[4] = xor4(funE(s[4].clone()),funB(s[5].clone()),funD(s[6].clone()),fun9(s[7].clone()));
t[5] = xor4(fun9(s[4].clone()),funE(s[5].clone()),funB(s[6].clone()),funD(s[7].clone()));
t[6] = xor4(funD(s[4].clone()),fun9(s[5].clone()),funE(s[6].clone()),funB(s[7].clone()));
t[7] = xor4(funB(s[4]),funD(s[5]),fun9(s[6]),funE(s[7]));
t[8] = xor4(funE(s[8].clone()),funB(s[9].clone()),funD(s[10].clone()),fun9(s[11].clone()));
t[9] = xor4(fun9(s[8].clone()),funE(s[9].clone()),funB(s[10].clone()),funD(s[11].clone()));
t[10] = xor4(funD(s[8].clone()),fun9(s[9].clone()),funE(s[10].clone()),funB(s[11].clone()));
t[11] = xor4(funB(s[8]),funD(s[9]),fun9(s[10]),funE(s[11]));
t[12] = xor4(funE(s[12].clone()),funB(s[13].clone()),funD(s[14].clone()),fun9(s[15].clone()));
t[13] = xor4(fun9(s[12].clone()),funE(s[13].clone()),funB(s[14].clone()),funD(s[15].clone()));
t[14] = xor4(funD(s[12].clone()),fun9(s[13].clone()),funE(s[14].clone()),funB(s[15].clone()));
t[15] = xor4(funB(s[12]),funD(s[13]),fun9(s[14]),funE(s[15]));
}
for(int j=0;j<t.length;j++)
{
State[i][2*j] = t[j][0];
State[i][2*j+1] = t[j][1];
}
}
}
static char[] fun2(char[] tt) //根据本算法中进行有限域乘2运算只是一个字节也就是两位十六进制
{
char ch = tt[0];
char dh = tt[1];
int chint,dhint;
int[] temparray = new int[8];
int[] xorarray = {0,0,0,1,1,0,1,1};
char[] result = new char[2];
int i;
if(ch>='0'&&ch<='9')
chint = ch-'0';
else
chint = ch-'A'+10;
if(dh>='0'&&dh<='9')
dhint = dh-'0';
else
dhint = dh-'A'+10;
for(i=3;i>=0;i--)
{
temparray[i] = chint%2;
chint = chint/2;
}
for(i=7;i>=4;i--)
{
temparray[i] = dhint%2;
dhint = dhint/2;
}
chint = temparray[0];//记下最高位
for(i=0;i<7;i++)//左移一位
temparray[i] = temparray[i+1];
temparray[7] = 0;
if(chint==1)
{
for(i=0;i<8;i++)
{
if(temparray[i]==xorarray[i])
temparray[i] = 0;
else
temparray[i] = 1;
}
}
chint = 0;
for(i=0;i<8;i++)
{
chint = chint*2+temparray[i];
if((i+1)%4==0)
{
if(chint<10)
result[i/4] = (char)(chint+'0');
else
result[i/4] = (char)(chint-10+'A');
chint = 0;
}
}
return result;
}
static char[] fun3(char[] tt)
{
char[] temparray = tt;
temparray = xor(temparray,fun2(tt));
return temparray;
}
static char[] fun9(char[] tt)//right
{
char[] ttclone = tt.clone();
for(int i=0;i<3;i++)
tt = fun2(tt);
tt = xor(tt,ttclone);
return tt;
}
static char[] funB(char[] tt)//right
{
char[] ttclone = tt.clone();
char[] fun3;
for(int i=0;i<3;i++)
tt = fun2(tt);
fun3 = fun3(ttclone);
tt = xor(tt,fun3);
return tt;
}
static char[] funD(char[] tt)
{
char[] ttclone = tt.clone();
char[] fun5 = tt.clone();
for(int i=0;i<3;i++)
tt = fun2(tt);
for(int i=0;i<2;i++)
fun5=fun2(fun5);
fun5 = xor(fun5,ttclone);
tt = xor(tt,fun5);
return tt;
}
//----------------------- write Results --------------------------------------------------
static char[] funE(char[] tt)//right
{
char[] fun4 = tt.clone();
char[] fun2 = tt.clone();
for(int i=0;i<3;i++)
tt = fun2(tt);
for(int i=0;i<2;i++)
fun4 = fun2(fun4);
fun2 = fun2(fun2);
tt = xor(tt,fun4);
tt = xor(tt,fun2);
return tt;
}
static void writeResults()throws IOException
{
//int hexlong = Long*2;
int num = 0;
char[]hexcode = new char[State.length*32];
lun[time-1] = new char[State.length*33];
for(int i=0,j=0,k=0;i<hexcode.length;i++,j++,k++)
{
if(j==32)
{
num++;
j=0;
lun[time-1][k++] = '\n';
}
hexcode[i] = State[num][j];
lun[time-1][k] =State[num][j];
}
//lun[time-1] = hexcode.clone();
/*Result[time] = new char[hexcode.length/2];
int a,b;
char aa,bb;
int k =0;
for(int i=0;i<hexcode.length;i+=2,k++)
{
aa = hexcode[i];
bb = hexcode[i+1];
if(aa>='0'&&aa<='9')
a = aa - '0';
else
a = aa - 'A'+10;
if(bb>='0'&&bb<='9')
b = bb - '0';
else
b = bb - 'A'+10;
Result[time][k] = (char)(a*16+b);
}
if(time==10)
System.out.println();*/
if(time==10){
int a,b;
char aa,bb;
int k =0;
//把加密结果写入outcode.dat文件,作为检查解密时用到的读取文件
for(int i=0;i<State.length;i++)
{
for(int j=0;j<State[i].length;j++)
{
hexcode[i*32+j] = State[i][j];
}
}
File out = new File(ff);
FileOutputStream file = new FileOutputStream(out);
for(int i=0;i<hexcode.length;i+=2,k++)
{
aa = hexcode[i];
bb = hexcode[i+1];
if(aa>='0'&&aa<='9')
a = aa - '0';
else
a = aa - 'A'+10;
if(bb>='0'&&bb<='9')
b = bb - '0';
else
b = bb - 'A'+10;
a=a*16+b;
file.write(a);
}
file.close();
}
}
/*public static void main(String[] args) throws IOException{
String keyfile = "key.dat";
String cfile = "wo.dat";
String jfile = "outcode.dat";
run(cfile,keyfile);
jiemi(jfile,keyfile);
}*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -