当前位置:我的异常网» QT开发 » 关于openssl作的rsa生成密钥及加解密
关于openssl作的rsa生成密钥及加解密(2)
www.myexceptions.net 网友分享于:2013-07-05 浏览:245次
out <
for(int i=0; i
{
quint8 tmmm=cipher[i];
out <
// qDebug()<
}
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()<
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()<
QDataStream in(&file1); // read the data serialized from the file
in.setVersion(QDataStream::Qt_4_6);
quint32 magic;
in >> magic;
qDebug()<
unsigned char tmpp[flen];
unsigned char cipher[flen];
for(int j=0;j
{
quint8 tm;
in>>tm; // extract "the answer is" and 42
tmpp[j]=tm;
//qDebug()<
//qDebug()<
}
qDebug("dRsa::%s",tmpp);
flen = RSA_private_decrypt( flen, tmpp, cipher, rsaK , RSA_NO_PADDING );
文章评论