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

本文提供了一个在QtCreator环境下使用OpenSSL进行RSA密钥生成及加解密操作的示例代码。示例中详细展示了如何生成1024位的RSA密钥对,并将其保存为文件;同时还包括了如何利用公钥对数据进行加密的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

133000464.gif

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值