gpt4 book ai didi

c++ - QT C++ - OpenSSL AES - EVP.h

转载 作者:行者123 更新时间:2023-11-28 03:47:10 25 4
gpt4 key购买 nike

过去几天我一直在尝试将 OpenSSL 与 QT 结合使用,但文档和示例很少或没有。

无论如何,我想出了这段代码:

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;

}

但是当我编译它时,我得到了一些错误...这是转换和强制转换错误,因为我不是那里最好的 C++ 程序员,我真的很感激能提供一些帮助来解决它们:

[...]/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*)'

无论如何,我怎样才能使 QT 更友好地将字符转换为 QByteArrays?

最好的问候,谢谢大家 ;)


代码更新

我一直在“胡闹”,我正在努力做到这一点,但没有任何运气:

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;


}

我在转换方面遇到了很多错误:

[...]/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_CTX*, unsigned char*, int*)'

谢谢。

最佳答案

好吧,正如错误所说,您正在尝试在指向 char 的指针和指向 unsigned char 的指针之间进行转换,这是不允许的。您必须添加一个明确的强制转换,例如

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

关于c++ - QT C++ - OpenSSL AES - EVP.h,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7257339/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com