-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrsa.js
More file actions
178 lines (165 loc) · 3.22 KB
/
rsa.js
File metadata and controls
178 lines (165 loc) · 3.22 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
const crypto = require("crypto")
function extendedEuclid(a, b) {
if (b === 0) {
return { a, x: 1, y: 0 }
} else {
let _ = extendedEuclid(b, a % b)
// console.log(_)
return {
a: _.a,
x: _.y,
y: _.x - parseInt(a/b)*_.y
}
}
}
var validPrimesSet = []
function genFakeRandPrime(bit = 1024) {
while (true) {
let n = BigInt(`0x${crypto.randomBytes(bit).toString("hex")}`)
// 构建检测用素数数组
if (validPrimesSet.length === 0) {
for (let i = 2; i < 2000; i++){
let isPrimes = true
for (let j = 2; j < i; j++) {
if (i % j === 0) {
isPrimes = false
break
}
}
if (isPrimes) {
validPrimesSet.push(BigInt(i))
}
}
}
let isPrimes = true
for (let p of validPrimesSet) {
if (n % p === 0) {
isPrimes = false
break
}
}
if (isPrimes) {
return n
}
}
}
/***
*
* @param n 一个大于3的奇整数
* @param t 一个大于等于1的安全参数(用于确定测试轮数)
*/
function checkByMillerRabin(n, t) {
let r = n - 1n
let s = 0
while (r % 2n == 0) {
r /= 2n
s++
}
for (let i = 0; i < t; i++) {
let a = genBigIntRand(2n, n - 2n) // 产生[2, n-2]的随机数
let y = modPow(a, r, n)
if (y !== 1 && y !== n - 1n) {
for (let j = 1; j < s && y !== n - 1n; j++) {
y = modPow(y, 2n, n)
if (y == 1) {
return false
}
}
if (y != n - 1n) {
return true
}
}
}
}
function genBigIntRand(min, max) {
let minStr = min.toString()
let maxStr = max.toString()
let minLen = minStr.length
let maxLen = maxStr.length
let len = rangeRand(minLen, maxLen)
let ret = ""
if (len === minLen || len === maxLen) {
let numStr = minStr
let numLen = minLen
if (len === maxLen) {
numStr = maxStr
numLen = maxLen
}
let randRng = true
for (let i = numLen - 1; i >= 0; i--) {
let low = randRng ? "0" : numStr[i]
ret += rangeRand(parseInt(low), 9).toString()
randRng = low !== numStr[i]
}
} else {
for (let i = len - 1; i >= 0; i--) {
ret += rangeRand(0, 9).toString()
}
}
return BigInt(ret)
}
function modPow(x, y, m) {
let ret = 1n
let b = x
while (y) {
if (y % 2n != 0) {
ret = ret * b
ret = ret % m
}
b = b * b
b = b % m
y = y / 2n
}
return ret
}
function rangeRand(min, max) {
return Math.ceil(Math.random() * (max - min + 1) + min) - 1
}
function getSmallOddAsPHIsCoPrime(phi) {
let e = 3n
for (; e < Number.MAX_VALUE; e += 2n) {
let isCoPrime = true
for (let i = e - 2n; i > 1; i -= 2n) {
if (e % i == 0 && phi % i == 0) {
isCoPrime = false
break
}
}
if (isCoPrime) {
break
}
}
return e
}
function exgcd(a, b, x, y) {
if (b == 0) {
x = 1n
y = 0n
return a
}
let q = exgcd(b, a%b, y, x)
y -= a/b * x
console.log(`x:${x}\ty:${y}`)
return q
}
// console.log(extendedEuclid(99, 78))
// let primes = genFakeRandPrime()
// console.log(primes)
// console.log(checkByMillerRabin(primes, 2000))
function rsaEncode() {
let p = null
do {
p = genFakeRandPrime()
} while (!checkByMillerRabin(p, 4))
let q = null
do {
q = genFakeRandPrime()
} while (p == q || !checkByMillerRabin(q, 4))
let n = p * q
let phi = (p - 1n) * (q - 1n)
let e = getSmallOddAsPHIsCoPrime(phi)
let d = BigInt(1)
exgcd(e, n, d, BigInt(1))
console.log(d)
}
rsaEncode()