解密密文时InvalidCiphertext异常 [英] InvalidCiphertext exception when decrypting ciphertext
问题描述
我正在使用一个新的安全通信协议,我有解密密文的问题。
I'm working in a new protocol for secure communication and I'm having problems to decrypt the ciphertext.
数据包保存在uint8_t *变量中并加密。直到这一部分都一切顺利。但是当我尝试解密我有以下问题:
The data packet is saved in a uint8_t* variable and encrypted. Until this part is all going well. But when I try to decrypt I got the followings problems:
1)如果我发送矢量和大小(它真的20,但我只是想解密最后16字节):
1) If I send the vector and the size (it's really 20 but I just want to decrypt the last 16 bytes):
CBC_Mode< AES >::Decryption decryptor;
decryptor.SetKeyWithIV( key, CryptoPP::AES::DEFAULT_KEYLENGTH, iv );
CryptoPP::StringSource ss( vector+4, 16 , true,
new CryptoPP::StreamTransformationFilter( decryptor,
new CryptoPP::StringSink( decryptedtext ) ) );
我得到:
terminate called after throwing an instance of 'CryptoPP::InvalidCiphertext'
what(): StreamTransformationFilter: invalid PKCS #7 block padding found
2)如果我只是发送不带大小的向量:
2) If I just send the vector without size:
CryptoPP::StringSource ss( vector+4, true,
new CryptoPP::StreamTransformationFilter( decryptor,
new CryptoPP::StringSink( decryptedtext ) ) );
程序运行,但我只是得到所有00:
The programs runs but I just get all 00:
Text Encrypted (20 bytes)
8c 97 b7 d8 74 80 3d 9f 9f 62 2e 93 38 c7 d1 b de a4 21 80
Text Decrypted (16 bytes)
0 0 0 0 0 0 0 0 68 0 0 0 0 0 0 0 0 0 0 0
我读到,可能是密钥没有正确生成,但我的工作大小为16,这里是我怎么做:
I read that it could be that the key is not generated correctly, but I'm working with a size of 16 and here is how I do it:
byte key[ CryptoPP::AES::DEFAULT_KEYLENGTH ], iv[ CryptoPP::AES::BLOCKSIZE ];
memset( key, 0x00, CryptoPP::AES::DEFAULT_KEYLENGTH );
memset( iv, 0x00, CryptoPP::AES::BLOCKSIZE );
3)我还试图将向量转换为char并以字符串形式发送:
3) I also tried to cast the vector to char and send it like an string:
CryptoPP::StringSource ss( reinterpret_cast<const unsigned char*>( (vector + 4) ), 16, true,
new CryptoPP::StreamTransformationFilter( decryptor,
new CryptoPP::StringSink( decryptedtext ) ) );
但我再次得到同样的结果:
But again I get the same thing:
terminate called after throwing an instance of 'CryptoPP::InvalidCiphertext'
what(): StreamTransformationFilter: invalid PKCS #7 block padding found
请帮助,我已经尝试了几天来弄清楚是什么问题。这花了我太长时间,我找不到解决方案。
Please help, I have tried for days to figure out what's wrong. This is taking me too long and I can't find the solutions.
有没有人知道可能发生什么?
Does anyone have any idea on what might be happening?
如果您需要更多详细信息,代码或任何内容,请与我们联系。
Let me know if you need further details, code or anything.
编辑:
4)我尝试的另一件事情是(构建解密器的另一种方法):
4) One more thing that I tried was (another way to construct the decrypter):
CryptoPP::AES::Decryption aesDecryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption( aesDecryption, iv );
CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink( decryptedtext ) );
stfDecryptor.Put( reinterpret_cast<const unsigned char*>( (vector + 4) ), 16 );
stfDecryptor.MessageEnd();
但我得到:
terminate called after throwing an instance of 'CryptoPP::InvalidCiphertext'
what(): StreamTransformationFilter: invalid PKCS #7 block padding found
Edit2:
使用此行创建向量因为我使用一个平台进行网络编码,所以很复杂):
The vector is created with this line (the way the vector is fulled is a quite complicated to put it here because I am using a platform for network encoding) :
uint8_t* vector;
Edit3:
加密向量。
CryptoPP::AES::Encryption aesEncryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption( aesEncryption, iv );
CryptoPP::StreamTransformationFilter stfEncryptor(cbcEncryption, new CryptoPP::StringSink( ciphertext ) );
stfEncryptor.Put( reinterpret_cast<const unsigned char*>( (vector + 4) ), 16 );
stfEncryptor.MessageEnd();
然后我把密文再次放在向量中:
And after that I put the ciphertext again in the vector:
std::cout << std::endl << std::endl;
for(int i=0;i < 16; i++){
*(vector+ i + 4) = (ciphertext[i]) ;
}
推荐答案
问题是编码与填充和试图解密它没有。
The problem was that I was encoding with padding and trying to decrypt it without. So I add not only in the encryption but in the descryption that it should work without padding.
创建密钥和IV:
CBC_Mode< AES >::Encryption encryptor;
encryptor.SetKeyWithIV( key, CryptoPP::AES::DEFAULT_KEYLENGTH, iv );
加密:
CryptoPP::StringSource ss( vector + 4 , 16, true,
new CryptoPP::StreamTransformationFilter( encryptor,
new CryptoPP::StringSink( ciphertext ),
CryptoPP::StreamTransformationFilter::NO_PADDING
) // StreamTransformationFilter
); // StringSource
解密:
CBC_Mode< AES >::Decryption decryptor;
decryptor.SetKeyWithIV( key, CryptoPP::AES::DEFAULT_KEYLENGTH, iv );
CryptoPP::StringSource ss( reinterpret_cast<const unsigned char*>( (vector + 4) ), 16, true,
new CryptoPP::StreamTransformationFilter( decryptor,
new CryptoPP::StringSink( decryptedtext ), CryptoPP::StreamTransformationFilter::NO_PADDING) );
我这样做是因为我不想使用填充。
I did this because I don't want to work with padding anymore.
这篇关于解密密文时InvalidCiphertext异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!