本文介绍了错误RijndaelManaged的,"填充是无效的不能删除"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经从错误的CryptoStream
:
code
公开的MemoryStream EncrypteBytes(流的InputStream,串的密码,串saltValue)
{
RijndaelManaged的RijndaelCipher =新RijndaelManaged的();
RijndaelCipher.Padding = PaddingMode.PKCS7;
RijndaelCipher.Mode = CipherMode.CBC;
byte []的盐= Encoding.ASCII.GetBytes(saltValue);
PasswordDeriveBytes密码=新PasswordDeriveBytes(口令,盐,SHA-1,2);
ICryptoTransform的加密器= RijndaelCipher.CreateEncryptor(password.GetBytes(32),password.GetBytes(16));
MemoryStream的MemoryStream的=新的MemoryStream();
CryptoStream的CryptoStream的=新的CryptoStream(MemoryStream的,加密机,CryptoStreamMode.Write);
VAR缓冲区=新的字节[1024];
变种读取= inputStream.Read(缓冲液,0,buffer.Length);
而(读> 0)
{
cryptoStream.Write(缓冲,0,读);
读= inputStream.Read(缓冲,0,buffer.Length);
}
cryptoStream.FlushFinalBlock();
memoryStream.Position = 0;
返回MemoryStream的;
}
//用法示例:DecryptBytes(encryptedBytes,SensitivePhrase,SodiumChloride);
公共字节[] DecrypteBytes(MemoryStream的MemoryStream的,字符串的密码,串saltValue)
{
RijndaelManaged的RijndaelCipher =新RijndaelManaged的();
RijndaelCipher.Padding = PaddingMode.PKCS7;
RijndaelCipher.Mode = CipherMode.CBC;
byte []的盐= Encoding.ASCII.GetBytes(saltValue);
PasswordDeriveBytes密码=新PasswordDeriveBytes(口令,盐,SHA-1,2);
ICryptoTransform的解密= RijndaelCipher.CreateDecryptor(password.GetBytes(32),password.GetBytes(16));
CryptoStream的CryptoStream的=新的CryptoStream(MemoryStream的,解密,CryptoStreamMode.Read);
byte []的plainBytes =新的字节[memoryStream.Length]
INT DecryptedCount = cryptoStream.Read(plainBytes,0,plainBytes.Length);
返回plainBytes;
}
解决方案
使用 PaddingMode.Zeros
来解决问题,按照code:
公共字节[] EncryptFile(流输入,字符串密码,串盐)
{
//从本质上讲,如果你想使用RijndaelManaged的如AES,你需要确保:
// 1,块大小被设定为128位
// 2。你不使用CFB模式,或者如果你是反馈的大小也是128位
变种算法=新RijndaelManaged的{密钥长度= 256,块大小= 128};
VAR键=新Rfc2898DeriveBytes(密码,Encoding.ASCII.GetBytes(盐));
algorithm.Key = key.GetBytes(algorithm.KeySize / 8);
algorithm.IV = key.GetBytes(algorithm.BlockSize / 8);
algorithm.Padding = PaddingMode.Zeros;
使用(流的CryptoStream =新的MemoryStream())
使用(VAR encryptedStream =新的CryptoStream(CryptoStream的,algorithm.CreateEncryptor(),CryptoStreamMode.Write))
{
CopyStream(输入,encryptedStream);
返回ReadToEnd(CryptoStream的);
}
}
公共字节[] DecryptFile(流输入,流输出,串密码,串盐)
{
//从本质上讲,如果你想使用RijndaelManaged的如AES,你需要确保:
// 1,块大小被设定为128位
// 2。你不使用CFB模式,或者如果你是反馈的大小也是128位
变种算法=新RijndaelManaged的{密钥长度= 256,块大小= 128};
VAR键=新Rfc2898DeriveBytes(密码,Encoding.ASCII.GetBytes(盐));
algorithm.Key = key.GetBytes(algorithm.KeySize / 8);
algorithm.IV = key.GetBytes(algorithm.BlockSize / 8);
algorithm.Padding = PaddingMode.Zeros;
尝试
{
使用(VAR decryptedStream =新的CryptoStream(输出,algorithm.CreateDecryptor(),CryptoStreamMode.Write))
{
CopyStream(输入,decryptedStream);
返回ReadToEnd(输出);
}
}
赶上(CryptographicException前)
{
抛出新InvalidDataException(请提供正确的密码);
}
赶上(例外前)
{
抛出新的异常(ex.Message);
}
}
我跳帮你。
I have error from CryptoStream
:
Code
public MemoryStream EncrypteBytes(Stream inputStream, string passPhrase, string saltValue)
{
RijndaelManaged RijndaelCipher = new RijndaelManaged();
RijndaelCipher.Padding = PaddingMode.PKCS7;
RijndaelCipher.Mode = CipherMode.CBC;
byte[] salt = Encoding.ASCII.GetBytes(saltValue);
PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, salt, "SHA1", 2);
ICryptoTransform Encryptor = RijndaelCipher.CreateEncryptor(password.GetBytes(32), password.GetBytes(16));
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream, Encryptor, CryptoStreamMode.Write);
var buffer = new byte[1024];
var read = inputStream.Read(buffer, 0, buffer.Length);
while (read > 0)
{
cryptoStream.Write(buffer, 0, read);
read = inputStream.Read(buffer, 0, buffer.Length);
}
cryptoStream.FlushFinalBlock();
memoryStream.Position = 0;
return memoryStream;
}
// Example usage: DecryptBytes(encryptedBytes, "SensitivePhrase", "SodiumChloride");
public byte[] DecrypteBytes(MemoryStream memoryStream, string passPhrase, string saltValue)
{
RijndaelManaged RijndaelCipher = new RijndaelManaged();
RijndaelCipher.Padding = PaddingMode.PKCS7;
RijndaelCipher.Mode = CipherMode.CBC;
byte[] salt = Encoding.ASCII.GetBytes(saltValue);
PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, salt, "SHA1", 2);
ICryptoTransform Decryptor = RijndaelCipher.CreateDecryptor(password.GetBytes(32), password.GetBytes(16));
CryptoStream cryptoStream = new CryptoStream(memoryStream, Decryptor, CryptoStreamMode.Read);
byte[] plainBytes = new byte[memoryStream.Length];
int DecryptedCount = cryptoStream.Read(plainBytes, 0, plainBytes.Length);
return plainBytes;
}
解决方案
Use PaddingMode.Zeros
to fix problem , Following code:
public byte[] EncryptFile(Stream input, string password, string salt)
{
// Essentially, if you want to use RijndaelManaged as AES you need to make sure that:
// 1.The block size is set to 128 bits
// 2.You are not using CFB mode, or if you are the feedback size is also 128 bits
var algorithm = new RijndaelManaged { KeySize = 256, BlockSize = 128 };
var key = new Rfc2898DeriveBytes(password, Encoding.ASCII.GetBytes(salt));
algorithm.Key = key.GetBytes(algorithm.KeySize / 8);
algorithm.IV = key.GetBytes(algorithm.BlockSize / 8);
algorithm.Padding = PaddingMode.Zeros;
using (Stream cryptoStream = new MemoryStream())
using (var encryptedStream = new CryptoStream(cryptoStream, algorithm.CreateEncryptor(), CryptoStreamMode.Write))
{
CopyStream(input, encryptedStream);
return ReadToEnd(cryptoStream);
}
}
public byte[] DecryptFile(Stream input, Stream output, string password, string salt)
{
// Essentially, if you want to use RijndaelManaged as AES you need to make sure that:
// 1.The block size is set to 128 bits
// 2.You are not using CFB mode, or if you are the feedback size is also 128 bits
var algorithm = new RijndaelManaged { KeySize = 256, BlockSize = 128 };
var key = new Rfc2898DeriveBytes(password, Encoding.ASCII.GetBytes(salt));
algorithm.Key = key.GetBytes(algorithm.KeySize / 8);
algorithm.IV = key.GetBytes(algorithm.BlockSize / 8);
algorithm.Padding = PaddingMode.Zeros;
try
{
using (var decryptedStream = new CryptoStream(output, algorithm.CreateDecryptor(), CryptoStreamMode.Write))
{
CopyStream(input, decryptedStream);
return ReadToEnd(output);
}
}
catch (CryptographicException ex)
{
throw new InvalidDataException("Please supply a correct password");
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
I hop help you.
这篇关于错误RijndaelManaged的,"填充是无效的不能删除"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!