输入数据不是完整的块。用AES [英] The input data is not a complete block. with AES
本文介绍了输入数据不是完整的块。用AES的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我使用AES算法解密时,我得到输入数据不是一个完整的块。
错误。
以下是代码:
When i am decrypting using AES algorithm i am getting the The input data is not a complete block.
error.
Following is the code:
base64StringToDecrypt="dHQ4eeglPQ";
public static string DecryptString(string base64StringToDecrypt, string passphrase)
{
//Set up the encryption objects
using (AesCryptoServiceProvider acsp = GetProvider(Encoding.Default.GetBytes(passphrase)))
{
acsp.Mode = CipherMode.CFB;
base64StringToDecrypt = base64StringToDecrypt.Trim().Replace(" ", "+");
if (base64StringToDecrypt.Length % 4 > 0)
base64StringToDecrypt = base64StringToDecrypt.PadRight(base64StringToDecrypt.Length + 4 - base64StringToDecrypt.Length % 4, '=');
byte[] RawBytes = (Encoding.Default.GetBytes(base64StringToDecrypt));
// byte[] RawBytes = Convert.FromBase64String(base64StringToDecrypt);
ICryptoTransform ictD = acsp.CreateDecryptor();
// Return decrypted bytes as a string
//RawBytes now contains original byte array, still in Encrypted state
//using (MemoryStream ms = new MemoryStream())
//{
// using (var cs = new CryptoStream(ms, ictD, CryptoStreamMode.Write))
// {
// cs.Write(RawBytes,0,RawBytes.Length);
// //using (var swEncrypt = new StreamWriter(csEncrypt))
// //{
// // //Write all data to the stream.
// // swEncrypt.Write(plainText);
// //}
// }
//}
// Encoding encoding = Encoding.UTF8;
// MemoryStream msD = new MemoryStream(RawBytes, 0, RawBytes.Length);
//Decrypt into stream
MemoryStream msD = new MemoryStream();
CryptoStream csD = new CryptoStream(msD, ictD, CryptoStreamMode.Write);
csD.Write(RawBytes, 0, RawBytes.Length);
csD.Close();
Encoding encoding = Encoding.UTF8;
return encoding.GetString(msD.ToArray());
//csD now contains original byte array, fully decrypted
//return the content of msD as a regular string
// return (new StreamReader(csD)).ReadToEnd();
}
}
请帮帮我...我做错了什么
please help me out...what i am doing wrong
推荐答案
检查数组中找到的字节数RawBytes
:它必须是密钥字节数的倍数(您的变量密码
)。密钥应该是128或256位(16或32字节)。
Check the number of bytes found in the arrayRawBytes
: it must me be a multiple of the number of bytes of your key (your variablepassphrase
). And the key ought to be 128 or 256 bits (16 or 32 bytes).
输入数据不是一个完整的块...........使用AES
INPUT DATA IS NOT A COMPLETE BLOCK........... using AES
protected void PerformDecryption(ICryptoTransform decryptor, string inputFilePath, string outputFilePath)
{
//Open both the input file for reading, and
// the output file for writing.
FileStream reader = new FileStream(inputFilePath, FileMode.Open, FileAccess.Read);
FileStream writer = new FileStream(outputFilePath, FileMode.OpenOrCreate, FileAccess.Write);
//Create an Encrypted Stream using the "encryptor" we created above
System.Security.Cryptography.CryptoStream decryptedStream = new System.Security.Cryptography.CryptoStream(writer, decryptor, CryptoStreamMode.Write);
byte[] bites = new byte[1024]; //Stores 1 KB
Int32 bitesRead = 0; //Number of Bytes read
Int64 totalBites = 0; //Total Number of Bytes read
//Loop through the entire file reading 1 kb at a time
while (!(totalBites >= reader.Length))
{
bitesRead = reader.Read(bites, 0, 1024); //Read the bytes from the input file.
decryptedStream.Write(bites, 0, bitesRead); //Write the encrypted bytes to the output file.
totalBites += bitesRead;
}
//Close and release streams:
decryptedStream.Close();
decryptedStream.Dispose();
reader.Close();
reader.Dispose();
writer.Close();
writer.Dispose();
}
这篇关于输入数据不是完整的块。用AES的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文