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");
        }
Exemple #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");
            }
        }
        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, "计时结果");
        }