Crypto++ Ver: 4.2 Dev Tool: msvc++ 6
This is a real entry-level question so bear with me if I explain it badly. I�m starting to get in to cryptography so I�m looking at the test code supplied with crypto v4.2, this is the sample usages of the crypto library found in the test.cpp file.
I�m looking at simple string encryption/decryption with a key, so from that example I�m using the routines:
string EncryptString(const char *instr, const char *passPhrase); string DecryptString(const char *instr, const char *passPhrase);
which in turn use DefaultEncryptorWithMAC, and HexDecoder.
The worked example works fine, however when I try to decrypt an input string that is not perfect (assume its lost a few characters, or its been corrupted or a someone has tried to fake/guess what some input should be) the test application crashes in both console on windows.
It seems there is no tolerance for �bad� encrypted string in the decrypt routine? Its not unreasonable to think that at some point an input will be wrong in some way so how can I check this rather than just having the application crash?
Code is passed bellow.
Thanks for any guidance Ewan
===============
#include "pch.h"
#include "md5.h" #include "sha.h" #include "ripemd.h" #include "files.h" #include "rng.h" #include "hex.h" #include "gzip.h" #include "default.h" #include "rsa.h" #include "randpool.h" #include "ida.h" #include "socketft.h"
#include "validate.h" #include "bench.h"
#include <iostream>
#if (_MSC_VER >= 1000) #include <crtdbg.h> // for the debug heap #endif
#if defined(__MWERKS__) && defined(macintosh) #include <console.h> #endif
USING_NAMESPACE(CryptoPP) USING_NAMESPACE(std)
const int MAX_PHRASE_LENGTH=250;
string EncryptString(const char *plaintext, const char *passPhrase); string DecryptString(const char *ciphertext, const char *passPhrase);
int main(int argc, char *argv[])
{
// try with these values - works fine
//pass: 1234
//cyphertext: C833582F5D00AB48E8B59ED5F1C6D7122F86689E8E86C042CC03A6DF4326ACE1AFC0DD1BABD13E78C20642F8F54C06A2CF28610FBE73B40A
//plain: try to decrypt this
// try with these values - crash every time
//pass: 1234
//cyphertext: SOMETHINGAHACKERMIGHTTRYORAENCRYPTEDTEXTWITHAMISSINGCHARACTER
char passPhrase[MAX_PHRASE_LENGTH], ciphertext[1024];
cout << "Passphrase: ";
cin.getline(passPhrase, MAX_PHRASE_LENGTH);
cout << "\nCiphertext: "; cin.getline(ciphertext, 1024);
string decrypted = DecryptString(ciphertext, passPhrase); cout << "\nDecrypted: " << decrypted << endl; }
// statics exactly the same as those supplied in v4.2 text.cpp file
string EncryptString(const char *instr, const char *passPhrase)
{
string outstr;DefaultEncryptorWithMAC encryptor(passPhrase, new HexEncoder(new StringSink(outstr)));
encryptor.Put((byte *)instr, strlen(instr));
encryptor.MessageEnd();
return outstr; }
string DecryptString(const char *instr, const char *passPhrase)
{
string outstr;HexDecoder decryptor(new DefaultDecryptorWithMAC(passPhrase, new StringSink(outstr)));
decryptor.Put((byte *)instr, strlen(instr));
decryptor.MessageEnd();
return outstr; }
