Repository navigation
Expand file tree
/
Copy pathApp.tsx
More file actions
292 lines (275 loc) · 13.8 KB
/
Copy pathApp.tsx
File metadata and controls
292 lines (275 loc) · 13.8 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import { useState } from "react";
import Button from '@mui/material/Button';
import TextField from '@mui/material/TextField';
import Alert from '@mui/material/Alert';
import "./App.css";
import { SM2 } from 'gmlog'
interface KeyPair {
privateKey: bigint | undefined
publicKey: { x: undefined | bigint, y: undefined | bigint }
}
function App() {
// 初始化
const [isShowAlert, setIsShowAlert] = useState(false)
const [isShowSuccess, setIsShowSuccess] = useState(false)
const [isShowError, setIsShowError] = useState(false)
const [p, setP] = useState(0x8542d69e4c044f18e8b92435bf6ff7de457283915c45517d722edb8b08f1dfc3n)
const [a, setA] = useState(0x787968b4fa32c3fd2417842e73bbfeff2f3c848b6831d7e0ec65228b3937e498n)
const [b, setB] = useState(0x63e4c6d3b23b0c849cf84241484bfe48f61d59a5b16ba06e6e12d1da27c5249an)
const [n, setN] = useState(0x8542d69e4c044f18e8b92435bf6ff7dd297720630485628d5ae74ee7c32e79b7n)
const [G, setG] = useState([0x421debd61b62eab6746434ebc3cc315e32220b3badd50bdc4c4e6c147fedd43dn, 0x0680512bcbb42c07d47349d2153b70c4e5d7fdfcbfa36ea1a85841b9e46e09a2n])
const [h, setH] = useState(1n)
const [IDA, setIDA] = useState('ALICE123@YAHOO.COM')
const [IDB, setIDB] = useState('BILL456@YAHOO.COM')
const option = false;
const curveParam: any[] = [
p,
a,
b,
n,
G,
h,
];
const sm = new SM2(...curveParam);
// a的正式密钥
const [smAKey, setSmAKey] = useState<KeyPair>({
publicKey: { x: undefined, y: undefined },
privateKey: undefined,
});
// b的正式密钥
const [smBKey, setSmBKey] = useState<KeyPair>({
publicKey: { x: 0n, y: 0n },
privateKey: 0n,
});
// a的临时密钥
const [rKeyA, setRKeyA] = useState<KeyPair>({
publicKey: { x: undefined, y: undefined },
privateKey: undefined,
});
// const rKeyA = sm.agreement_initiate();
// console.log('😀rKeyA:', rKeyA);
// b的临时密钥
const [rKeyB, setRKeyB] = useState<KeyPair>({
publicKey: { x: undefined, y: undefined },
privateKey: undefined,
});
const generateKeyTmpB = () => {
setIsShowSuccess(false)
setIsShowError(false)
const smb_lin=sm.agreement_initiate()
// const smb_lin = {
// privateKey: 0x33FE21940342161C55619C4A0C060293D543C80AF19748CE176D83477DE71C80n,
// publicKey: {
// x: 0x1799B2A2C778295300D9A2325C686129B8F2B5337B3DCF4514E8BBC19D900EE5n,
// y:0x54C9288C82733EFDF7808AE7F27D0E732F7C73A7D9AC98B7D8740A91D0DB3CF4n,
// }
// }
setRKeyB(smb_lin)
}
// const [RB, setRB] = useState<any[]>([undefined])
const [KB, setKB] = useState<string>('')
const [KA, setKA] = useState<string>('')
const keyAgreement = () => {
setIsShowError(false)
setIsShowSuccess(false)
const { publicKey: publicKeyA, privateKey: privateKeyA } = smAKey;
const { publicKey: publicKeyB, privateKey: privateKeyB } = smBKey;
const smA = new SM2(...curveParam, IDA as any, privateKeyA, publicKeyA as any);
const smB = new SM2(...curveParam, IDB as any, privateKeyB, publicKeyB as any);
if (rKeyA.privateKey === undefined || rKeyB.privateKey === undefined || smAKey.privateKey === undefined || smBKey.privateKey === undefined) {
console.log('fail')
setIsShowAlert(true)
return
// generateKeyA();
// generateKeyB();
// generateKeyTmpA();
// generateKeyTmpB();
}
setIsShowAlert(false)
const { res, content } = smB.agreement_response(
rKeyA.publicKey as { x: bigint, y: bigint },
publicKeyA as { x: bigint, y: bigint },
IDA,
option,
rKeyB.privateKey,
rKeyB.publicKey as { x: bigint, y: bigint }
);
console.log('😉res, content:', res, content);
let RB = null,
KB = null,
SB = null,
S2 = null;
if (option) {
RB = content[0];
KB = content[1];
SB = content[2];
S2 = content[3];
} else {
RB = content[0];
KB = content[1];
SB = null;
}
setKB(KB as string)
const { res: res1, content: content1 } = smA.agreement_confirm(
rKeyA.privateKey as bigint,
rKeyA.publicKey as { x: bigint, y: bigint },
rKeyB.publicKey as { x: bigint, y: bigint },
publicKeyB as { x: bigint, y: bigint },
IDB,
null,
option
);
let KA = null,
SA = null;
if (option) {
KA = content1[0];
SA = content1[1];
} else {
KA = content1[0];
}
setKA(KA)
if (KB == KA) {
console.log('😎agreement success');
setIsShowSuccess(true)
} else {
setIsShowError(true)
console.log('😎agreement fail');
}
}
// console.log('😎...curveParam:', ...curveParam)
const generateKeyA = () => {
setIsShowError(false)
setIsShowSuccess(false)
const sma_zheng=sm.genKeyPaire()
// console.log('😎sma_zheng:', sma_zheng);
// const sma_zheng = {
// privateKey:0x6FCBA2EF9AE0AB902BC3BDE3FF915D44BA4CC78F88E2F8E7F8996D3B8CCEEDEEn,
// publicKey:{
// x:0x3099093BF3C137D8FCBBCDF4A2AE50F3B0F216C3122D79425FE03A45DBFE1655n,
// y:0x3DF79E8DAC1CF0ECBAA2F2B49D51A4B387F2EFAF482339086A27A8E05BAED98Bn,
// }
// }
setSmAKey(sma_zheng)
}
const generateKeyB = () => {
setIsShowError(false)
setIsShowSuccess(false)
const smb_zheng = sm.genKeyPaire()
// const smb_zheng = {
// privateKey: 0x5E35D7D3F3C54DBAC72E61819E730B019A84208CA3A35E4C2E353DFCCB2A3B53n,
// publicKey: {
// x: 0x245493D446C38D8CC0F118374690E7DF633A8A4BFB3329B5ECE604B2B4F37F43n,
// y:0x53C0869F4B9E17773DE68FEC45E14904E0DEA45BF6CECF9918C85EA047C60A4Cn,
// }
// }
setSmBKey(smb_zheng)
}
const generateKeyTmpA = () => {
setIsShowSuccess(false)
setIsShowError(false)
const sma_lin = sm.agreement_initiate()
// console.log("code line-174 \n\r😐 sma_lin:\n\r",sma_lin);
// const sma_lin={
// privateKey: 0x83A2C9C8B96E5AF70BD480B472409A9A327257F1EBB73F5B073354B248668563n,
// publicKey: {
// x: 0x6CB5633816F4DD560B1DEC458310CBCC6856C09505324A6D23150C408F162BF0n,
// y:0x0D6FCF62F1036C0A1B6DACCF57399223A65F7D7BF2D9637E5BBBEB857961BF1An,
// }
// }
setRKeyA(sma_lin)
}
const generateAllkey = () => {
generateKeyA();
generateKeyB();
generateKeyTmpA();
generateKeyTmpB()
}
return (
<div className="App w-full">
{isShowAlert ?
<div className="absolute w-full -mt-10">
<Alert severity="error" >不存在密钥参数,请检查</Alert>
</div> : null}
{isShowSuccess ?
<div className="absolute w-full -mt-10">
<Alert severity="success">密钥协商成功</Alert>
</div> : null}
{isShowError ?
<div className="absolute w-full -mt-10">
<Alert severity="error">密钥协商失败</Alert>
</div> : null}
<div className="w-full flex gap-3">
<div className="flex flex-col gap-2 w-99 border-1 border-solid p-3 border-blue-300">
<h4 className="p-0 m-0">曲线参数</h4>
<TextField id="outlined-basic" label="p" variant="outlined" size="small" multiline
rows={2} value={p.toString(16)} />
<TextField id="outlined-basic" label="a" variant="outlined" size="small" multiline
rows={2} value={a.toString(16)} />
<TextField id="outlined-basic" label="b" variant="outlined" size="small" multiline
rows={2} value={b.toString(16)} />
<TextField id="outlined-basic" label="n" variant="outlined" size="small" multiline
rows={2} value={n.toString(16)} />
<TextField id="outlined-basic" label="Gx" variant="outlined" size="small" multiline
rows={2} value={G[0].toString(16)} />
<TextField id="outlined-basic" label="Gy" variant="outlined" size="small" multiline
rows={2} value={G[1].toString(16)} />
<TextField id="outlined-basic" label="IDA" variant="outlined" size="small" defaultValue={IDA} onChange={(event) => setIDA(event.target.value)} />
<TextField id="outlined-basic" label="IDB" variant="outlined" size="small" defaultValue={IDB} onChange={(event) => setIDB(event.target.value)} />
</div>
<div className="flex flex-col items-center gap-2 w-99 border-1 border-solid p-3 border-blue-300">
<h4 className="p-0 m-0">密钥</h4>
<div className="flex flex-col gap-2 w-88 border-1 border-solid p-3 border-green-300">
<h5 className="p-0 m-0">密钥A</h5>
<TextField id="outlined-basic" label="A's private key" variant="outlined" size="small" multiline
rows={2} value={smAKey.privateKey ? smAKey.privateKey.toString(16) : ''} />
<TextField id="outlined-basic" label="A's publick key-x" variant="outlined" size="small" multiline
rows={2} value={smAKey.publicKey.x ? smAKey.publicKey.x.toString(16) : ''} />
<TextField id="outlined-basic" label="A's publick key-y" variant="outlined" size="small" multiline
rows={2} value={smAKey.publicKey.y ? smAKey.publicKey.y.toString(16) : ''} />
<Button onClick={generateKeyA} variant="contained">A密钥生成</Button>
</div>
<div className="flex flex-col gap-2 w-88 border-1 border-solid p-3 border-green-300">
<h5 className="p-0 m-0">密钥B</h5>
<TextField id="outlined-basic" label="B's private key" variant="outlined" size="small" multiline
rows={2} value={smBKey.privateKey ? smBKey.privateKey.toString(16) : ''} />
<TextField id="outlined-basic" label="B's publick key-x" variant="outlined" size="small" multiline
rows={2} value={smBKey.publicKey.x ? smBKey.publicKey.x.toString(16) : ''} />
<TextField id="outlined-basic" label="B's publick key-y" variant="outlined" size="small" multiline
rows={2} value={smBKey.publicKey.y ? smBKey.publicKey.y.toString(16) : ''} />
<Button onClick={generateKeyB} variant="contained">B密钥生成</Button>
</div>
</div>
<div className="flex flex-col items-center gap-2 w-99 border-1 border-solid p-3 border-blue-300">
<h4 className="p-0 m-0">临时密钥</h4>
<div className="flex flex-col gap-2 w-88 border-1 border-solid p-3 border-green-300">
<h5 className="p-0 m-0">临时密钥A</h5>
<TextField id="outlined-basic" label="A's private key" variant="outlined" size="small" multiline
rows={2} value={rKeyA.privateKey ? rKeyA.privateKey.toString(16) : ''} />
<TextField id="outlined-basic" label="A's publick key-x" variant="outlined" size="small" multiline
rows={2} value={rKeyA.publicKey.x ? rKeyA.publicKey.x.toString(16) : ''} />
<TextField id="outlined-basic" label="A's publick key-y" variant="outlined" size="small" multiline
rows={2} value={rKeyA.publicKey.y ? rKeyA.publicKey.y.toString(16) : ''} />
<Button onClick={generateKeyTmpA} variant="contained">A临时密钥生成</Button>
</div>
<div className="flex flex-col gap-2 w-88 border-1 border-solid p-3 border-green-300">
<h5 className="p-0 m-0">临时密钥B</h5>
<TextField id="outlined-basic" label="B's private key" variant="outlined" size="small" multiline
rows={2} value={rKeyB.privateKey ? rKeyB.privateKey.toString(16) : ''} />
<TextField id="outlined-basic" label="B's publick key-x" variant="outlined" size="small" multiline
rows={2} value={rKeyB.publicKey.x ? rKeyB.publicKey.x.toString(16) : ''} />
<TextField id="outlined-basic" label="B's publick key-y" variant="outlined" size="small" multiline
rows={2} value={rKeyB.publicKey.y ? rKeyB.publicKey.y.toString(16) : ''} />
<Button onClick={generateKeyTmpB} variant="contained">B临时密钥生成</Button>
</div>
</div>
</div>
<div className="w-full flex gap-3 justify-center mt-10">
<Button onClick={generateAllkey} variant="contained">生成所有密钥</Button>
<Button onClick={keyAgreement} variant="contained">密钥协商</Button>
<TextField className="w-89" id="outlined-basic" label="KA" variant="outlined" size="small" value={KA} />
<TextField className="w-89" id="outlined-basic" label="KB" variant="outlined" size="small" value={KB} />
</div>
</div>
);
}
export default App;