经过多年的完美运行,AesManaged 开始产生空字符串加密结果
AesManaged started to produce null string encryption result after years of working perfectly
几年前,我基于 MSDN - AesManaged Class 代码编写了一个简单的包装器,以隐藏注册表中保存的值(只是为了防止手动篡改这些,仅此而已):
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 在有效字符串上产生了空结果。更改 key
和 iv
没有任何区别。尝试在多台机器上执行 - 结果相同。没有异常被抛出。然而,解密仍然正常!
为什么Encrypt( )
突然失效了?是否有一些 Windows 更新改变了游戏环境?
在找到并研究了几个类似的问题 (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#) 并再次查看我的代码后,我注意到了与 MSDN 示例的不同之处。事实上,我做了一个 优化 和 也就是说,什么 破坏了执行!代码必须这样拼写:
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 );
}
注意每个 using(..)
后面都有花括号!是的,这意味着 CryptoStream 已关闭 - 并因此被刷新 - 在我尝试使用缓冲区之前,使这种方法安全。
不知道,为什么 @GregS and @HansPassant 的解决方案没有这样做,但由于代码现在可以工作(恢复到原始版本:),我的问题已关闭。感谢上帝的版本控制! :))
谢谢你们指导我找到解决方案!
几年前,我基于 MSDN - AesManaged Class 代码编写了一个简单的包装器,以隐藏注册表中保存的值(只是为了防止手动篡改这些,仅此而已):
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 在有效字符串上产生了空结果。更改 key
和 iv
没有任何区别。尝试在多台机器上执行 - 结果相同。没有异常被抛出。然而,解密仍然正常!
为什么Encrypt( )
突然失效了?是否有一些 Windows 更新改变了游戏环境?
在找到并研究了几个类似的问题 (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#) 并再次查看我的代码后,我注意到了与 MSDN 示例的不同之处。事实上,我做了一个 优化 和 也就是说,什么 破坏了执行!代码必须这样拼写:
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 );
}
注意每个 using(..)
后面都有花括号!是的,这意味着 CryptoStream 已关闭 - 并因此被刷新 - 在我尝试使用缓冲区之前,使这种方法安全。
不知道,为什么 @GregS and @HansPassant 的解决方案没有这样做,但由于代码现在可以工作(恢复到原始版本:),我的问题已关闭。感谢上帝的版本控制! :))
谢谢你们指导我找到解决方案!