📄 aes.cs
字号:
}
for (int r = 1; r < 4; ++r) // shift temp into State
{
for (int c = 0; c < 4; ++c)
{
this.State[r,c] = temp[ r, (c + r) % Nb ];
}
}
} // ShiftRows()
private void InvShiftRows()
{
byte[,] temp = new byte[4,4];
for (int r = 0; r < 4; ++r) // copy State into temp[]
{
for (int c = 0; c < 4; ++c)
{
temp[r,c] = this.State[r,c];
}
}
for (int r = 1; r < 4; ++r) // shift temp into State
{
for (int c = 0; c < 4; ++c)
{
this.State[r, (c + r) % Nb ] = temp[r,c];
}
}
} // InvShiftRows()
private void MixColumns()
{
byte[,] temp = new byte[4,4];
for (int r = 0; r < 4; ++r) // copy State into temp[]
{
for (int c = 0; c < 4; ++c)
{
temp[r,c] = this.State[r,c];
}
}
for (int c = 0; c < 4; ++c)
{
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]) );
}
} // MixColumns
private void InvMixColumns()
{
byte[,] temp = new byte[4,4];
for (int r = 0; r < 4; ++r) // copy State into temp[]
{
for (int c = 0; c < 4; ++c)
{
temp[r,c] = this.State[r,c];
}
}
for (int c = 0; c < 4; ++c)
{
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]) );
}
} // InvMixColumns
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) );
}
private void KeyExpansion()
{
this.w = new byte[Nb * (Nr+1), 4]; // 4 columns of bytes corresponds to a word
for (int row = 0; row < Nk; ++row)
{
this.w[row,0] = this.key[4*row];
this.w[row,1] = this.key[4*row+1];
this.w[row,2] = this.key[4*row+2];
this.w[row,3] = this.key[4*row+3];
}
byte[] temp = new byte[4];
for (int row = Nk; row < Nb * (Nr+1); ++row)
{
temp[0] = this.w[row-1,0]; temp[1] = this.w[row-1,1];
temp[2] = this.w[row-1,2]; temp[3] = this.w[row-1,3];
if (row % Nk == 0)
{
temp = SubWord(RotWord(temp));
temp[0] = (byte)( (int)temp[0] ^ (int)this.Rcon[row/Nk,0] );
temp[1] = (byte)( (int)temp[1] ^ (int)this.Rcon[row/Nk,1] );
temp[2] = (byte)( (int)temp[2] ^ (int)this.Rcon[row/Nk,2] );
temp[3] = (byte)( (int)temp[3] ^ (int)this.Rcon[row/Nk,3] );
}
else if ( Nk > 6 && (row % Nk == 4) )
{
temp = SubWord(temp);
}
// w[row] = w[row-Nk] xor temp
this.w[row,0] = (byte) ( (int)this.w[row-Nk,0] ^ (int)temp[0] );
this.w[row,1] = (byte) ( (int)this.w[row-Nk,1] ^ (int)temp[1] );
this.w[row,2] = (byte) ( (int)this.w[row-Nk,2] ^ (int)temp[2] );
this.w[row,3] = (byte) ( (int)this.w[row-Nk,3] ^ (int)temp[3] );
} // for loop
} // KeyExpansion()
private byte[] SubWord(byte[] word)
{
byte[] result = new byte[4];
result[0] = this.Sbox[ word[0] >> 4, word[0] & 0x0f ];
result[1] = this.Sbox[ word[1] >> 4, word[1] & 0x0f ];
result[2] = this.Sbox[ word[2] >> 4, word[2] & 0x0f ];
result[3] = this.Sbox[ word[3] >> 4, word[3] & 0x0f ];
return result;
}
private byte[] RotWord(byte[] word)
{
byte[] result = new byte[4];
result[0] = word[1];
result[1] = word[2];
result[2] = word[3];
result[3] = word[0];
return result;
}
public void Dump()
{
Console.WriteLine("Nb = " + Nb + " Nk = " + Nk + " Nr = " + Nr);
Console.WriteLine("\nThe key is \n" + DumpKey() );
Console.WriteLine("\nThe Sbox is \n" + DumpTwoByTwo(Sbox));
Console.WriteLine("\nThe w array is \n" + DumpTwoByTwo(w));
Console.WriteLine("\nThe State array is \n" + DumpTwoByTwo(State));
}
public string DumpKey()
{
string s = "";
for (int i = 0; i < key.Length; ++i)
s += key[i].ToString("x2") + " ";
return s;
}
public string DumpTwoByTwo(byte[,] a)
{
string s ="";
for (int r = 0; r < a.GetLength(0); ++r)
{
s += "["+r+"]" + " ";
for (int c = 0; c < a.GetLength(1); ++c)
{
s += a[r,c].ToString("x2") + " " ;
}
s += "\n";
}
return s;
}
} // class Aes
} // ns AesLib
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -