cryptopp561 InvalidCiphertext

210 views
Skip to first unread message

TrockFox

unread,
Feb 20, 2013, 1:21:16 PM2/20/13
to cryptop...@googlegroups.com
Hello,

I am new with cryptopp,i try to encrydpt and decrypt text from a file.
I alway receive this error CryptoPP::InvalidCiphertext at memory location 0x0012efe4 just after these lines :
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink( decryptedtext ) );
stfDecryptor.Put( reinterpret_cast<const unsigned char*>( ciphertext.c_str() ), ciphertext.length());
stfDecryptor.MessageEnd();
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

The encryption/decryption code:

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
BOOL Encryption()
{
//
	// Key and IV setup
	//AES encryption uses a secret key of a variable length (128-bit, 196-bit or 256-  
	//bit). This key is secretly exchanged between two parties before communication  
	//begins. DEFAULT_KEYLENGTH= 16 bytes
	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
	//AES encryption uses a secret key of a variable length (128-bit, 196-bit or 256-  
	//bit). This key is secretly exchanged between two parties before communication  
	//begins. DEFAULT_KEYLENGTH= 16 bytes
	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);
//char* rawKey="malakagergeerwherwhewrhwehewhhwrehrewhewhrewrwhherwherwh";
(hwProfileInfo.szHwProfileGuid, strlen(hwProfileInfo.szHwProfileGuid), key);
//char* rawIv="malakaherwheherwheerwhewrwherwhgerwhewrhewrrwhewrhewrherw";
(hwProfileInfo.szHwProfileGuid, strlen(hwProfileInfo.szHwProfileGuid), iv);
//
	// 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;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Anyone know how to solve this??
Thanks!

cppi812

unread,
Feb 20, 2013, 8:51:05 PM2/20/13
to cryptop...@googlegroups.com
Please try explicitly opening this in binary mode in your encryption function?

//your code
infile.open("test2.txt");

//try this
infile.open("test2.txt", ios::in | ios::binary);

TrockFox

unread,
Feb 21, 2013, 10:32:29 AM2/21/13
to cryptop...@googlegroups.com

 hello,

I still receive this error CryptoPP::InvalidCiphertext at memory location 0x0012efe4 just after these lines :

RusiEll

unread,
Apr 11, 2014, 1:33:38 AM4/11/14
to cryptop...@googlegroups.com
Hi,
 I have to same problem..
Did you solve it?

2013년 2월 21일 목요일 오전 3시 21분 16초 UTC+9, TrockFox 님의 말:

Jeffrey Walton

unread,
Apr 11, 2014, 8:50:48 AM4/11/14
to cryptop...@googlegroups.com
On Wednesday, February 20, 2013 8:51:05 PM UTC-5, cppi812 wrote:
Please try explicitly opening this in binary mode in your encryption function?

//your code
infile.open("test2.txt");

//try this
infile.open("test2.txt", ios::in | ios::binary);
I think you'll also need noskipws flag if its is a binary file.

I did not check the code, though (just commenting on iso::binary).

Jeff

Reply all
Reply to author
Forward
0 new messages