欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

openssl qt 生成秘钥_关于openssl作的rsa生成密钥及加解密

发布时间:2025/3/20 编程问答 44 豆豆
生活随笔 收集整理的这篇文章主要介绍了 openssl qt 生成秘钥_关于openssl作的rsa生成密钥及加解密 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

关于openssl做的rsa生成密钥及加解密

谁能给个在QtCreator上用openssl做的rsa生成密钥及加解密的例子参考下  网上找的都是片段 不全   谢谢!

RSA

openssl

qtcreator

分享到:

------解决方案--------------------

你可以试验这个

void cryptoRsa(QString input)

{

RSA *rsa=NULL;

qDebug("generating RSA key...\n");

OpenSSL_add_all_algorithms();

rsa = RSA_generate_key(1024, RSA_F4, NULL, NULL);

if(rsa==NULL)

{

qDebug("gen rsa error\n");

exit(-1);

}

BIO *bp;

unsigned char passwd[]="abc";

// key

#ifdef Q_WS_WIN

QString path=WIN_TtempPath+"/private.pem";

qDebug()<

QByteArray ba = path.toUtf8();

const char *c_str2 = ba.data();

printf("str2: %s", c_str2);

bp = BIO_new_file(c_str2, "w");

#endif

#ifdef Q_WS_X11

const char *path = (xmlPath + "/private.pem").toLocal8Bit().data();

qDebug("private:%s", path);

bp = BIO_new_file(path , "w");

#endif

//BIO_write_filename(bp, (void *)("private.pem"));

if(PEM_write_bio_RSAPrivateKey(bp, rsa, EVP_des_ede3_ofb(), passwd,3, NULL,NULL )!=1)

{

qDebug("write public key error\n");

exit(-1);

}

qDebug("store key successfully\n");

BIO_free_all(bp);

qDebug()<

flen=RSA_size(rsa);

unsigned char cipher[flen];

unsigned char *in =(unsigned char *)input.toUtf8().data();

flen = RSA_public_encrypt(flen, in, cipher, rsa, RSA_NO_PADDING );

qDebug( "RSA_public_encrypt: %d\ncipher: %s\n", flen,cipher);

RSA_free(rsa);

#ifdef Q_WS_WIN

QFile file(WIN_TtempPath + "/file.dat");

#else

QFile file(xmlPath + "/file.dat");

#endif

file.open(QIODevice::WriteOnly);

QDataStream out(&file);   // we will serialize the data into the file

总结

以上是生活随笔为你收集整理的openssl qt 生成秘钥_关于openssl作的rsa生成密钥及加解密的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。