public void SetText(string str, int[] cipher)
        {
            StringBuilder s = new StringBuilder();

            byte[] buf = new byte[2];

            strB.Append("\n");
            strB.Append(str);
            for (int i = 0; i < cipher.Length; i++)
            {
                s      = SubBytes.GetStr(cipher[i]);
                buf[0] = Convert.ToByte(s.ToString(0, 4), 2);
                buf[1] = Convert.ToByte(s.ToString(4, 4), 2);
                strB.Append(alphabet[buf[0]] + "" + alphabet[buf[1]]);
            }
            strB.Append("\r\n");
        }
Beispiel #2
0
        public void SetText(int[,] keyExpansion)
        {
            StringBuilder s = new StringBuilder(8);

            byte[] buf = new byte[2];

            for (int i = 0; i < keyExpansion.GetLength(0); i++)
            {
                for (int j = 0; j < keyExpansion.GetLength(1); j++)
                {
                    s      = SubBytes.GetStr(keyExpansion[i, j]);
                    buf[0] = Convert.ToByte(s.ToString(0, 4), 2);
                    buf[1] = Convert.ToByte(s.ToString(4, 4), 2);
                    str.Append(alphabet[buf[0]] + "" + alphabet[buf[1]]);
                }
                str.Append("\n");
            }
        }
Beispiel #3
0
        public static int[,] GetKeyExpansion(string str)
        {
            str = str.ToLower();
            byte[]        buf = Encoding.Default.GetBytes(str);
            StringBuilder s   = new StringBuilder(128);

            int nb = 4;
            int nk = buf.Length / 4;
            int nr = 0;

            switch (nk)
            {
            case 4: nr = 10;
                break;

            case 6: nr = 12;
                break;

            case 8: nr = 14;
                break;
            }
            int[,] key       = new int[nb * (nr + 1), nb];
            int[,] cipherKey = new int[nr + 1, 16];
            int[] temp = new int[4];

            for (int i = 0; i < buf.Length; i++)
            {
                for (int j = 7; j >= 0; j--)
                {
                    s = s.Append(buf[i] >> j & 1);
                }
            }
            for (int i = 0; i < nk; i++)
            {
                for (int j = 0; j < nb; j++)
                {
                    key[i, j] = Convert.ToInt32(s.ToString(8 * j + i * 32, 8), 2);
                }
            }
            for (int i = nk; i < nb * (nr + 1); i++)
            {
                temp[0] = key[i - 1, 0];
                temp[1] = key[i - 1, 1];
                temp[2] = key[i - 1, 2];
                temp[3] = key[i - 1, 3];
                for (int j = 0; j < nb; j++)
                {
                    if (i % nk == 0)
                    {
                        temp[j] = SubBytes.GetSubByte(key[i - 1, (j + 1) % 4]) ^ Rcon[i / nk, j];
                    }
                    else if ((nk > 6) && (i % nk == 4))
                    {
                        temp[j] = SubBytes.GetSubByte(key[i - 1, j]);
                    }
                    key[i, j] = temp[j] ^ key[i - nk, j];
                }
            }
            for (int i = 0; i < nr + 1; i++)
            {
                for (int j = 0; j < 16; j++)
                {
                    cipherKey[i, j] = key[j / 4 + i * 4, j % 4];
                }
            }
            return(cipherKey);
        }
Beispiel #4
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);
        }
Beispiel #5
0
        public static int[,] Getencryption(string str, int[,] key)
        {
            str = str.ToLower();
            StringBuilder strB = new StringBuilder(str);

            if (strB.Length % 16 != 0)
            {
                int len = strB.Length;
                for (int i = 0; i < 32 - len % 32; i++)
                {
                    strB = strB.Append(" ");
                }
            }
            byte[] buf = Encoding.Default.GetBytes(strB.ToString());

            int[] cipherkey = new int[16];
            int[,] plain      = new int[buf.Length / 16, 16];
            int[,] ciphertext = new int[buf.Length / 16, 16];
            int[] plaintext = new int[16];

            StringBuilder s = new StringBuilder(100000);

            for (int i = 0; i < buf.Length; i++)
            {
                for (int j = 7; j >= 0; j--)
                {
                    s = s.Append(buf[i] >> j & 1);
                }
            }
            for (int i = 0; i < plain.GetLength(0); i++)
            {
                for (int j = 0; j < 16; j++)
                {
                    plain[i, j] = Convert.ToInt32(s.ToString(j * 8 + i * 128, 8), 2);
                }
            }
            EncryptionForm encryForm = new EncryptionForm();

            encryForm.Show();
            for (int i = 0; i < plain.GetLength(0); i++)
            {
                for (int h = 0; h < 16; h++)
                {
                    cipherkey[h] = key[0, h];
                    plaintext[h] = plain[i, h];
                }
                encryForm.SetText("密钥:", cipherkey);
                plaintext = AddRoundKey(plaintext, cipherkey);
                encryForm.SetText("AddRoundKey:", plaintext);
                encryForm.Set();
                for (int j = 0; j < key.GetLength(0) - 2; j++)
                {
                    for (int h = 0; h < 16; h++)
                    {
                        cipherkey[h] = key[j + 1, h];
                    }
                    for (int h = 0; h < plaintext.Length; h++)
                    {
                        plaintext[h] = SubBytes.GetSubByte(plaintext[h]);
                    }
                    encryForm.SetText("SubBytes:", plaintext);
                    plaintext = ShiftRows(plaintext);
                    encryForm.SetText("ShiftRows:", plaintext);
                    plaintext = MixColumn.GetMixColumn(plaintext);
                    encryForm.SetText("MixColumn:", plaintext);
                    encryForm.SetText("轮密钥:", cipherkey);
                    plaintext = AddRoundKey(plaintext, cipherkey);
                    encryForm.SetText("AddRoundKey:", plaintext);
                    encryForm.Set();
                }
                for (int h = 0; h < 16; h++)
                {
                    cipherkey[h] = key[key.GetLength(0) - 1, h];
                }
                for (int h = 0; h < plaintext.Length; h++)
                {
                    plaintext[h] = SubBytes.GetSubByte(plaintext[h]);
                }
                encryForm.SetText("SubBytes:", plaintext);
                plaintext = ShiftRows(plaintext);
                encryForm.SetText("ShiftRows:", plaintext);
                encryForm.SetText("轮密钥:", cipherkey);
                plaintext = AddRoundKey(plaintext, cipherkey);
                encryForm.SetText("AddRoundKey:", plaintext);
                for (int j = 0; j < 16; j++)
                {
                    ciphertext[i, j] = plaintext[j];
                }
            }
            encryForm.Settext();
            return(ciphertext);
        }
        private void EncryptionBtn_Click_1(object sender, EventArgs e)
        {
            if (radioBtn1.Checked == true)
            {
                if (Encoding.Default.GetBytes(KeyText.Text).Length != 16)
                {
                    MessageBox.Show("ERROR!密钥个数必须为16个字符或8个汉字!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            if (radioBtn2.Checked == true)
            {
                if (Encoding.Default.GetBytes(KeyText.Text).Length != 24)
                {
                    MessageBox.Show("ERROR!密钥个数必须为24个字符或12个汉字!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            if (radioBtn3.Checked == true)
            {
                if (Encoding.Default.GetBytes(KeyText.Text).Length != 32)
                {
                    MessageBox.Show("ERROR!密钥个数必须为32个字符或16个汉字!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            else
            {
                if (KeyText.Text.Length == 0)
                {
                    MessageBox.Show("请输入密钥!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                if (PlainText.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[,] cipher = Encryption.Getencryption(PlainText.Text.ToString(), key);

                StringBuilder strB = new StringBuilder();
                for (int i = 0; i < cipher.GetLength(0); i++)
                {
                    for (int j = 0; j < 16; j++)
                    {
                        StringBuilder s   = new StringBuilder();
                        byte[]        buf = new byte[2];
                        s      = SubBytes.GetStr(cipher[i, j]);
                        buf[0] = Convert.ToByte(s.ToString(0, 4), 2);
                        buf[1] = Convert.ToByte(s.ToString(4, 4), 2);
                        strB.Append(alphabet[buf[0]] + "" + alphabet[buf[1]]);
                    }
                }
                CipherText.Text = strB.ToString();
                sw4.Stop();
            }

            TimeSpan ts3 = sw4.Elapsed;

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