我在C#
中遇到AES Rijndael管理加密问题我有一个大约110,000个图像文件名的列表,我想单独加密。我编写了一个算法,一起加密它们,然后是一个解密单个图像的方法。
加密方法如下:
var saltBytes = Encoding.UTF8.GetBytes(salt);
// The key size for encrypting the images
int KEY_SIZE = 256;
// The initialization vector for encrypting the images
byte[] _initialisationVectorBytes = Encoding.ASCII.GetBytes("EAzjVfNrCzOoE7AI");
//Derives the key from the phrase and the salt
using(var password = new Rfc2898DeriveBytes(password, saltBytes))
{
//use the AES Rijndael algorithm
using(var symmetricKey = new RijndaelManaged())
{
//set the mode
symmetricKey.Mode = CipherMode.CBC;
//create an encryption object
using(var encryptor = symmetricKey.CreateEncryptor(password.GetBytes(KEY_SIZE / 8), _initialisationVectorBytes))
{
//loop through the images
Parallel.ForEach(_sourceImages, image =>
{
//set the plain text bytes and the salt bytes
var plainTextBytes = Encoding.UTF8.GetBytes(image.ImageID.ToString());
var cipherText = string.Empty;
if(_encryptImageFilenames)
{
//create a memory stream
using(var cryptoMemoryStream = new MemoryStream())
{
//create a cryptographic stream
using(var cryptoStream = new CryptoStream(cryptoMemoryStream, encryptor, CryptoStreamMode.Write))
{
//encrypt the image id
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
cryptoStream.FlushFinalBlock();
cipherText = Convert.ToBase64String(cryptoMemoryStream.ToArray());
}
}
}
else
cipherText = image.ImageID.ToString();
}
}
}
}
据我所知,这很有效。但是,当我运行以下解密方法时,绝大多数图像都正确解密,但是有些图像使用奇怪的字符进行了不正确的解密,有些图像提供了“填充无效且无法删除”错误。
// The size of the key
const int KeySize = 256;
// Use the Rijndael Managed algorithm
RijndaelManaged SymmetricKey = new RijndaelManaged();
// The initialisation vector
byte[] initialisationVectorBytes = Encoding.ASCII.GetBytes("EAzjVfNrCzOoE7AI");
try
{
//get the cipher text bytes and the salt bytes
var cipherTextBytes = Convert.FromBase64String(cipherText);
//Derives the key from the phrase and the salt
using(var password = new Rfc2898DeriveBytes(passPhrase, Encoding.UTF8.GetBytes(salt)))
{
//set the mode
SymmetricKey.Mode = CipherMode.CBC;
//create an decryption object
using(var decryptor = SymmetricKey.CreateDecryptor(password.GetBytes(KeySize / 8), initialisationVectorBytes))
{
//create a memory stream
using(var memoryStream = new MemoryStream(cipherTextBytes))
{
//create a crypto stream
using(var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
{
//decrypt the string
var plainTextBytes = new byte[cipherTextBytes.Length];
var decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
}
}
}
}
}
catch(Exception ex)
{
return string.Empty;
}
因此,对于110000张图像,我获得了大约20-30张图像的成功解密,失败是之前两个错误中的一个。
我不能为我的生活看到为什么如果加密方法中出现错误肯定会影响所有已加密的图像?
一些例子:
明文= 128784
Ciphertext = J2W / 5Y1 / nvnrpvaWIZhl6g ==
Decrypted = 128784
明文= 122875
Ciphertext = N + heUx57427Lk8 / Ew10rsA ==
解密= \ u0001 \ u0017275
明文= 121693
Ciphertext = Zf70jcYCbHQqhd23NqD6yA ==
Decrypted = 121693
明文= 133456
Ciphertext = wzBgoDaTnGBEyQokI + l6Uw ==
Decrypted = “填充无效,无法删除”
这些示例中使用的密码和盐是:
密码 =“!XbLg`p / 0.9nyF ?; jGf @ nA; e19'TtVk?Ik.l * 2 = zy.9MYH:7Jmj [| * 0N!”
盐 =“alkfdslkaj:H6D£ rWe!N,K;?sdoidnalks34334234 &(*£';”