4 #include <openssl/evp.h>
11 static bool seed =
false;
15 QTime time = QTime::currentTime();
18 qsrand( (
uint)time.msec() );
23 QByteArray iv_ba( 16,
'0' );
25 for (
int i = 0; i < 16; i++ )
26 iv_ba[ i ] = qrand() % 256;
28 uchar* iv_ptr = (uchar*)iv_ba.data();
33 for (
int i = 0; i < pw.size(); i++ )
34 key[ i ] = pw[ i ].cell();
36 QString plaintext = plain_text;
37 QByteArray plain_ba = plaintext.toAscii();
38 uchar* plain_ptr = (uchar*)plain_ba.data();
45 EVP_EncryptInit ( &ctx, EVP_aes_128_cbc(), key, iv_ptr );
46 EVP_EncryptUpdate( &ctx, out, &out_length, plain_ptr, plaintext.size() );
47 EVP_EncryptFinal ( &ctx, &out[ out_length ], &final_length );
49 int c_size = out_length + final_length;
51 QByteArray cipher_ba = QByteArray( (
const char*)out, c_size );
53 result << cipher_ba.toHex() << iv_ba.toHex();
59 const QString& initVector )
61 if ( pw.size() == 0 )
return QString();
66 for (
int i = 0; i < pw.size(); i++ )
67 key[ i ] = pw[ i ].cell();
69 QByteArray iv_ba = QByteArray::fromHex( initVector.toAscii() );
70 uchar* iv_ptr = (uchar*)iv_ba.data();
72 QByteArray cipher_ba = QByteArray::fromHex( ciphertext.toAscii() );
73 uchar* cipher_ptr = (uchar*)cipher_ba.data();
81 EVP_DecryptInit ( &ctx, EVP_aes_128_cbc(), key, iv_ptr );
82 EVP_DecryptUpdate( &ctx, out, &ol, cipher_ptr, cipher_ba.size() );
83 EVP_DecryptFinal ( &ctx,
final, &ol );
85 QByteArray final_ba( (
char*)
final, ol );