我正在尝试编写 2 个不同的函数来在 cryptopp 中使用 AES 加密和解密数据。我想将密文作为参数传递给解密函数。但是在解密功能上,它会收到一些特殊符号作为密文,因此无法正确解密。请帮忙。
// -- AES encryption function ----------
void Security_packetAgent::encryption(char out[])
{
std::string plaintext = out;
std::string ciphertext = "";
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*>( plaintext.c_str() ), plaintext.length() + 1 );
stfEncryptor.MessageEnd();
std::cout << "Cipher Text (" << ciphertext.size() << " bytes)" << std::endl;
for( int i = 0; i < ciphertext.size(); i++ )
{
std::cout << "0x" << std::hex << (0xFF & static_cast<byte>(ciphertext[i])) << " ";
}
std::cout<<"\nMessage encrypted ...";
std::cout << std::endl << std::endl;
sprintf(out, "%s", ciphertext.c_str());
printf("Final Data: %s : %s ", out, reinterpret_cast<const unsigned char*>(ciphertext.c_str()));
}
// ---- AES decryption ------------------
void Security_packetAgent::decryption(char out[])
{
cout<<"\nCipher recieved: "<<out;
std::cout<<"\nEntered decryption ..";
std::string ciphertext = out;
std::string decryptedtext;
cout<<"\nCipher recieved: "<<reinterpret_cast<const unsigned char*>( ciphertext.c_str());
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*>( ciphertext.c_str() ), ciphertext.size() );
stfDecryptor.MessageEnd();
std::cout << "Decrypted Text: " << std::endl;
std::cout << decryptedtext;
std::cout << std::endl << std::endl;
}