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

📄 form1.cs

📁 AES和MD5算法源码
💻 CS
📖 第 1 页 / 共 3 页
字号:
                BYTE4 temp = new BYTE4();
                for (int row = Nk; row < Nb * (Nr + 1); ++row)
                {
                    temp = new BYTE4();
                    temp.Byte4 = new byte[4];
                    temp.Byte4[0] = this.ExpandKey[row - 1].Byte4[0];
                    temp.Byte4[1] = this.ExpandKey[row - 1].Byte4[1];
                    temp.Byte4[2] = this.ExpandKey[row - 1].Byte4[2];
                    temp.Byte4[3] = this.ExpandKey[row - 1].Byte4[3];
                    if (row % Nk == 0)
                    {
                        temp = SubWord(RotWord(temp));
                        temp.Byte4[0] = (byte)((int)temp.Byte4[0] ^ (int)this.Rcon[row / Nk, 0]);
                        temp.Byte4[1] = (byte)((int)temp.Byte4[1] ^ (int)this.Rcon[row / Nk, 1]);
                        temp.Byte4[2] = (byte)((int)temp.Byte4[2] ^ (int)this.Rcon[row / Nk, 2]);
                        temp.Byte4[3] = (byte)((int)temp.Byte4[3] ^ (int)this.Rcon[row / Nk, 3]);
                    }
                    else if (Nk > 6 && (row % Nk == 4))
                    {
                        temp = SubWord(temp);
                    }
                    this.ExpandKey[row] = new BYTE4();
                    this.ExpandKey[row].Byte4 = new byte[4];
                    this.ExpandKey[row].Byte4[0] = (byte)((int)this.ExpandKey[row - this.Nk].Byte4[0] ^ (int)temp.Byte4[0]);
                    this.ExpandKey[row].Byte4[1] = (byte)((int)this.ExpandKey[row - this.Nk].Byte4[1] ^ (int)temp.Byte4[1]);
                    this.ExpandKey[row].Byte4[2] = (byte)((int)this.ExpandKey[row - this.Nk].Byte4[2] ^ (int)temp.Byte4[2]);
                    this.ExpandKey[row].Byte4[3] = (byte)((int)this.ExpandKey[row - this.Nk].Byte4[3] ^ (int)temp.Byte4[3]);
                }
            }

            public void SubBytes(byte[,] State)                                                   // 字节变换函数
            {
                for (int r = 0; r < 4; ++r)
                {
                    for (int c = 0; c < 4; ++c)
                    {
                        State[r, c] = Sbox[(State[r, c] >> 4), (State[r, c] & 0x0f)];
                    }
                }
            }

            public void ShiftRows(byte[,] State)                                                 // 行变换函数
            {
                byte[,] temp = new byte[4, 4];
                for (int r = 0; r < 4; ++r) 
                {
                    for (int c = 0; c < 4; ++c)
                    {
                        temp[r, c] = State[r, c];
                    }
                }

                for (int r = 1; r < 4; ++r)
                {
                    for (int c = 0; c < 4; ++c)
                    {
                        State[r, c] = temp[r, (c + r) % Nb];
                    }
                }
            }

            public void MixColumns(byte[,] State)                                                  // 列混合函数
            {
                byte[,] temp = new byte[4, 4];
                for (int r = 0; r < 4; ++r) 
                {
                    for (int c = 0; c < 4; ++c)
                    {
                        temp[r, c] = State[r, c];
                    }
                }
                for (int c = 0; c < 4; ++c)
                {
                    State[0, c] = (byte)((int)GfMultBy02(temp[0, c]) ^ (int)GfMultBy03(temp[1, c]) ^
                                            (int)GfMultBy01(temp[2, c]) ^ (int)GfMultBy01(temp[3, c]));

                    State[1, c] = (byte)((int)GfMultBy01(temp[0, c]) ^ (int)GfMultBy02(temp[1, c]) ^
                                            (int)GfMultBy03(temp[2, c]) ^ (int)GfMultBy01(temp[3, c]));

                    State[2, c] = (byte)((int)GfMultBy01(temp[0, c]) ^ (int)GfMultBy01(temp[1, c]) ^
                                            (int)GfMultBy02(temp[2, c]) ^ (int)GfMultBy03(temp[3, c]));

                    State[3, c] = (byte)((int)GfMultBy03(temp[0, c]) ^ (int)GfMultBy01(temp[1, c]) ^
                                            (int)GfMultBy01(temp[2, c]) ^ (int)GfMultBy02(temp[3, c]));
                }
            }

            public void AddRoundKey(int round, byte[,] State)                                   // 明文与轮密钥异或
            {
                for (int r = 0; r < 4; ++r)
                {
                    for (int c = 0; c < 4; ++c)
                    {
                        State[r, c] = (byte)((int)State[r, c] ^ (int)this.ExpandKey[(round * 4) + c].Byte4[r]);
                    }
                }
            }

            public void InvSubBytes(byte[,] State)                                                // 解密字节变换
            {
                for (int r = 0; r < 4; ++r)
                {
                    for (int c = 0; c < 4; ++c)
                    {
                        State[r, c] = iSbox[(State[r, c] >> 4), (State[r, c] & 0x0f)];
                    }
                }
            }

            public void InvShiftRows(byte[,] State)                                               // 解密行变换
            {
                byte[,] temp = new byte[4, 4];
                for (int r = 0; r < 4; ++r)  
                {
                    for (int c = 0; c < 4; ++c)
                    {
                        temp[r, c] = State[r, c];
                    }
                }
                for (int r = 1; r < 4; ++r) 
                {
                    for (int c = 0; c < 4; ++c)
                    {
                        State[r, (c + r) % Nb] = temp[r, c];
                    }
                }
            }

            public void InvMixColumns(byte[,] State)                                                // 解密列变换
            {
                byte[,] temp = new byte[4, 4];
                for (int r = 0; r < 4; ++r) 
                {
                    for (int c = 0; c < 4; ++c)
                    {
                        temp[r, c] = State[r, c];
                    }
                }

                for (int c = 0; c < 4; ++c)
                {
                    State[0, c] = (byte)((int)GfMultBy0e(temp[0, c]) ^ (int)GfMultBy0b(temp[1, c]) ^
                        (int)GfMultBy0d(temp[2, c]) ^ (int)GfMultBy09(temp[3, c]));
                    State[1, c] = (byte)((int)GfMultBy09(temp[0, c]) ^ (int)GfMultBy0e(temp[1, c]) ^
                        (int)GfMultBy0b(temp[2, c]) ^ (int)GfMultBy0d(temp[3, c]));
                    State[2, c] = (byte)((int)GfMultBy0d(temp[0, c]) ^ (int)GfMultBy09(temp[1, c]) ^
                        (int)GfMultBy0e(temp[2, c]) ^ (int)GfMultBy0b(temp[3, c]));
                    State[3, c] = (byte)((int)GfMultBy0b(temp[0, c]) ^ (int)GfMultBy0d(temp[1, c]) ^
                        (int)GfMultBy09(temp[2, c]) ^ (int)GfMultBy0e(temp[3, c]));
                }
            }

            public void CipherOnce(byte[,] State)                                            // 加密一个分组
            {
                AddRoundKey(0, State);
                for (int round = 1; round <= (this.Nr - 1); ++round)  
                {
                    SubBytes(State);
                    ShiftRows(State);
                    MixColumns(State);
                    AddRoundKey(round, State);
                }  

                SubBytes(State);
                ShiftRows(State);
                AddRoundKey(this.Nr, State);

                for (int i = 0; i < this.Nb; i++)                                           // 生成密文ASCII码
                {
                    for (int j = 0; j < 4; j++)
                    {
                        this.CryptographByte.Add(State[i, j]);
                    }
                }
            }

            public void InvCipherOnce(byte[,] State)                                          // 解密一个分组
            {
                AddRoundKey(Nr, State);
                for (int round = Nr - 1; round >= 1; --round)  
                {
                    InvShiftRows(State);
                    InvSubBytes(State);
                    AddRoundKey(round, State);
                    InvMixColumns(State);
                } 

                InvShiftRows(State);
                InvSubBytes(State);
                AddRoundKey(0, State);

                for (int i = 0; i < this.Nb; i++)                                            // 生成解密后明文ASCII码
                {
                    for (int j = 0; j < 4; j++)
                    {
                        this.InvMessageByte.Add(State[i, j]);
                    }
                }
            }

          
            public void CipherAll()
            {
                ASCIIEncoding asc = new ASCIIEncoding();                                       // 全文加密
                this.CryptographByte = new List<byte>();
                foreach (byte[,] State in this.State)
                {
                    this.CipherOnce(State);
                }
                this.Cryptograph = asc.GetString(this.CryptographByte.ToArray());
            }

            public void InvCipherAll()                                                           // 全文解密
            {
                ASCIIEncoding asc = new ASCIIEncoding();
                this.InvMessageByte = new List<byte>();
                foreach (byte[,] State in this.State)
                {
                    this.InvCipherOnce(State);
                }
                this.InvMessage = asc.GetString(this.InvMessageByte.ToArray());
            }

            public AES()
            {
                this.InitRcon();
                this.InitSboxAndiSbox();
                this.Nb = 4;
                this.Nk = 4;
                this.Nr = 10;
            }
        }

        AES AESCipher = new AES();
        public bool Invciphered = false;

        private void KeySizeSelected(object sender, EventArgs e)
        {
            if (radioButton1.Checked == true) AESCipher.Nk = 4;
            else if (radioButton2.Checked == true) AESCipher.Nk = 6;
            else if (radioButton3.Checked == true) AESCipher.Nk = 8;
        }

        private void randomPlaintext_Click(object sender, EventArgs e)
        {
            int number, q;
            string plintext = string.Empty;
            Random random = new Random();
            Random a = new Random(DateTime.Now.Millisecond);
            q = random.Next(0, 129);
            for (int i = 0; i < q; i++)
            {
                number = random.Next(0, 9);
                plintext = plintext + number.ToString();
            }
            plaintextInput.Clear();
            plaintextInput.Text = plintext;
        }

        private void Clear_Click(object sender, EventArgs e)
        {
            plaintextInput.Clear();
            keyInput.Clear();
            ciphertextOutput.Clear();
            plaintextOutput.Clear();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Invciphered = false;
            if (plaintextInput.Text == "")
            {
                MessageBox.Show("明文不能为空", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else if (keyInput.Text == "")
            {
                MessageBox.Show("密钥不能为空", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            AESCipher.Message = plaintextInput.Text;
            AESCipher.KeySeed = keyInput.Text;
            AESCipher.InitRoundTime();
            AESCipher.InitExpentKey();
            AESCipher.InitState();
            AESCipher.CipherAll();
            ciphertextOutput.Text = AESCipher.Cryptograph;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (ciphertextOutput.Text != "")
            {
                if (Invciphered == true) return;
                AESCipher.KeySeed = keyInput.Text;
                AESCipher.InitExpentKey();
                AESCipher.InvCipherAll();
                Invciphered = true;
                plaintextOutput.Text = AESCipher.InvMessage;
            }
        }

        private void randomKey_Click(object sender, EventArgs e)
        {
            radioButton1.Select();
            int number;
            string key = string.Empty;
            Random random = new Random();
            int q;
            q = random.Next(0, 129);
                for (int i = 0; i < q; i++)
                {
                    number = random.Next(0, 9);
                    key = key + number.ToString();
                }
          
            keyInput.Clear();
            keyInput.Text = key;
        }

        private void Exit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("实验一可以自主在输入框内输入明文、密钥;实验二可以自主在输入框内输入消息(字符串)。", "卢金杰的提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        // AES代码部分结束
     }
}

⌨️ 快捷键说明

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