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生成密钥及加解密的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vue状态管理存取数据_vue状态管理v
- 下一篇: alert不会影响到页面中其他代码执行_