C# 中的 Rijndael 解密

发布于 2024-09-10 17:54:38 字数 1144 浏览 3 评论 0原文

我需要使用 Rijndael 和这些值解密字符串:

密钥大小 - 192

块大小 - 128

密钥 - cmdAj45F37I5ud2134FDg2fF

当我使用下面的代码时,出现错误:字符串大小不正确,任何人都可以帮助我吗?

public static string DecryptRijndael(string value, string encryptionKey)
    {

            var key = Encoding.UTF8.GetBytes(encryptionKey); //must be 16 chars 
            var rijndael = new RijndaelManaged
            {
                BlockSize = 128,
                IV = key,
                KeySize = 192,
                Key = key
            };

            var buffer = Convert.FromBase64String(value);
            var transform = rijndael.CreateDecryptor();
            string decrypted;
            using (var ms = new MemoryStream())
            {
                using (var cs = new CryptoStream(ms, transform, CryptoStreamMode.Write))
                {
                    cs.Write(buffer, 0, buffer.Length);
                    cs.FlushFinalBlock();
                    decrypted = Encoding.UTF8.GetString(ms.ToArray());
                    cs.Close();
                }
                ms.Close();
            }

            return decrypted;

    }

I need to decrypt a string using Rijndael and those values:

key size - 192

block size - 128

key - cmdAj45F37I5ud2134FDg2fF

When I'm using the code below I get an error : string size illigle, can anyone help me?

public static string DecryptRijndael(string value, string encryptionKey)
    {

            var key = Encoding.UTF8.GetBytes(encryptionKey); //must be 16 chars 
            var rijndael = new RijndaelManaged
            {
                BlockSize = 128,
                IV = key,
                KeySize = 192,
                Key = key
            };

            var buffer = Convert.FromBase64String(value);
            var transform = rijndael.CreateDecryptor();
            string decrypted;
            using (var ms = new MemoryStream())
            {
                using (var cs = new CryptoStream(ms, transform, CryptoStreamMode.Write))
                {
                    cs.Write(buffer, 0, buffer.Length);
                    cs.FlushFinalBlock();
                    decrypted = Encoding.UTF8.GetString(ms.ToArray());
                    cs.Close();
                }
                ms.Close();
            }

            return decrypted;

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

唠甜嗑 2024-09-17 17:54:38

一个(大)问题是使用 UTF8.GetBytes() 从字符串中获取 byte[]。字节数很难控制,也不是很安全。

使用Rfc2898DeriveBytes.GetBytes()< /code>相反。然后您可以指定所需的长度。

但当然,您在加密时也必须这样做。
我同意卢克关于 IV 的评论

One (big) problem is in using UTF8.GetBytes() to get the byte[] from string. It is hard to control the number of bytes and it is not very safe.

Use Rfc2898DeriveBytes.GetBytes() instead. And then you can specify the desired length.

But of course you have to do that while encrypting as well.
And I agrre with Luke's remarks about the IV

世界如花海般美丽 2024-09-17 17:54:38

您能看到代码中的注释吗?“必须是 16 个字符”?对我来说,你的密钥看起来更像是 24 个字符!

在这种情况下,您将重新使用密钥作为 IV(无论如何都不是推荐的最佳实践),但 IV 的大小必须与块大小匹配,块大小设置为 128 位/16 字节。

话虽如此,我刚才描述的问题应该会给您错误“指定的初始化向量 (IV) 与该算法的块大小不匹配”,而不是“字符串大小不匹配” em>,所以这可能是一个转移注意力的事情。

Can you see the comment in your code that says the key "must be 16 chars"? Your key looks more like 24 characters to me!

In this case you're re-using the key as the IV -- not recommended best practice anyway -- but the size of the IV must match the block size, which is set to 128 bits/16 bytes.

Having said that, the problem I just described should give you the error "Specified initialization vector (IV) does not match the block size for this algorithm", not "string size illigle", so this might be a red herring.

滥情哥ㄟ 2024-09-17 17:54:38

错误是因为输入是 64 位编码的。

IV和key不一样。 IV用于腌制。无论如何,您收到的错误是因为输入是 64 位编码的。所以这样做,错误就会消失。

var解码加密密钥= Base64Decode(加密密钥);

var key = Encoding.UTF8.GetBytes(decodedEncryptionKey);

这是完整的代码:

 private string decyptInit(string toBeDecrypted, string key, string initVector)
    {
        var keyByte = Encoding.Default.GetBytes(key);
        var decodedIV = Base64Decode(initVector);
        var iv = Encoding.Default.GetBytes(decodedIV);

        var rijndael = new RijndaelManaged
        {
            BlockSize = 128,
            IV = iv,
            KeySize = 192,
            Key = keyByte
        };

        var buffer = Convert.FromBase64String(toBeDecrypted);
        var transform = rijndael.CreateDecryptor();
        string decrypted;
        using (var ms = new MemoryStream())
        {
            using (var cs = new CryptoStream(ms, transform, CryptoStreamMode.Write))
            {
                cs.Write(buffer, 0, buffer.Length);
                cs.FlushFinalBlock();
                decrypted = Encoding.UTF8.GetString(ms.ToArray());
                cs.Close();
            }
            ms.Close();
        }

        return decrypted;
    } public static string Base64Decode(string base64EncodedData)
    {
        var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
        return System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
    }

Error is because of the input being 64 bit encoded.

IV and key is not the same. IV is for salting. Anyway the error you are getting is because the input is 64bit encoded. so do this and the error will go.

var decodedEncryptionKey= Base64Decode(encryptionKey);

var key = Encoding.UTF8.GetBytes(decodedEncryptionKey);

here is the full code:

 private string decyptInit(string toBeDecrypted, string key, string initVector)
    {
        var keyByte = Encoding.Default.GetBytes(key);
        var decodedIV = Base64Decode(initVector);
        var iv = Encoding.Default.GetBytes(decodedIV);

        var rijndael = new RijndaelManaged
        {
            BlockSize = 128,
            IV = iv,
            KeySize = 192,
            Key = keyByte
        };

        var buffer = Convert.FromBase64String(toBeDecrypted);
        var transform = rijndael.CreateDecryptor();
        string decrypted;
        using (var ms = new MemoryStream())
        {
            using (var cs = new CryptoStream(ms, transform, CryptoStreamMode.Write))
            {
                cs.Write(buffer, 0, buffer.Length);
                cs.FlushFinalBlock();
                decrypted = Encoding.UTF8.GetString(ms.ToArray());
                cs.Close();
            }
            ms.Close();
        }

        return decrypted;
    } public static string Base64Decode(string base64EncodedData)
    {
        var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
        return System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文