📄 aes.cs
字号:
{
output[i]=this.State[i % 4, i / 4];//State数组竖着赋值到输出字符串
}
}
//AddRoundKey()轮密钥加函数
private void AddRoundKey(int round)
{
for (int r = 0; r < 4; ++r)//r为列
{
for (int c = 0; c < 4; ++c)//c为行
{
this.State[c, r] = (byte)((int)this.State[c, r] ^ (int)w[(round * 4) + r, c]);//XOR
}
}
}
//SubBytes()字节代换函数
private void SubBytes()
{
for(int i=0;i<4;i++)
for (int j = 0; j < 4; j++)
{
this.State[j, i] = this.Sbox[(int)(this.State[j, i] >> 4)/*高4位*/, (int)(this.State[j, i] & 0x0f)/*低4位*/];
}
}
//ShiftRows()行变换函数,第0行循环左移0字节,第1行循环左移1字节,第2行循环左移2字节,第3行循环左移3字节
private void ShiftRows()
{
byte[,] temp = new byte[4, 4];
for (int r = 0; r < 4; ++r) // 复制State到临时数组temp[]
{
for (int c = 0; c < 4; ++c)
{
temp[r, c] = this.State[r, c];
}
}
for (int r = 0; r < 4; ++r) //第0行循环左移0字节,第1行循环左移1字节,第2行循环左移2字节,第3行循环左移3字节
{
for (int c = 0; c < 4; ++c)
{
this.State[r, c] = temp[r, (c + r) % Nb];//r表示循环左移的字节数
}
}
}
//MixColumns()列混淆函数
private void MixColumns()
{
byte[,] temp = new byte[4, 4];
for (int r = 0; r < 4; ++r) // 复制 State 到临时数组 temp[]
{
for (int c = 0; c < 4; ++c)
{
temp[r, c] = this.State[r, c];
}
}
for (int c = 0; c < 4; ++c)//参照课本P107图5.5和式子5.4,注意运算都是在GF(2E8)上的
{
this.State[0, c] = (byte)((int)gfmultby02(temp[0, c]) ^ (int)gfmultby03(temp[1, c]) ^
(int)gfmultby01(temp[2, c]) ^ (int)gfmultby01(temp[3, c]));
this.State[1, c] = (byte)((int)gfmultby01(temp[0, c]) ^ (int)gfmultby02(temp[1, c]) ^
(int)gfmultby03(temp[2, c]) ^ (int)gfmultby01(temp[3, c]));
this.State[2, c] = (byte)((int)gfmultby01(temp[0, c]) ^ (int)gfmultby01(temp[1, c]) ^
(int)gfmultby02(temp[2, c]) ^ (int)gfmultby03(temp[3, c]));
this.State[3, c] = (byte)((int)gfmultby03(temp[0, c]) ^ (int)gfmultby01(temp[1, c]) ^
(int)gfmultby01(temp[2, c]) ^ (int)gfmultby02(temp[3, c]));
}
}
//GF(2E8)上的运算如下:
private static byte gfmultby01(byte b)
{
return b;
}
private static byte gfmultby02(byte b)
{
if (b < 0x80)
return (byte)(int)(b << 1);
else
return (byte)((int)(b << 1) ^ (int)(0x1b));
}
private static byte gfmultby03(byte b)
{
return (byte)((int)gfmultby02(b) ^ (int)b);
}
private static byte gfmultby09(byte b)
{
return (byte)((int)gfmultby02(gfmultby02(gfmultby02(b))) ^
(int)b);
}
private static byte gfmultby0b(byte b)
{
return (byte)((int)gfmultby02(gfmultby02(gfmultby02(b))) ^
(int)gfmultby02(b) ^
(int)b);
}
private static byte gfmultby0d(byte b)
{
return (byte)((int)gfmultby02(gfmultby02(gfmultby02(b))) ^
(int)gfmultby02(gfmultby02(b)) ^
(int)(b));
}
private static byte gfmultby0e(byte b)
{
return (byte)((int)gfmultby02(gfmultby02(gfmultby02(b))) ^
(int)gfmultby02(gfmultby02(b)) ^
(int)gfmultby02(b));
}
/********************************************************************************************************************************************/
//以下为解密过程
/********************************************************************************************************************************************/
//InvCipher()解密函数
public void InvCipher(byte[] input, byte[] output) // decipher 16-bit input
{
//接收输入,存入State数组
this.State = new byte[4, Nb];//Nb恒为4
for (int i = 0; i < 16; i++)
{
this.State[i % 4, i / 4] = input[i];//State数组竖着存放输入字符串
}
AddRoundKey(Nr);//初始轮密钥加,第Nr轮
for (int round = Nr - 1; round >= 1; round--) //每一轮过程如下
{
InvShiftRows();//行移位求逆
InvSubBytes();//字节代换求逆
AddRoundKey(round);//轮密钥加
InvMixColumns();//列混淆求逆
}
//最后一轮
InvShiftRows();//行移位求逆
InvSubBytes();//字节代换求逆
AddRoundKey(0);//轮密钥加
//输出处理
for (int i = 0; i < (4 * Nb); ++i)
{
output[i] = this.State[i % 4, i / 4];//State数组竖着赋值到输出字符串
}
}
//InvSubBytes()字节代换求逆函数
private void InvSubBytes()
{
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
{
this.State[j, i] = this.iSbox[(int)(this.State[j, i] >> 4)/*高4位*/, (int)(this.State[j, i] & 0x0f)/*低4位*/];
}
}
//InvShiftRows()行变换求逆函数,第0行循环右移0字节,第1行循环右移1字节,第2行循环右移2字节,第3行循环右移3字节
private void InvShiftRows()
{
byte[,] temp = new byte[4, 4];
for (int r = 0; r < 4; ++r) // 复制State到临时数组temp[]
{
for (int c = 0; c < 4; ++c)
{
temp[r, c] = this.State[r, c];
}
}
for (int r = 0; r < 4; ++r) //第0行循环右移0字节,第1行循环右移1字节,第2行循环右移2字节,第3行循环右移3字节
{
for (int c = 0; c < 4; ++c)
{
this.State[r, (c + r) % Nb] = temp[r, c];//这里只要与循环左移的下标对调即可
}
}
}
//InvMixColumns()列混淆求逆函数
private void InvMixColumns()
{
byte[,] temp = new byte[4, 4];
for (int r = 0; r < 4; ++r) //复制 State 到临时数组 temp[]
{
for (int c = 0; c < 4; ++c)
{
temp[r, c] = this.State[r, c];
}
}
for (int c = 0; c < 4; ++c)//参照课本P108式子5.5,注意运算都是在GF(2E8)上的,不过这里使用了
//一个别人写的方法,使用相同的矩阵,而课本中的矩阵使用已包含在gfmultby函数之中
{
this.State[0, c] = (byte)((int)gfmultby0e(temp[0, c]) ^ (int)gfmultby0b(temp[1, c]) ^
(int)gfmultby0d(temp[2, c]) ^ (int)gfmultby09(temp[3, c]));
this.State[1, c] = (byte)((int)gfmultby09(temp[0, c]) ^ (int)gfmultby0e(temp[1, c]) ^
(int)gfmultby0b(temp[2, c]) ^ (int)gfmultby0d(temp[3, c]));
this.State[2, c] = (byte)((int)gfmultby0d(temp[0, c]) ^ (int)gfmultby09(temp[1, c]) ^
(int)gfmultby0e(temp[2, c]) ^ (int)gfmultby0b(temp[3, c]));
this.State[3, c] = (byte)((int)gfmultby0b(temp[0, c]) ^ (int)gfmultby0d(temp[1, c]) ^
(int)gfmultby09(temp[2, c]) ^ (int)gfmultby0e(temp[3, c]));
}
}
/********************************************************************************************************************************************/
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -