.NET AES/Rijndael —重用解密器时解密不一致
我创建了一个使用 AES 进行加密和解密的类。
public class AesEncryptionProvider {
#region Fields
// Encryption key
private static readonly byte[] s_key = new byte[32] {
// Omitted...
};
// Initialization vector
private static readonly byte[] s_iv = new byte[16] {
// Omitted...
};
private AesCryptoServiceProvider m_provider;
private ICryptoTransform m_encryptor;
private ICryptoTransform m_decryptor;
#endregion
#region Constructors
private AesEncryptionProvider () {
m_provider = new AesCryptoServiceProvider();
m_encryptor = m_provider.CreateEncryptor(s_key, s_iv);
m_decryptor = m_provider.CreateDecryptor(s_key, s_iv);
}
static AesEncryptionProvider () {
Instance = new AesEncryptionProvider();
}
#endregion
#region Properties
public static AesEncryptionProvider Instance { get; private set; }
#endregion
#region Methods
public string Encrypt (string value) {
if (string.IsNullOrEmpty(value)) {
throw new ArgumentException("Value required.");
}
return Convert.ToBase64String(
Transform(
Encoding.UTF8.GetBytes(value),
m_encryptor));
}
public string Decrypt (string value) {
if (string.IsNullOrEmpty(value)) {
throw new ArgumentException("Value required.");
}
return Encoding.UTF8.GetString(
Transform(
Convert.FromBase64String(value),
m_decryptor));
}
#endregion
#region Private methods
private byte[] Transform (byte[] input, ICryptoTransform transform) {
byte[] output;
using (MemoryStream memory = new MemoryStream()) {
using (CryptoStream crypto = new CryptoStream(
memory,
transform,
CryptoStreamMode.Write
)) {
crypto.Write(input, 0, input.Length);
crypto.FlushFinalBlock();
output = memory.ToArray();
}
}
return output;
}
#endregion
}
正如您所看到的,在这两种情况下,我都通过CryptoStream
写入到MemoryStream
。如果我在每次调用 Decrypt
时通过 m_provider.CreateDecyptor(s_key, s_iv)
创建一个新的解密器,它就可以正常工作。
这里出了什么问题?为什么解密器表现得好像忘记了 IV?对 StreamReader.ReadToEnd()
的调用是否正在执行某些操作来帮助 m_decryptor
正常运行?
我想避免我在这里列出的两种“有效”方法中的任何一种,因为这两种方法都会影响性能,而这是一条非常关键的路径。提前致谢。
I've created a class for encrypting and decrypting using AES.
public class AesEncryptionProvider {
#region Fields
// Encryption key
private static readonly byte[] s_key = new byte[32] {
// Omitted...
};
// Initialization vector
private static readonly byte[] s_iv = new byte[16] {
// Omitted...
};
private AesCryptoServiceProvider m_provider;
private ICryptoTransform m_encryptor;
private ICryptoTransform m_decryptor;
#endregion
#region Constructors
private AesEncryptionProvider () {
m_provider = new AesCryptoServiceProvider();
m_encryptor = m_provider.CreateEncryptor(s_key, s_iv);
m_decryptor = m_provider.CreateDecryptor(s_key, s_iv);
}
static AesEncryptionProvider () {
Instance = new AesEncryptionProvider();
}
#endregion
#region Properties
public static AesEncryptionProvider Instance { get; private set; }
#endregion
#region Methods
public string Encrypt (string value) {
if (string.IsNullOrEmpty(value)) {
throw new ArgumentException("Value required.");
}
return Convert.ToBase64String(
Transform(
Encoding.UTF8.GetBytes(value),
m_encryptor));
}
public string Decrypt (string value) {
if (string.IsNullOrEmpty(value)) {
throw new ArgumentException("Value required.");
}
return Encoding.UTF8.GetString(
Transform(
Convert.FromBase64String(value),
m_decryptor));
}
#endregion
#region Private methods
private byte[] Transform (byte[] input, ICryptoTransform transform) {
byte[] output;
using (MemoryStream memory = new MemoryStream()) {
using (CryptoStream crypto = new CryptoStream(
memory,
transform,
CryptoStreamMode.Write
)) {
crypto.Write(input, 0, input.Length);
crypto.FlushFinalBlock();
output = memory.ToArray();
}
}
return output;
}
#endregion
}
As you can see, in both cases I'm writing to a MemoryStream
via a CryptoStream
. If I create a new decryptor via m_provider.CreateDecyptor(s_key, s_iv)
on every call to Decrypt
it works just fine.
What has gone wrong here? Why is the decryptor behaving as if its forgotten the IV? Is there something that the call to StreamReader.ReadToEnd()
is doing that helps m_decryptor
function correctly?
I would like to avoid either of the two "working" approaches I listed here as there is a performance hit on both and this is a very critical path. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我承认我不知道为什么会这样,但是将
AesCryptoServiceProvider
更改为AesManaged
即可。我还建议您的类实现 IDisposable,因为它包含三个实现它的成员变量。请参阅下面的代码更改:
Ok, I admit I have no idea why this works, but change
AesCryptoServiceProvider
toAesManaged
and voila.I also recommend making your class implement
IDisposable
as it contains three member variables which implement it. See below for code changes: