-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsm2encrypt.cpp
181 lines (149 loc) · 4.81 KB
/
sm2encrypt.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include "sm2encrypt.h"
#include "sm2.h"
#include "ui_sm2encrypt.h"
Sm2Encrypt::Sm2Encrypt(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Sm2Encrypt)
{
ui->setupUi(this);
}
Sm2Encrypt::~Sm2Encrypt()
{
delete ui;
}
void Sm2Encrypt::on_pushButtonEncrypt_clicked()
{
QString input = this->ui->textEditPlain->toPlainText();
QString pubQstrInput = this->ui->plainTextEditPub->toPlainText();
EVP_PKEY_CTX *encctx = NULL;
EVP_PKEY *pkey = NULL;
size_t outlen;
std::vector<unsigned char> buf;
std::vector<char> str;
if (pubQstrInput.isEmpty()) {
QMessageBox::warning(NULL,
"warning",
QString("请输入公钥!"),
QMessageBox::Close,
QMessageBox::Close);
return;
}
if (input.isEmpty()) {
QMessageBox::warning(NULL,
"warning",
QString("请输入明文!"),
QMessageBox::Close,
QMessageBox::Close);
return;
}
pkey = sm2_key_new_from_raw_pub(pubQstrInput.toStdString());
if (pkey == NULL) {
printTSError();
goto end;
}
encctx = EVP_PKEY_CTX_new_from_pkey(NULL, pkey, NULL);
if (encctx == NULL)
goto end;
if (EVP_PKEY_encrypt_init(encctx) <= 0
|| EVP_PKEY_encrypt(encctx,
NULL,
&outlen,
(const unsigned char *) input.toStdString().c_str(),
input.toStdString().length())
<= 0)
goto end;
buf.clear();
buf.reserve(outlen);
if (EVP_PKEY_encrypt(encctx,
buf.data(),
&outlen,
(const unsigned char *) input.toStdString().c_str(),
input.toStdString().length())
<= 0)
goto end;
str.reserve(outlen * 2 + 1);
if (OPENSSL_buf2hexstr_ex(str.data(), str.capacity(), NULL, buf.data(), outlen, '\0') != 1) {
printTSError();
goto end;
}
this->ui->textEditCipher->setText(QString::fromStdString(std::string(str.data(), outlen * 2)));
end:
EVP_PKEY_free(pkey);
}
void Sm2Encrypt::on_pushButtonDecrypt_clicked()
{
QString input = this->ui->textEditCipher->toPlainText();
QString pubQstrInput = this->ui->plainTextEditPub->toPlainText();
QString privQstrInput = this->ui->plainTextEditPriv->toPlainText();
EVP_PKEY_CTX *encctx = NULL;
EVP_PKEY *pkey = NULL;
size_t outlen;
std::vector<unsigned char> buf;
std::vector<char> str;
if (pubQstrInput.isEmpty() || privQstrInput.isEmpty()) {
QMessageBox::warning(NULL,
"warning",
QString("请输入公钥和私钥!"),
QMessageBox::Close,
QMessageBox::Close);
return;
}
if (input.isEmpty()) {
QMessageBox::warning(NULL,
"warning",
QString("请输入密文!"),
QMessageBox::Close,
QMessageBox::Close);
return;
}
pkey = sm2_key_new_from_raw_pub_and_priv(pubQstrInput.toStdString(),
privQstrInput.toStdString());
if (pkey == NULL) {
printTSError();
goto end;
}
encctx = EVP_PKEY_CTX_new_from_pkey(NULL, pkey, NULL);
if (encctx == NULL)
goto end;
buf.clear();
buf.reserve(input.length() / 2);
if (OPENSSL_hexstr2buf_ex(buf.data(), buf.capacity(), NULL, input.toStdString().c_str(), '\0')
!= 1) {
printTSError();
goto end;
}
if (EVP_PKEY_decrypt_init(encctx) <= 0
|| EVP_PKEY_decrypt(encctx, NULL, &outlen, buf.data(), buf.capacity()) <= 0)
goto end;
str.clear();
str.reserve(outlen);
if (EVP_PKEY_decrypt(encctx, (unsigned char *) str.data(), &outlen, buf.data(), buf.capacity())
<= 0)
goto end;
this->ui->textEditPlain->setText(QString::fromStdString(std::string(str.data(), outlen)));
end:
EVP_PKEY_free(pkey);
}
void Sm2Encrypt::on_pushButtonGen_clicked()
{
EVP_PKEY *pkey = NULL;
std::string hex;
pkey = EVP_PKEY_Q_keygen(NULL, NULL, "SM2");
if (pkey == NULL) {
printTSError();
goto end;
}
if (!sm2_key_get_pub_hex(pkey, hex)) {
printTSError();
goto end;
}
this->ui->plainTextEditPub->setPlainText(QString::fromStdString(hex));
if (!sm2_key_get_priv_hex(pkey, hex)) {
printTSError();
goto end;
}
this->ui->plainTextEditPriv->setPlainText(QString::fromStdString(hex));
end:
EVP_PKEY_free(pkey);
return;
}