Exemple #1
0
        public static int[,] Getdecryption(string str, int[,] key)
        {
            int[] cipherkey = new int[16];
            str = str.ToLower();
            char[] ch = str.ToCharArray();

            int[,] cipher = new int[str.Length / 32, 16];
            int[,] plian  = new int[str.Length / 32, 16];
            int[] plaintext = new int[16];

            StringBuilder s = new StringBuilder(100000);

            for (int i = 0; i < str.Length; i++)
            {
                for (int j = 3; j >= 0; j--)
                {
                    s = s.Append(Decryption.GetNum(ch[i]) >> j & 1);
                }
            }
            for (int i = 0; i < cipher.GetLength(0); i++)
            {
                for (int j = 0; j < 16; j++)
                {
                    cipher[i, j] = Convert.ToInt32(s.ToString(j * 8 + i * 128, 8), 2);
                }
            }

            EncryptionForm encryForm = new EncryptionForm();

            encryForm.Show();
            for (int i = 0; i < cipher.GetLength(0); i++)
            {
                for (int h = 0; h < 16; h++)
                {
                    cipherkey[h] = key[key.GetLength(0) - 1, h];
                    plaintext[h] = cipher[i, h];
                }
                encryForm.SetText("密钥:", cipherkey);
                plaintext = AddRoundKey(plaintext, cipherkey);
                encryForm.SetText("InvAddRoundKey:", plaintext);
                encryForm.Set();
                for (int j = key.GetLength(0) - 2; j > 0; j--)
                {
                    for (int h = 0; h < 16; h++)
                    {
                        cipherkey[h] = key[j, h];
                    }
                    for (int h = 0; h < plaintext.Length; h++)
                    {
                        plaintext[h] = SubBytes.GetByteSub(plaintext[h]);
                    }
                    encryForm.SetText("InvSubBytes:", plaintext);
                    plaintext = InvShiftRows(plaintext);
                    encryForm.SetText("InvShiftRows:", plaintext);
                    plaintext = MixColumn.InvMixColumn(plaintext);
                    encryForm.SetText("InvMixColumn:", plaintext);
                    encryForm.SetText("轮密钥:", cipherkey);
                    plaintext = AddRoundKey(plaintext, cipherkey);
                    encryForm.SetText("InvAddRoundKey:", plaintext);
                    encryForm.Set();
                }
                for (int h = 0; h < 16; h++)
                {
                    cipherkey[h] = key[0, h];
                }
                for (int h = 0; h < plaintext.Length; h++)
                {
                    plaintext[h] = SubBytes.GetByteSub(plaintext[h]);
                }
                encryForm.SetText("InvSubBytes:", plaintext);
                plaintext = InvShiftRows(plaintext);
                encryForm.SetText("InvShiftRows:", plaintext);
                encryForm.SetText("轮密钥:", cipherkey);
                plaintext = AddRoundKey(plaintext, cipherkey);
                encryForm.SetText("InvAddRoundKey:", plaintext);
                for (int j = 0; j < 16; j++)
                {
                    plian[i, j] = plaintext[j];
                }
            }
            encryForm.Settext();
            return(plian);
        }
        private void DecryptionBtn_Click_1(object sender, EventArgs e)
        {
            if (radioBtn1.Checked == true)
            {
                if (Encoding.Default.GetBytes(KeyText.Text).Length != 16)
                {
                    MessageBox.Show("密钥个数必须为16个字符或8个汉字!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            if (radioBtn2.Checked == true)
            {
                if (Encoding.Default.GetBytes(KeyText.Text).Length != 24)
                {
                    MessageBox.Show("密钥个数必须为24个字符或12个汉字!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            if (radioBtn3.Checked == true)
            {
                if (Encoding.Default.GetBytes(KeyText.Text).Length != 32)
                {
                    MessageBox.Show("密钥个数必须为32个字符或16个汉字!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            else
            {
                if (KeyText.Text.Length == 0)
                {
                    MessageBox.Show("请输入密钥!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                if (CipherText.Text.Length == 0 && KeyText.Text.Length != 0)
                {
                    MessageBox.Show("请输入密文!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                sw4.Start();
                int[,] key       = KeyExpansion.GetKeyExpansion(KeyText.Text.ToString().Trim());
                int[,] cipherKey = new int[key.GetLength(0), key.GetLength(1)];
                int[] key1 = new int[key.GetLength(1)];
                for (int i = 0; i < key.GetLength(1); i++)
                {
                    cipherKey[0, i] = key[0, i];
                    cipherKey[key.GetLength(0) - 1, i] = key[key.GetLength(0) - 1, i];
                }
                for (int i = 1; i < key.GetLength(0) - 1; i++)
                {
                    for (int j = 0; j < key.GetLength(1); j++)
                    {
                        key1[j] = key[i, j];
                    }
                    key1 = MixColumn.InvMixColumn(key1);
                    for (int j = 0; j < key.GetLength(1); j++)
                    {
                        cipherKey[i, j] = key1[j];
                    }
                }
                int[,] plain = Decryption.Getdecryption(CipherText.Text.ToString(), cipherKey);
                StringBuilder strB   = new StringBuilder();
                StringBuilder s      = new StringBuilder();
                byte[]        result = new byte[plain.GetLength(0) * 16];

                for (int i = 0; i < plain.GetLength(0); i++)
                {
                    for (int j = 0; j < 16; j++)
                    {
                        for (int h = 7; h >= 0; h--)
                        {
                            s.Append(plain[i, j] >> h & 1);
                        }
                    }
                }
                for (int i = 0; i < result.Length; i++)
                {
                    result[i] = Convert.ToByte(s.ToString(i * 8, 8), 2);
                }
                strB.Append(Encoding.Default.GetString(result));
                PlainText.Text = strB.ToString();
                sw4.Stop();
            }
            TimeSpan ts4 = sw4.Elapsed;

            label2.Text = ts4.ToString();
            //MessageBox.Show("解密用时" + ts4, "计时结果");
        }