QT+openssl实现以下Java代码 并写出pro文件public static String decryptString str String privateKey throws Exception 64位解码加密后的字符串-----解码字符串UTF-8类型 byte inputByte = Base64decodeBase64strgetBytesUTF-8;
对于此Java代码,在QT中可以使用openssl库进行实现。在.pro文件中,需要添加以下内容:
LIBS += -lssl -lcrypto
接下来,我们可以使用以下代码实现Java代码的功能:
QString decrypt(QString str, QString privateKey) { QByteArray inputByte = QByteArray::fromBase64(str.toUtf8()); QByteArray decoded = QByteArray::fromBase64(privateKey.toUtf8());
const unsigned char* pkcs8Key = (const unsigned char*) decoded.constData();
RSA* rsa = d2i_RSAPrivateKey(NULL, &pkcs8Key, decoded.length());
if (rsa == NULL) {
qDebug() << "Error: RSA private key decode failed";
return "";
}
QByteArray resultBytes;
const int MAX_ENCRYPT_BLOCK = 128;
int inputLength = inputByte.length();
int offSet = 0;
while (inputLength - offSet > 0) {
QByteArray cache;
int length = (inputLength - offSet > MAX_ENCRYPT_BLOCK) ? MAX_ENCRYPT_BLOCK : inputLength - offSet;
cache.resize(length);
memcpy(cache.data(), inputByte.constData() + offSet, length);
unsigned char* output = new unsigned char[RSA_size(rsa)];
int outlen = RSA_private_decrypt(length, (const unsigned char*) cache.constData(), output, rsa, RSA_PKCS1_PADDING);
if (outlen < 0) {
qDebug() << "Error: RSA decryption failed";
RSA_free(rsa);
return "";
}
resultBytes.append((char*) output, outlen);
delete[] output;
offSet += length;
}
RSA_free(rsa);
return QString::fromUtf8(resultBytes.constData(), resultBytes.length());
原文地址: https://www.cveoy.top/t/topic/ef6D 著作权归作者所有。请勿转载和采集!