谁能给个在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()<<path<<endl;
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()<<input<<"\n";
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