AES (Rijndael) 加密 128 位密钥 ecb

本文关键字:密钥 ecb 加密 Rijndael AES | 更新日期: 2023-09-27 18:34:33

我正在尝试为代表 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;
    }

我收到一个错误:"要解密的数据长度无效。

我将高度赞赏任何建议。

AES (Rijndael) 加密 128 位密钥 ecb

查看您的评论后,我找到了解决方案:

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的帮助