关于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()<<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
out << (quint32)0xA0B0C0D0;
for(int i=0; i<flen; i++)
{
quint8 tmmm=cipher[i];
out <<tmmm; // serialize a string
// qDebug()<<tmmm;
}
out.setVersion(QDataStream::Qt_4_6);
qDebug("cRsa::%s",cipher);
}
QString decryptRsa()
{
RSA *rsaK=RSA_new();
OpenSSL_add_all_algorithms();
BIO *BP;
#ifdef Q_WS_WIN
QString path=WIN_TtempPath+"/private.pem";
QByteArray ba = path.toLatin1();
const char *c_str2 = ba.data();
printf("str2: %s", c_str2);
BP=BIO_new_file(c_str2, "rb");
#endif
#ifdef Q_WS_X11
const char *path = (xmlPath + "/private.pem").toLocal8Bit().data();
qDebug("private:%s", path);
BP=BIO_new_file(path, "rb");
#endif
char PSW[]="abc";
rsaK=PEM_read_bio_RSAPrivateKey(BP,&rsaK,0,PSW);
qDebug()<<rsaK;
flen=RSA_size(rsaK);
if(rsaK==NULL)
{
qDebug("read failed!\n");
BIO_free(BP);
RSA_free(rsaK);
exit(-1);
}
qDebug("get key successfully\n");
#ifdef Q_WS_WIN
QFile file1(WIN_TtempPath + "/file.dat");
#else
QFile file1(xmlPath + "/file.dat");
#endif
if(file1.open(QIODevice::ReadOnly))
qDebug()<<"open!";
QDataStream in(&file1); // read the data serialized from the file
in.setVersion(QDataStream::Qt_4_6);
quint32 magic;
in >> magic;
qDebug()<<magic;
unsigned char tmpp[flen];
unsigned char cipher[flen];
for(int j=0;j<flen;j++)
{
quint8 tm;
in>>tm; // extract "the answer is" and 42
tmpp[j]=tm;
//qDebug()<<tm;
//qDebug()<<"cc:"<<cc<<endl;
}
qDebug("dRsa::%s",tmpp);
flen = RSA_private_decrypt( flen, tmpp, cipher, rsaK , RSA_NO_PADDING );
qDebug( "RSA_public_encrypt: %d\ncipher: %s\n", flen,cipher );
RSA_free(rsaK);
BIO_free(BP);
QString out=QString::fromUtf8(QByteArray((char*)cipher));
qDebug()<<out<<"\n";
return out;
}
PEM_write_bio_RSAPrivateKey(bp, rsa, EVP_des_ede3_ofb(), passwd,3, NULL,NULL )
int PEM_write_bio_RSAPublicKey(BIO *bp, RSA *x);