开发者

QT C++ - OpenSSL AES - EVP.h

开发者 https://www.devze.com 2023-04-01 10:23 出处:网络
I\'ve been trying to use OpenSSL with QT in the past few days but the documentation and the examples are rare or none.

I've been trying to use OpenSSL with QT in the past few days but the documentation and the examples are rare or none.

Anyway, I come up with this code:

void qkCrypto::AES_CBC(const unsigned char *string, const unsigned char *key, const unsigned char *iv)
{

    int outlen;
    unsigned char *out;

    EVP_CIPHER_CTX ctx;
    EVP_CIPHER_CTX_init(&ctx);
    const EVP_CIPHER *cipher = EVP_aes_128_cbc();

    EVP_EncryptInit(&ctx,cipher,key,iv);
    EVP_EncryptUpdate(&ctx,out,&outlen,string,strlen(string));

    EVP_EncryptFinal(&ctx,out,&outlen);

    EVP_CIPHER_CTX_cleanup(&ctx);

    qDebug() << out;

}

But when I compile it I get some error... this are conversion and cast errors and since I'm not the best C++ programmer out there I would really appreciate some help to solve them:

[...]/qkcrypto.cpp: In member function 'void qkCrypto::AES_CBC(const unsigned char*, const unsigned char*, const unsigned char*)':
[...]/qkcrypto.cpp:39: error: invalid conversion from 'const unsigned char*' to 'const char*'
[...]/qkcrypto.cpp:39: error: initializing argument 1 of 'size_t strlen(const char*)'

Anyway, how can I make this more QT friendly converting the chars to QByteArrays?

Best Regards, Thank you all ;)


Code Update

I've been "messing around" and I'm trying to do this without any luck:

void qkCrypto::AES_CBC(QByteArray string, QByteArray key, QByteArray iv)
{

    int outlen;
    QByteArray out;

    EVP_CIPHER_CTX ctx;
    EVP_CIPHER_CTX_init(&ctx);
    const EVP_CIPHER *cipher = EVP_aes_128_cbc();

    EVP_EncryptInit(&ctx,cipher, key.constData() , iv.constData() );
    EVP_EncryptUpdate(&ctx, out.constData() ,&outlen, string.constData() ,strlen((const char*)string));

    EVP_EncryptFinal(&ctx, out.constData() ,&outlen);

    EVP_CIPHER_CTX_cleanup(&ctx);

    qDebug() << "OUT: "<< out;


}

I got lot's of errors regarding to the conversions:

[...]/qkcrypto.cpp: In member function 'void qkCrypto::AES_CBC(QByteArray, QByteArray, QByteArray)':
[...]/qkcrypto.cpp:38: error: invalid conversion from 'const char*' to 'const unsigned char*'
[...]/qkcrypto.cpp:38: error:   initializing argument 3 of 'int EVP_EncryptInit(EVP_CIPHER_CTX*, const EVP_CIPHER*, const unsigned char*, const unsigned char*)'
[...]/qkcrypto.cpp:38: error: invalid conversion from 'const char*' to 'const unsigned char*'
[...]/qkcrypto.cpp:38: error:   initializing argument 4 of 'int EVP_EncryptInit(EVP_CIPHER_CTX*, const EVP_CIPHER*, const unsigned char*, const unsigned char*)'
[...]/qkcrypto.cpp:39: error: invalid conversion from 'const char*' to 'unsigned char*'
[...]/qkcrypto.cpp:39: error:   initializing argument 2 of 'int EVP_EncryptUpdate(EVP_CIPHER_CTX*, unsigned char*, int*, const unsigned char*, int)'
[...]/qkcrypto.cpp:39: error: invalid conversion from 'const char*' to 'const unsigned char*'
[...]/qkcrypto.cpp:39: error:   initializing argument 4 of 'int EVP_EncryptUpdate(EVP_CIPHER_CTX*, unsigned char*, int*, const unsigned char*, int)'
[...]/qkcrypto.cpp:41: error: invalid conversion from 'const char*' to 'unsigned char*'
[...]/qkcrypto.cpp:41: error:   initializing argument 2 of 'int EVP_EncryptFinal(EVP_CIPHER_C开发者_JAVA百科TX*, unsigned char*, int*)'

Thanks.


Well, as the error says, you're trying to convert between pointers to char and to unsigned char, which is not allowed. You have to add an explicit cast, e.g.

EVP_EncryptUpdate(&ctx, out, &outlen, string, strlen((const char*)string));
                                                     ^^^^^^^^^^^^^


It seems like strlen requires a const pointer to a signed char. So you need to cast that one. Either using the plain C casts ((const char*)), static_cast<const char*> or reinterpret_cast<const char*>. All should go well.


Anyway, how can I make this more QT friendly converting the chars to QByteArrays?

Looking at the documentation, the easiest solution would be something like this:

QByteArray arr((const char*)string);


I've new code now almost perfect.

QByteArray qkCrypto::encrypt_AES_CBC(QByteArray string, QByteArray key, QByteArray ivv)
{

    char mykey[EVP_MAX_KEY_LENGTH] = { 0 };
    strcpy(mykey,key.constData());

    char iv[EVP_MAX_IV_LENGTH] = { 0 };
    strcpy(iv,ivv.constData());

    char ciphertext[1024];

    const EVP_CIPHER *cipher = EVP_aes_256_cbc();
    int in_len;
    int out_len=0;

    in_len = (string.length());

    qDebug() << "Before encrypt: " << string << "\r\n";

         EVP_EncryptInit(&ctx, cipher, (const unsigned char*)mykey, (const unsigned char*)iv);
         EVP_EncryptUpdate(&ctx, ( unsigned char*)ciphertext, &out_len, (const unsigned char*)string.data(), in_len);
         EVP_EncryptFinal(&ctx, ( unsigned char*)&ciphertext[out_len], &out_len);
    EVP_CIPHER_CTX_cleanup(&ctx);
    qDebug() << "Encrypted: " << ciphertext << "\r\n";

    return ciphertext;

}

By some reason the output is only right some times... other times the output is wrong and I can't decrypt it later with OpenSSL.

What's wrong about this? :S

Thanks.

0

精彩评论

暂无评论...
验证码 换一张
取 消