C ++ AES解密错误:输入数据不是一个完整的块? [英] C++ AES decryption error: the input data is not a complete block?
问题描述
这是C#中的加密方法:
Here`s the encryption method in C#:
string CryptographyKey = "BC234xs45nme7HU9";
public byte[] Encrypt(byte[] IN)
{
byte[] OUT;
using (Aes encryptor = Aes.Create())
{
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(CryptographyKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(IN, 0, IN.Length);
cs.Close();
}
OUT = ms.ToArray();
}
}
return OUT;
}
这里是c ++中的解密方法:
And here's the decryption method in c++:
void Cryptograph::Decrypt(void* in, void* out, int Size)
{
try
{
unsigned char* pIn = (unsigned char*)in;
unsigned char* pOut = (unsigned char*)out;
cli::array<unsigned char> ^Buffer;
Buffer = gcnew cli::array<unsigned char>(Size);
cli::array<unsigned char> ^OutBuffer;
OutBuffer = gcnew cli::array<unsigned char>(Size);
cli::array<unsigned char> ^Salt;
Salt = gcnew cli::array<unsigned char>(13) { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 };
for (int i = 0; i < Size; i++)
Buffer[i] = pIn[i];
Aes^ encryptor = Aes::Create();
System::String^ CryptographyKey = "BC234xs45nme7HU9";
Rfc2898DeriveBytes^ pdb = gcnew Rfc2898DeriveBytes(CryptographyKey, Salt);
encryptor->Key = pdb->GetBytes(32);
encryptor->IV = pdb->GetBytes(16);
ICryptoTransform^ ICT = encryptor->CreateDecryptor();
MemoryStream^ ms = gcnew MemoryStream();
CryptoStream^ cs = gcnew CryptoStream(ms, ICT, CryptoStreamMode::Write);
cs->Write(Buffer, 0, Size);
OutBuffer = ms->ToArray();
cs->Close();
ms->Close();
delete cs;
for (int i = 0; i < Size; i++)
pOut[i] = OutBuffer[i];
}
catch(Exception^ e) { throw gcnew Exception("Failed to create DES Symmetric CryptoStream with error: " + e->Message); }
}
我尝试过:
但它一直在cs-> Close();那说它不是一个完整的块?我正在使用16字节/ 128位阵列,块大小设置为128.我不明白什么是错的?
解密时没有输出结果异常消息...即使我评论了流关闭代码我在内存流关闭时得到异常,如果它也被注释,则OutBuffer总是为空且内存流长度为0
What I have tried:
But it keeps throwing an exception at cs->Close(); that says it's not a complete block? I'm using a 16 byte/ 128 bit array with the block size set to 128. I don't understand what's wrong?
No output results on decryption just that exception message... even if i commented on stream close code i get exception on memory stream close, if its commented too, the OutBuffer is always empty and memory stream length is 0
推荐答案
调用Close应该调用FlushFinalBlock - 但是参考源显示它没有专门覆盖Rfc2898DeriveBytes类中的Close方法,所以可能值得将你的调用替换为close:
Calling Close should call FlushFinalBlock - but the reference sources show that it doesn't
specifically override the Close method in the Rfc2898DeriveBytes class, so it might be worth replacing your call to close:
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(IN, 0, IN.Length);
cs.FlushFinalBlock();
}
鉴于using块将关闭并为你处理流。
Given that the using block will Close and Dispose the stream for you.
好的问题已修复,我忘了加密第一个数据包被发送到c ++应用程序,现在一切顺利
我也编辑了这样的代码
Okay problem fixed, i forgot to encrypt first packet was sent to the c++ app, now everything is going well
I also edited the code to be like this
unsigned char* Cryptograph::Decrypt(void* in, int Size)
{
try
{
unsigned char* pIn = (unsigned char*)in;
cli::array<unsigned char> ^Buffer;
Buffer = gcnew cli::array<unsigned char>(Size);
cli::array<unsigned char> ^OutBuffer;
OutBuffer = gcnew cli::array<unsigned char>(Size);
cli::array<unsigned char> ^Salt;
Salt = gcnew cli::array<unsigned char>(13) { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 };
for (int i = 0; i < Size; i++)
Buffer[i] = pIn[i];
Aes^ encryptor = Aes::Create();
System::String^ CryptographyKey = "BC234xs45nme7HU9";
Rfc2898DeriveBytes^ pdb = gcnew Rfc2898DeriveBytes(CryptographyKey, Salt);
encryptor->Key = pdb->GetBytes(32);
encryptor->IV = pdb->GetBytes(16);
ICryptoTransform^ ICT = encryptor->CreateDecryptor();
MemoryStream^ ms = gcnew MemoryStream();
CryptoStream^ cs = gcnew CryptoStream(ms, ICT, CryptoStreamMode::Write);
cs->Write(Buffer, 0, Size);
cs->FlushFinalBlock();
OutBuffer = ms->ToArray();
cs->Close();
ms->Close();
delete cs;
unsigned char* pOut = new unsigned char[OutBuffer->Length];
for (int i = 0; i < OutBuffer->Length; i++)
pOut[i] = OutBuffer[i];
return pOut;
}
catch (System::Exception^ e) { throw gcnew System::Exception("Failed to create DES Symmetric CryptoStream with error: " + e->Message); }
}
这篇关于C ++ AES解密错误:输入数据不是一个完整的块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!