AesManaged开始生成空字符串加密结果后多年的工作完美 [英] AesManaged started to produce null string encryption result after years of working perfectly

查看:137
本文介绍了AesManaged开始生成空字符串加密结果后多年的工作完美的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几年前,我写了一个简单的包装器基于 MSDN - AesManaged Class 代码,以掩盖注册表中保存的值(仅仅是为了防止手动篡改这些值,而不再是其他操作):

A few years ago I wrote a simple wrapper based on MSDN - AesManaged Class code, to obscure values saved in registry (simply to prevent manual tampering with these, nothing more):

public static string    Encrypt( string s, byte[] key, byte[] iv )
{
    byte[]  enc;

    using(  AesManaged aes =    new AesManaged( )  )
    {
        ICryptoTransform    ict =   aes.CreateEncryptor( key, iv );

        using(  MemoryStream ms= new MemoryStream( )  )
        using(  CryptoStream cs= new CryptoStream( ms, ict, CryptoStreamMode.Write )  )
        using(  StreamWriter sw= new StreamWriter( cs )  )
        {
            sw.Write( s );      enc =   ms.ToArray( );
        }
    }
    return  Convert.ToBase64String( enc );
}

public static string    Decrypt( string p, byte[] key, byte[] iv )
{
    string  s=  null;

    using(  AesManaged aes =    new AesManaged( )  )
    {
        ICryptoTransform    ict =   aes.CreateDecryptor( key, iv );

        using(  MemoryStream ms= new MemoryStream( Convert.FromBase64String( p ) )  )
        using(  CryptoStream cs= new CryptoStream( ms, ict, CryptoStreamMode.Read )  )
        using(  StreamReader sr= new StreamReader( cs )  )
        {
            s=  sr.ReadToEnd( );
        }
    }
    return  s;
}

这些方法在这段时间内都工作得很好。直到昨天,Encrypt null结果在一个有效的字符串。更改 iv 没有任何区别。试图在几台机器上执行 - 结果相同。不会抛出异常。但是,解密仍然可以正常工作!

These methods worked perfectly all this time .. until yesterday, when Encrypt produced a null result on a valid string. Changing key and iv does not make any difference. Tried executing on several machines - same result. No exceptions are thrown. However, decryption still works fine!

为什么 Encrypt()突然失败?

推荐答案

在找到并研究了几个类似的问题后(​​解密器提供空字符串; 在.NET中使用AES加密 - CryptographicException说填充无效,无法删除; 填充无效,无法使用AesManaged 删除; 填充无效,不能删除异常,而解密字符串使用AesManagedC#),并再次查看我的代码,我注意到与MSDN样本的区别中。事实上,我进行了优化 这是什么破坏了执行! 必须拼写如下:

After finding and studying several similar questions (Aes decryptor gives empty string; Using AES encryption in .NET - CryptographicException saying the padding is invalid and cannot be removed; "Padding is invalid and cannot be removed" using AesManaged; Padding is invalid and cannot be removed Exception while decrypting string using "AesManaged" C#) and looking at my code again, I noticed the difference with MSDN sample. Indeed, i made an optimization and that is, what broke the execution! The code must be spelled this way:

public static string    Encrypt( string s, byte[] key, byte[] iv )
{
    byte[]  enc;

    using(  AesManaged aes =    new AesManaged( )  )
    {
        ICryptoTransform    ict =   aes.CreateEncryptor( key, iv );

        using(  MemoryStream ms= new MemoryStream( )  )
        {
            using(  CryptoStream cs= new CryptoStream( ms, ict, CryptoStreamMode.Write ) )
            {
                using(  StreamWriter sw= new StreamWriter( cs )  )
                {
                    sw.Write( s );
                }
            }
            enc =   ms.ToArray( );
        }
    }

    return  Convert.ToBase64String( enc );
}

注意每个后使用..)!是的,这意味着在我尝试使用缓冲区之前,CryptoStream已关闭 - 并因此被刷新,使此方法安全。

Notice the presence of curly braces after each using(..)! Yes, that means the CryptoStream is closed - and therefore flushed - before i attempt to use the buffer, making this approach safe.

,为什么解决方案由 @GregS @HansPassant 没有做,但由于代码现在工作(恢复到原始版本:),我的问题是关闭。感谢上帝的版本控制! :))

No idea, why solution by @GregS and @HansPassant did not do it, but since the code works now (reverted to original version :), my issue is closed. Thank god for version control! :))

感谢大家指导我解决问题!

Thank you guys for guiding me to the solution!

这篇关于AesManaged开始生成空字符串加密结果后多年的工作完美的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆