简体   繁体   English

C#中的Rijndael解密

[英]Rijndael decryption in C#

I need to decrypt a string using Rijndael and those values: 我需要使用Rijndael和这些值解密字符串:

key size - 192 密钥大小-192

block size - 128 块大小-128

key - cmdAj45F37I5ud2134FDg2fF 键-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;

    }

One (big) problem is in using UTF8.GetBytes() to get the byte[] from string. 一个(大)问题是使用UTF8.GetBytes()从字符串获取byte []。 It is hard to control the number of bytes and it is not very safe. 很难控制字节数,也不是很安全。

Use Rfc2898DeriveBytes.GetBytes() instead. 请改用Rfc2898DeriveBytes.GetBytes() 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 我同意卢克关于四世的言论

Can you see the comment in your code that says the key "must be 16 chars" ? 您可以在代码中看到注释,指出“必须为16个字符”键吗? Your key looks more like 24 characters to me! 您的密钥对我来说更像是24个字符!

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. 在这种情况下,您将密钥重新用作IV-仍然不建议采用最佳做法-但IV的大小必须与块大小匹配,该块大小设置为128位/ 16字节。

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. 话虽如此,我刚刚描述的问题应该给您错误“指定的初始化向量(IV)与该算法的块大小不匹配” ,而不是“字符串大小错误” ,因此这可能是一个红色鲱鱼。

Error is because of the input being 64 bit encoded. 错误是因为输入是64位编码的。

IV and key is not the same. IV和键不一样。 IV is for salting. IV用于腌制。 Anyway the error you are getting is because the input is 64bit encoded. 无论如何,您得到的错误是因为输入是64位编码的。 so do this and the error will go. 这样做,错误就会消失。

var decodedEncryptionKey= Base64Decode(encryptionKey); var encodedEncryptionKey = Base64Decode(encryptionKey);

var key = Encoding.UTF8.GetBytes(decodedEncryptionKey); 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);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM