this is a function I wrote & tested for encrypting an in memory file stored in a QByteArray.
it takes a QByteArray and a passPhrase and then encrypts the contents of the QByteArray.
Crypto++ encryption class which it uses is DefaultEncryptorWithMAC and according to the reference doc it uses Password-Based Encryptor using DES-EDE2 and HMAC/SHA-1.
void encrypt
(QByteArray &in_out,
const char *passPhrase
) {
string tmp;
StringSource s((const byte *)in_out.constData(), in_out.size(), true, new DefaultEncryptorWithMAC(passPhrase, new StringSink(tmp)));
in_out.clear();
in_out.
append(QByteArray(tmp.
c_str(), tmp.
size()));
}
void encrypt(QByteArray &in_out, const char *passPhrase) {
string tmp;
StringSource s((const byte *)in_out.constData(), in_out.size(), true, new DefaultEncryptorWithMAC(passPhrase, new StringSink(tmp)));
in_out.clear();
in_out.append(QByteArray(tmp.c_str(), tmp.size()));
}
To copy to clipboard, switch view to plain text mode
for decrypting u simply need to a DefaultDecryptorWithMAC instead of the DefaultEncryptorWithMAC:
void decrypt
(QByteArray &in_out,
const char *passPhrase
) {
string tmp;
StringSource s((const byte *)in_out.constData(), in_out.size(), true, new DefaultDecryptorWithMAC(passPhrase, new StringSink(tmp)));
in_out.clear();
in_out.
append(QByteArray(tmp.
c_str(), tmp.
size()));
}
void decrypt(QByteArray &in_out, const char *passPhrase) {
string tmp;
StringSource s((const byte *)in_out.constData(), in_out.size(), true, new DefaultDecryptorWithMAC(passPhrase, new StringSink(tmp)));
in_out.clear();
in_out.append(QByteArray(tmp.c_str(), tmp.size()));
}
To copy to clipboard, switch view to plain text mode
I am new to Crypto++ (and to cryptography in general) and haven't read the entire crypto++'s reference; Any idea about a better code is welcome.
Bookmarks