📄 aes.java
字号:
temp[0]=w[(i-1)*4+0];temp[1]=w[(i-1)*4+1];
temp[2]=w[(i-1)*4+2];temp[3]=w[(i-1)*4+3];
if((i%Nk)==0)
{
RotWord(temp);
SubWord(temp);
for(int j=0;j<4;j++)
temp[j]^=Rcon[((i-1)/Nk)*4+j];//与Rcon异或;
}
else if(Nk==8&&i%Nk==4)
SubWord(temp);
w[i*4+0] = (char)(w[(i-Nk)*4+0]^temp[0]);
w[i*4+1] = (char)(w[(i-Nk)*4+1]^temp[1]);
w[i*4+2] = (char)(w[(i-Nk)*4+2]^temp[2]);
w[i*4+3] = (char)(w[(i-Nk)*4+3]^temp[3]);
i++;
}
}
/**
* This method is used to do S-replace.
* @param state is a array which stored the data block.
* @param Nb the length of data block.
///////////////////////////////////////////////////
功能: S盒置换
入口参数: Nb为以32bit为单位的明文块的大小;
state为明文块;
////////////////////////////////////////////////////
*/
public void SubChar(char []state,int Nb)
{
for(int i=0;i<4*Nb;i++)
state[i]=S_BOX[state[i]%256];
}
/**
* @param state is a array which stored the data block.
* @param Nb the length of data block.
/////////////////////////////////////////////////////
功能: 加密对明文块进行移位运算;
入口参数:state是明文块;
Nb是以32比特为单位的明文块的大小;
//////////////////////////////////////////////////////
*/
public void ShiftRows(char []state, int Nb)
{
char[] t = new char [8];
for( int r=0;r<4;r++)
{
for(int c=0;c<Nb;c++)t[c]= state[Nb*r+(r+c)%Nb];
for(int c=0;c<Nb;c++)
state[Nb*r+c]=t[c];
}
}
/***
* This method is used to mix columns.
* @param state is a array which stored the data block.
* @param Nb the length of data block.
//////////////////////////////////////////////////
功能:加密时对明文块进行列混合变换;
入口参数:state是明文块;
Nb是以32比特为单位的明文块的大小;
//////////////////////////////////////////////////
*/
public void MixColumns(char[]state, int Nb)
{
int [] t = new int[4];
for( int c=0;c<Nb;c++)
{
for(int r=0;r<4;r++)t[r] = state[Nb*r+c];
for(int r=0;r<4;r++)
{
state[Nb*r+c] = (char)(Ffmul(0x02,t[r])^Ffmul(0x03,t[(r+1)%4])
^t[(r+2)%4]^t[(r+3)%4]);
}
}
}
/**
* This method is used to get the product of A and B.
* @param A first number.
* @param B second number.
* @return the product of A and B.
/////////////////////////////////////////////////////////
功能:返回两个域元素A,B的积;
/////////////////////////////////////////////////////////
*/
public int Ffmul(int A, int B)
{
//查对数表;
if(A==0||B==0)return 0;
A = Log[A];
B = Log[B];
A =(A+B)%0xff;
//查反对数表;
A = Log_1[A];
return A;
}
/**
* This method is used to add round key and data.
* @param state is the array which contains the data.
* @param Nb length of data block(32bits).
* @param round the index of current round.
///////////////////////////////////////////////////////////////
功能: 轮密钥加变换;
入口参数: state明文块
w为子密钥,Nb为明文块的大小,round为当前加密的轮数;
///////////////////////////////////////////////////////////////
*/
public void AddRoundKey(char[]state, int Nb,int round)
{
for(int c=0;c<Nb;c++,round++)
for(int r=0;r<4;r++)
state[r*Nb+c] = (char)(state[r*Nb+c]^w[round*4+r]);
}
/**
* This method is used to do exchang durying the proccess of encryption.
* @param state is a array which contains the data to encrypt.
* @param Nb the length of data block(32bits).
* @param Nr the length of user key
* @return the cryptograph.
*/
public byte[] Transform(char[]state, int Nb,int Nr)
{
int round=1;
AddRoundKey(state,Nb,0);
for(;round<Nr;round++)
{
SubChar(state,Nb);
ShiftRows(state,Nb);
MixColumns(state,Nb);
AddRoundKey(state,Nb,round*Nb);
}
SubChar(state,Nb);
ShiftRows(state,Nb);
AddRoundKey(state,Nb,round*Nb);
return CharToByte(state);
}
/**
* This method is used to do exchang durying the proccess of de-encryption.
* @param state is a array which contains the cryptograph to de-encrypt.
* @param Nb the length of cryptograph block(32bits).
* @param Nr the length of user key
* @return the original text.
*/
public byte[] ReTransform(char []state, int Nb,int Nr)
{
AddRoundKey(state, Nb,Nr*Nb);
for(int round=Nr-1;round>=1;round--)
{
InvShiftRows(state,Nb);
InvSubint(state,Nb);
AddRoundKey(state,Nb,round*Nb);
InvMixColumns(state,Nb);
}
InvShiftRows(state,Nb);
InvSubint(state,Nb);
AddRoundKey(state,Nb,0);
return CharToByte(state);
}
/**
* This method is used to do S-replace durying de-encrypt cryptograph .
* @param state is a array which contains the cryptograph to de-encrypt.
* @param Nb the length of cryptograph block(32bits).
* @param Nr the length of user key
/////////////////////////////////////////////////////////////
功能:解密时的S盒逆置换;
入口参数:state为密文块;
Nb为密文块的大小;
*/
public void InvSubint(char []state, int Nb)
{
for(int i=0;i<4*Nb;i++)
state[i] = S_BOX_1[state[i]%256];
}
/**
* This method is used to shift rows durying de-encyrpt cryptograph .
* @param state is a array which contains the cryptograph to de-encrypt.
* @param Nb the length of cryptograph block(32bits).
////////////////////////////////////////////////////////////////
功能:解密的时候的右移位变换;
入口参数:state为密文块;
Nb为密文块的大小;
////////////////////////////////////////////////////////////////
*/
public void InvShiftRows(char[]state, int Nb)
{
char [] t = new char[8];
for( int r=0;r<4;r++)
{
for(int c=0;c<Nb;c++)
t[(c+r)%Nb] = state[r*Nb+c];
for(int c=0;c<Nb;c++)
state[r*Nb+c]=t[c];
}
}
/**
* This method is used to mix columns durying de-encrypt cyyptograph.
* @param state is a array which contains the cryptograph to de-encrypt.
* @param Nb the length of cryptograph block(32bits).
//////////////////////////////////////////////////////////////
功能:解密时的列混合变换;
入口参数:state为密文块;
Nb为密文块的大小;
//////////////////////////////////////////////////////////////
*/
public void InvMixColumns(char []state, int Nb)
{
char []t = new char[4];
for( int c=0;c<Nb;c++)
{
for(int r=0;r<4;r++)t[r] = state[Nb*r+c];
for(int r=0;r<4;r++)
{
state[Nb*r+c] = (char)(Ffmul(0x0e,t[r])^Ffmul(0x0b,t[(r+1)%4])
^Ffmul(0x0d,t[(r+2)%4])^Ffmul(0x09,t[(r+3)%4]));
}
}
}
/**
* This method is used to transform a array from byte type to char type.
* @param data a byte type array.
* @return a char type array.
*/
public static char[] ByteToChar(byte[] data)
{
char []A = new char[data.length];
for(int i = 0;i<data.length;i++)
A[i] = (char)data[i];
return A;
}
/**
* This method is used to transform a array from char type to byte type.
* @param data a char type array.
* @return a byte type array.
*/
public static byte[]CharToByte(char[]data)
{
byte[] A = new byte[data.length];
for(int i = 0;i<data.length;i++)
A[i] = (byte)data[i];
return A;
}
public static void main(String[] args) {
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -