简体   繁体   English

如何解密 Rijndael 128 加密文本

[英]How to decrypt a Rijndael 128 encrypted text

I'm trying to decrypt a Rijndael-128 encrypted cipher, these are the values:我正在尝试解密 Rijndael-128 加密密码,这些是值:

Cipher: "QfJzZ9V6Jm43jYPiVaXP9mu+f88S/JC24saHbOMxxC8="
Key: "45744855535472525844494538555934",
Mode: CBC

Result should be: "abcd@1234"

This website seems to decrypt the cipher just fine: https://codebeautify.org/encrypt-decrypt这个网站似乎可以很好地解密密码: https : //codebeautify.org/encrypt-decrypt

I'm trying to do the same thing in C# with absolutely no luck, what am I missing here?我正在尝试在 C# 中做同样的事情,但绝对没有运气,我在这里错过了什么?

class Program
{
    static void Main(string[] args)
    {
        var text = Decrypt("QfJzZ9V6Jm43jYPiVaXP9mu+f88S/JC24saHbOMxxC8=", Convert.FromBase64String("45744855535472525844494538555934"));
    }

    public static string Decrypt(string Text, byte[] keyBytes)
    {
        var textBytes = Convert.FromBase64String(Text);

        var rijKey = new RijndaelManaged();

        rijKey.IV = textBytes.Take(rijKey.BlockSize / 8).ToArray(); 

        rijKey.Padding = PaddingMode.None;

        rijKey.Mode = CipherMode.CBC;

        var  decryptor = rijKey.CreateDecryptor(keyBytes, rijKey.IV);

        var memoryStream = new MemoryStream(textBytes);

        var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);

        var pTextBytes = new byte[textBytes.Length];

        var decryptedByteCount = cryptoStream.Read(pTextBytes, 0, pTextBytes.Length);

        memoryStream.Close();

        cryptoStream.Close();

        string plainText = Encoding.UTF8.GetString(pTextBytes, 0, decryptedByteCount);

        return plainText;
    }
}

The problem with your code is this line:您的代码的问题是这一行:

rijKey.GenerateIV();

You need the original IV.你需要原来的IV。 You can't just use a random one.你不能只使用一个随机的。

If you go to the site you linked to, each time you press encrypt with the key and text you have given, you get a different encrypted text (because a random IV is used).如果您访问链接到的站点,每次使用您提供的密钥和文本按 encrypt 时,您都会得到不同的加密文本(因为使用了随机 IV)。 The web page must be prepending the random IV used to encrypt to the encrypted text (or less likely, the web page is storing it), which is why it can then decrypt the encrypted text.网页必须预先准备用于加密加密文本的随机 IV(或者不太可能,网页正在存储它),这就是它可以解密加密文本的原因。

[Your code also needs using statements.] [您的代码也需要using语句。]

Is it possible to NOT use the IV when implementing Rijndael decryption? 实现 Rijndael 解密时是否可以不使用 IV?

How to Use Rijndael ManagedEncryption with C# 如何在 C# 中使用 Rijndael ManagedEncryption

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

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