#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
m_prsa = NULL;
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_2_clicked()
{
createRSAKey();
}
void MainWindow::createRSAKey()
{
//创建目录
QString strPath = "./key";
QDir dir;
if(dir.exists(strPath) == false)
{
dir.mkpath(strPath);//创建目录
}
BIGNUM *bne = BN_new();
int ret = BN_set_word(bne,RSA_F4);/* 设置随机数的长度 */
RSA *rsa = RSA_new();
ret = RSA_generate_key_ex(rsa,1024,bne,NULL);/*设置密钥的长度1024,生成RSA密钥对 */
if(ret != 1)
{
QMessageBox::information(this,"错误提示","生成RSA密钥错误!无法实现后续数字签名!",QMessageBox::Yes);
return;
}
/* 提取私钥 */
FILE *filename = NULL;
filename = fopen("./key/privateKey.pem", "wb");
PEM_write_RSAPrivateKey(filename, rsa, NULL, NULL, 0, NULL, NULL);
fclose(filename);
filename = NULL;
/* 提取公钥 */
unsigned char *n_b = (unsigned char *)calloc(RSA_size(rsa), sizeof(unsigned char));
unsigned char *e_b = (unsigned char *)calloc(RSA_size(rsa), sizeof(unsigned char));
int n_size = BN_bn2bin(rsa->n, n_b);
int b_size = BN_bn2bin(rsa->e, e_b);
RSA *pubrsa = RSA_new();
pubrsa->n = BN_bin2bn(n_b, n_size, NULL);
pubrsa->e = BN_bin2bn(e_b, b_size, NULL);
// //另一种格式的公钥输出
// filename = fopen("./key/publicKeyRSA.pem", "wb");
// PEM_write_RSAPublicKey(filename, pubrsa);
// fclose(filename);
// filename = NULL;
filename = fopen("./key/publicKey.pem", "wb");
PEM_write_RSA_PUBKEY(filename, pubrsa);
fclose(filename);
filename = NULL;
RSA_free(pubrsa);
RSA_free(rsa);
}
void MainWindow::CreateSignature(QByteArray const &strxml,unsigned char *bSign,size_t *iSignlen)
{
unsigned char *bData = NULL;//报文
bData = (unsigned char*)(strxml.data());
size_t iDatalen = strxml.size();
LoadRSA();//这里可以放在构造函数执行一次加载进来就可以了
unsigned char szTmp[20]={0};
SHA1(bData, iDatalen, szTmp);//一定先生成摘要
int ret = RSA_sign(NID_sha1,szTmp,20,bSign,iSignlen,m_prsa);
if(ret!=1)
{
QMessageBox::information(this,"错误提示","生成签名失败!",QMessageBox::Yes);
}
}
void MainWindow::on_pushButton_clicked()
{
unsigned char bsign[128]={0};// 保存数字签名
size_t sign_len = 0; //数字签名的长度
QString strXml;
strXml = ui->lineEdit->text();
// 先utf8编码
CreateSignature(strXml.toUtf8(), bsign, &sign_len);
// 十六进制输出,不是RSA签名的一部分
QByteArray signByteArray(reinterpret_cast<char*>(&bsign),sign_len);
QString strShow(signByteArray.toHex());
ui->textEdit->setText(strShow);
}
void MainWindow::LoadRSA()
{
if(m_prsa != NULL)
return;
FILE *file;
if((file=fopen(".//key//privateKey.pem","r"))==NULL)
{
QMessageBox::information(this,"错误提示","打开RSA密钥错误!",QMessageBox::Yes);
return;
}
//奇怪的是p_rsa指针放到外面作为成员时,初始化一次,打算反复使用,指针失效了,变为空了
m_prsa = PEM_read_RSAPrivateKey(file,NULL,NULL,NULL);
fclose(file);
if(m_prsa == NULL)
{
QMessageBox::information(this,"错误提示","读取RSA密钥错误!",QMessageBox::Yes);
return;
}
}
Qt利用OpenSSl实现RSA数字签名
4星 · 超过85%的资源 需积分: 45 89 浏览量
2017-07-05
16:07:41
上传
评论
收藏 19.99MB RAR 举报
别逗我乐
- 粉丝: 91
- 资源: 36
最新资源
- video_20240425_124410_edit.mp4
- IMG_20240425_120538.jpg
- My Complete Genome_6k Base-Pairs of Phenotype SNPs_Complete Raw Data.zip
- qt 的mqtt测试demo
- 移动应用开发教程-zip.zip
- mosquitto-2.018-install-windows-x64
- FTPServer FTP 服务器,绿色免安装,单文件
- 梦畅语音点名软件,上课点名
- 利用ADNI数据集和标签,在tensorflow框架上使用tensorlayer接口,通过架构u-net实现海马体的分割
- Kutools for Word v9.0 office word 插件
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈