AES(Rijndael)加密128位密钥ecb

时间:2016-02-15 07:43:04

标签: c# encryption cryptography aes rijndael

我正在尝试为表示16字节数据的32字节十六进制字符串创建解密函数这是我到目前为止所尝试的(我在这里找到了一个有效的JS示例:http://www.hanewin.net/encrypt/aes/aes-test.htm):

public static string DecryptString(string code)
    {

        string text = hex2s("0102030405060708090a0b0c0d0e0f11");
        string key = hex2s("ed7c0c82daf513b81c32f6655e8fd4ee");

        //Expected result: 260fe45846fb26dbb1d28d8166d4a89f

        return BitConverter.ToString(Encoding.Default.GetBytes(decrypt(text, key)));
    }


    private static string hex2s(string hex)
    {
        var r = "";
        if (hex.IndexOf("0x") == 0 || hex.IndexOf("0X") == 0) hex = hex.Substring(2);

        if (hex.Length % 2 > 0) hex += '0';

        for (var i = 0; i < hex.Length; i += 2)
        {
            int thisCharCode = hex[i];
            char newCharCode = (char)(thisCharCode);
            r = r + newCharCode;
        }
        return r;
    }

    public static String decrypt(String imput, String key)
    {
        byte[] data = Convert.FromBase64String(imput);
        String decrypted;

        using (RijndaelManaged rijAlg = new RijndaelManaged())
        {
            rijAlg.Key = Encoding.ASCII.GetBytes(key);
            rijAlg.Mode = CipherMode.ECB;
            rijAlg.BlockSize = 128;
            rijAlg.Padding = PaddingMode.Zeros;

            ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, null);
            using (MemoryStream msDecrypt = new MemoryStream(data))
            {
                using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                {
                    using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                    {
                        decrypted = srDecrypt.ReadToEnd();
                    }
                }
            }
        }
        return decrypted;
    }

我收到错误:“要解密的数据长度无效。”

我将非常感谢任何建议。

1 个答案:

答案 0 :(得分:0)

在审核您的评论后,我找到了解决方案:

public static string DecryptString(string code)
    {

        byte[] text = FromHex("0102030405060708090a0b0c0d0e0f11");
        byte[] key = FromHex("ed7c0c82daf513b81c32f6655e8fd4ee");

        //Expected result: 260fe45846fb26dbb1d28d8166d4a89f

        return BitConverter.ToString(Encoding.Default.GetBytes(decrypt(text, key)));
    }

    public static byte[] FromHex(string hex)
    {
        byte[] raw = new byte[hex.Length / 2];
        for (int i = 0; i < raw.Length; i++)
        {
            raw[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16);
        }
        return raw;
    } 


    public static String decrypt(byte[] input, byte[] key)
    {
        byte[] data = input;
        String decrypted;

        using (RijndaelManaged rijAlg = new RijndaelManaged())
        {
            rijAlg.Key = key;
            rijAlg.Mode = CipherMode.ECB;
            rijAlg.BlockSize = 128;
            rijAlg.Padding = PaddingMode.Zeros;

            ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, null);
            using (MemoryStream msDecrypt = new MemoryStream(data))
            {
                using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                {
                    using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                    {
                        decrypted = srDecrypt.ReadToEnd();
                    }
                }
            }
        }
        return decrypted;
    }

感谢您的帮助@Damien_The_Unbeliever和@Ian