⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 aes.cs

📁 利用C#做的AES加密和解密的小程序
💻 CS
📖 第 1 页 / 共 2 页
字号:
                    temp[r, c] = this.State[r, c];
                }
            }

            for (int r = 1; r < 4; r ++) //将变换后的数组temp复制给State
            {
                for (int c = 0; c < 4; c ++)
                {
                    this.State[r, c] = temp[r, (c + r) % 4];
                }
            }
        } 

        private void 逆ShiftRows()//对应于ShiftBows的逆操作
        {
            byte[,] temp = new byte[4, 4];
            for (int r = 0; r < 4; r ++)  
            {
                for (int c = 0; c < 4; c ++)
                {
                    temp[r, c] = this.State[r, c];
                }
            }
            for (int r = 1; r < 4; r ++)  
            {
                for (int c = 0; c < 4; c ++)
                {
                    this.State[r, (c + r) % 4] = temp[r, c];
                }
            }
        }  

        private void MixColumn()//书中算法3.5,对State的四列中的每一列进行操作
        {
            byte[,] temp = new byte[4, 4];
            for (int r = 0; r < 4; r ++)  
            {
                for (int c = 0; c < 4; c ++)
                {
                    temp[r, c] = this.State[r, c];
                }
            }

            for (int c = 0; c < 4; c ++)//do...for
            {
                this.State[0, c] = (byte)((int)Fieldmultby02(temp[0, c]) ^ (int)Fieldmultby03(temp[1, c]) ^
                                           (int)Fieldmultby01(temp[2, c]) ^ (int)Fieldmultby01(temp[3, c]));
                this.State[1, c] = (byte)((int)Fieldmultby01(temp[0, c]) ^ (int)Fieldmultby02(temp[1, c]) ^
                                           (int)Fieldmultby03(temp[2, c]) ^ (int)Fieldmultby01(temp[3, c]));
                this.State[2, c] = (byte)((int)Fieldmultby01(temp[0, c]) ^ (int)Fieldmultby01(temp[1, c]) ^
                                           (int)Fieldmultby02(temp[2, c]) ^ (int)Fieldmultby03(temp[3, c]));
                this.State[3, c] = (byte)((int)Fieldmultby03(temp[0, c]) ^ (int)Fieldmultby01(temp[1, c]) ^
                                           (int)Fieldmultby01(temp[2, c]) ^ (int)Fieldmultby02(temp[3, c]));
            }
        }  

        private void 逆MixColumn()//对应于MixColumn的逆运算
        {
            byte[,] temp = new byte[4, 4];
            for (int r = 0; r < 4; r ++)  
            {
                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)Fieldmultby0e(temp[0, c]) ^ (int)Fieldmultby0b(temp[1, c]) ^
                                           (int)Fieldmultby0d(temp[2, c]) ^ (int)Fieldmultby09(temp[3, c]));
                this.State[1, c] = (byte)((int)Fieldmultby09(temp[0, c]) ^ (int)Fieldmultby0e(temp[1, c]) ^
                                           (int)Fieldmultby0b(temp[2, c]) ^ (int)Fieldmultby0d(temp[3, c]));
                this.State[2, c] = (byte)((int)Fieldmultby0d(temp[0, c]) ^ (int)Fieldmultby09(temp[1, c]) ^
                                           (int)Fieldmultby0e(temp[2, c]) ^ (int)Fieldmultby0b(temp[3, c]));
                this.State[3, c] = (byte)((int)Fieldmultby0b(temp[0, c]) ^ (int)Fieldmultby0d(temp[1, c]) ^
                                           (int)Fieldmultby09(temp[2, c]) ^ (int)Fieldmultby0e(temp[3, c]));
            }
        }
#region FieldMult
        private static byte Fieldmultby01(byte b)
        {
            return b;
        }

        private static byte Fieldmultby02(byte b)
        {
            if (b < 0x80)
                return (byte)(int)(b << 1);
            else
                return (byte)((int)(b << 1) ^ (int)(0x1b));
        }

        private static byte Fieldmultby03(byte b)
        {
            return (byte)((int)Fieldmultby02(b) ^ (int)b);
        }

        private static byte Fieldmultby09(byte b)
        {
            return (byte)((int)Fieldmultby02(Fieldmultby02(Fieldmultby02(b))) ^
                           (int)b);
        }

        private static byte Fieldmultby0b(byte b)
        {
            return (byte)((int)Fieldmultby02(Fieldmultby02(Fieldmultby02(b))) ^
                           (int)Fieldmultby02(b) ^
                           (int)b);
        }

        private static byte Fieldmultby0d(byte b)
        {
            return (byte)((int)Fieldmultby02(Fieldmultby02(Fieldmultby02(b))) ^
                           (int)Fieldmultby02(Fieldmultby02(b)) ^
                           (int)(b));
        }

        private static byte Fieldmultby0e(byte b)
        {
            return (byte)((int)Fieldmultby02(Fieldmultby02(Fieldmultby02(b))) ^
                           (int)Fieldmultby02(Fieldmultby02(b)) ^
                           (int)Fieldmultby02(b));
        }

#endregion
        private void KeyExpansion()//扩展密钥操作
        {
            this.w = new byte[60 , 4];  //一个字由四个字节组成。书中算法3.6的for...do

            for (int row = 0; row < 8; row ++)//for...do
            {
                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 = 8; row < 60; 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 % 8 == 0)
                {
                    temp = SubWord(RotWord(temp));

                    temp[0] = (byte)((int)temp[0] ^ (int)this.Rcon[row / 8, 0]);
                    temp[1] = (byte)((int)temp[1] ^ (int)this.Rcon[row / 8, 1]);
                    temp[2] = (byte)((int)temp[2] ^ (int)this.Rcon[row / 8, 2]);
                    temp[3] = (byte)((int)temp[3] ^ (int)this.Rcon[row / 8, 3]);
                }
                else if (row % 8 == 4)
                {
                    temp = SubWord(temp);
                }

                
                this.w[row, 0] = (byte)((int)this.w[row - 8, 0] ^ (int)temp[0]);
                this.w[row, 1] = (byte)((int)this.w[row - 8, 1] ^ (int)temp[1]);
                this.w[row, 2] = (byte)((int)this.w[row - 8, 2] ^ (int)temp[2]);
                this.w[row, 3] = (byte)((int)this.w[row - 8, 3] ^ (int)temp[3]);

            } 
        }  

        private byte[] SubWord(byte[] word)//对四个字节使用AES的S盒
        {
            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)//RotWord对四个字节进行循环移位操作
        {
            byte[] result = new byte[4];
            result[0] = word[1];
            result[1] = word[2];
            result[2] = word[3];
            result[3] = word[0];
            return result;
        }     

    } 
}  

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -