QByteArray encrypt(QByteArray in) { byte iv[CryptoPP::AES::BLOCKSIZE]; rnd.GenerateBlock(iv, CryptoPP::AES::BLOCKSIZE); QByteArray out = QByteArray((char*)iv, CryptoPP::AES::BLOCKSIZE); int inputSize = in.size(); string cipher; CBC_Mode<AES>::Encryption aes(keyByte(), keySize(), iv); ArraySource((byte *)in.data(), inputSize, true, new StreamTransformationFilter(aes, new StringSink(cipher))); QByteArray encryptedBytes = QByteArray(cipher.c_str(), cipher.size()); out.append(encryptedBytes); return out; }
QByteArray decrypt(QByteArray in) { QByteArray iv = in.left(AES::BLOCKSIZE); in.remove(0, AES::BLOCKSIZE); string decrypted; QByteArray result; try { CBC_Mode<AES>::Decryption aes_dec; aes_dec.SetKeyWithIV((byte *)key().data(), keySize(), (byte *)iv.data()); ArraySource((byte *)in.data(), in.size(), true, new StreamTransformationFilter(aes_dec, new StringSink(decrypted))); result = QByteArray(decrypted.c_str(), in.size()); } catch (CryptoPP::Exception err) { result = "Failed to decrypt"; qDebug() << QString(err.GetWhat().c_str()); } return result; }