cryptopp561 InvalidCiphertext

Hello,

I am new with cryptopp,i try to encrypt and decrypt text from a file.
I alway receive this error CryptoPP::InvalidCiphertext at memory location 0x0012efe4 just after these lines :

1
2
3
    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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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!
Topic archived. No new replies allowed.