我是cryptopp的新手,我尝试加密和解密文件中的文本。在这些行之后,我总是在内存位置 0x0012efe4 处收到此错误 CryptoPP::InvalidCiphertext:
CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink( decryptedtext ) );
stfDecryptor.Put( reinterpret_cast<const unsigned char*>( ciphertext.c_str() ), ciphertext.length());
stfDecryptor.MessageEnd();
加密/解密代码:
BOOL Encryption()
{
// Key and IV setup
byte key[ CryptoPP::AES::DEFAULT_KEYLENGTH ], iv[ CryptoPP::AES::BLOCKSIZE ];
memset( key, 0x00, CryptoPP::AES::DEFAULT_KEYLENGTH );
memset( iv, 0x00, CryptoPP::AES::BLOCKSIZE );
HW_PROFILE_INFO hwProfileInfo;
GetCurrentHwProfile(&hwProfileInfo);
(hwProfileInfo.szHwProfileGuid, strlen(hwProfileInfo.szHwProfileGuid), key);
(hwProfileInfo.szHwProfileGuid, strlen(hwProfileInfo.szHwProfileGuid), iv);
// String and Sink setup
string STRING;
ifstream infile;
infile.open ("test2.txt");
getline(infile,STRING, '\0'); // Saves the line in STRING.
char cFilm[1000];
strcpy(cFilm,STRING.c_str());
infile.close();
std::string plaintext = cFilm;
std::string ciphertext;
std::string decryptedtext;
// Dump Plain Text
std::cout << "Plain Text (" << plaintext.size() << " bytes)" << std::endl;
std::cout << plaintext;
std::cout << std::endl << std::endl;
// Create Cipher Text
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();
// Dump Cipher Text
ofstream write ("test2a.txt", ios::out | ios::binary);
int at = ciphertext.length()+ 1;
write.write(ciphertext.c_str(),at);
write.close();
ciphertext.erase();
remove("test2.txt");
rename("test2a.txt","c:\\test2.txt");
return 0;
}
BOOL Decryption()
{
// Key and IV setup
byte key[ CryptoPP::AES::DEFAULT_KEYLENGTH ], iv[ CryptoPP::AES::BLOCKSIZE ];
memset( key, 0x00, CryptoPP::AES::DEFAULT_KEYLENGTH );
memset( iv, 0x00, CryptoPP::AES::BLOCKSIZE );
HW_PROFILE_INFO hwProfileInfo;
GetCurrentHwProfile(&hwProfileInfo);
// String and Sink setup
string STRING2;
ifstream infile2;
infile2.open ("test2.txt",ios::binary);
getline(infile2,STRING2, '\0'); // Saves the line in STRING.
char cFilm2[1000];
strcpy(cFilm2,STRING2.c_str());
infile2.close();
std::string ciphertext (cFilm2);
std::string decryptedtext;
// Decrypt
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.length());
stfDecryptor.MessageEnd();
// Dump Decrypted Text
ofstream write ("test2a.txt", ios::out | ios::binary);
write << decryptedtext;
write.close();
decryptedtext.erase();
remove("test2.txt");
rename("test2a.txt","test2.txt");
return 0;
}