Ejemplo n.º 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);
                }
            }

            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];
                }
                plaintext = AddRoundKey(plaintext, cipherkey);
                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]);
                    }
                    plaintext = InvShiftRows(plaintext);
                    plaintext = MixColumn.InvMixColumn(plaintext);
                    plaintext = AddRoundKey(plaintext, cipherkey);
                }
                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]);
                }
                plaintext = InvShiftRows(plaintext);
                plaintext = AddRoundKey(plaintext, cipherkey);
                for (int j = 0; j < 16; j++)
                {
                    plian[i, j] = plaintext[j];
                }
            }
            return(plian);
        }
Ejemplo n.º 2
0
        public static string Decrypt(string Source, string Key, byte KeyLengthChoiced = 16)
        {
            if (String.IsNullOrEmpty(Source))
            {
                throw new Exception("没有输入明文!");
            }

            int KeyByteLength = Encoding.Default.GetBytes(Key).Length;

            switch (KeyLengthChoiced)
            {
            case 16:
            {
                if (KeyByteLength != 16)
                {
                    throw new Exception("密钥个数必须为16个字符或8个汉字!");
                }
                break;
            }

            case 24:
            {
                if (KeyByteLength != 24)
                {
                    throw new Exception("密钥个数必须为24个字符或12个汉字!");
                }
                break;
            }

            case 32:
            {
                if (KeyByteLength != 32)
                {
                    throw new Exception("密钥个数必须为32个字符或16个汉字!");
                }
                break;
            }

            default:
            {
                throw new Exception("所选密钥个数错误,请选择16、24或32位密匙");
            }
            }

            try
            {
                //用于检测,"密文内容有误!(密文内容应为16进制)"
                int temp;
                for (int i = 0; i < Source.Length; i++)
                {
                    temp = Convert.ToInt32(Source[i].ToString(), 16);
                }
            }
            catch
            {
                throw new Exception("密文内容有误!(密文内容应为16进制)");
            }

            int[,] key       = KeyExpansion.GetKeyExpansion(Key);
            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(Source, 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.UTF8.GetString(result));
            return(strB.ToString());
        }
Ejemplo n.º 3
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.UTF8.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);
                }
            }
            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];
                }
                plaintext = AddRoundKey(plaintext, cipherkey);
                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]);
                    }
                    plaintext = ShiftRows(plaintext);
                    plaintext = MixColumn.GetMixColumn(plaintext);
                    plaintext = AddRoundKey(plaintext, cipherkey);
                }
                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]);
                }
                plaintext = ShiftRows(plaintext);
                plaintext = AddRoundKey(plaintext, cipherkey);
                for (int j = 0; j < 16; j++)
                {
                    ciphertext[i, j] = plaintext[j];
                }
            }
            return(ciphertext);
        }