1
//------------------------------------
2
// RSA Encoder
3
// 创建于 2004年10月11日
4
// 创建人 luoluo
5
// 说明 一个说明RSA原理的简单程序
6
// 达不到Encoder的要求,且由于Jscript
7
// 的整型精度不够,不适合做RSA加密解密
8
// 的程序
9
//------------------------------------
10
11
12
/////////////////////////////////////////////////////////////////
13
14
//------------------------------------
15
// 函数名: isNumber
16
// 参数: int n
17
// 返回值: boolean
18
// 作用: 判断变量是否是数字
19
//------------------------------------
20
function isNumber(n) {
21
return ! isNaN(n);
22
}
23
24
25
//------------------------------------
26
// 函数名: isInt
27
// 参数: int n
28
// 返回值: boolean
29
// 作用: 判断变量是否是整数
30
//------------------------------------
31
function isInt(n) {
32
if (! isNumber(n))
33
throw n + " is not a number";
34
35
var re = /\./i;
36
return (n.toString().search(re) == -1);
37
}
38
39
//------------------------------------
40
// 函数名: gcd
41
// 参数一: x int
42
// 参数二: y int
43
// 返回值: int
44
// 作用: 求两个数的最大公因数(公约数)
45
//------------------------------------
46
function gcd(x, y) {
47
// 验证参数的类型
48
if (! isInt(x))
49
throw x + " is not a integer";
50
if (! isInt(y))
51
throw y + " is not a integer";
52
53
x = parseInt(x);
54
y = parseInt(y);
55
56
var ret; // 存放返回值
57
58
// 取参数的绝对值
59
if (x < 0)
60
x = -x;
61
if (y < 0)
62
y = -y;
63
64
// 判断是否为0
65
if (! (x + y))
66
throw "x and y can't be zero";
67
68
// 计算最大公因数
69
ret = y;
70
71
while (x > 0) {
72
ret = x;
73
x = y % x;
74
y = ret;
75
}
76
77
// 返回
78
return ret;
79
}
80
81
//------------------------------------
82
// 函数名: isEven
83
// 参数: int n
84
// 返回值: boolean
85
// 作用: 判断变量是否是偶数
86
//------------------------------------
87
function isEven(n) {
88
if (! isInt(n))
89
throw n + " is not a integer";
90
91
return (n & 0x01 == 0);
92
}
93
94
//------------------------------------
95
// 函数名: isEven
96
// 参数: int n
97
// 返回值: boolean
98
// 作用: 判断变量是否是奇数
99
//------------------------------------
100
function isOdd(n) {
101
if (! isInt(n))
102
throw n + " is not ainteger";
103
104
return (n & 0x01 != 0);
105
}
106
107
//------------------------------------
108
// 函数名: inverse
109
// 参数: int u
110
// 参数: int v
111
// v * d = 1 (mod u)
112
// 返回值: int
113
// 作用: 返回v关于u的乘法逆元素
114
// 说明: 修改自课本上的程序
115
//------------------------------------
116
function inverse(u, v) {
117
if (! isInt(u))
118
throw u + " is not a integer";
119
if (! isInt(v))
120
throw v + " is not a integer";
121
122
u = parseInt(u);
123
v = parseInt(v);
124
125
var t1, t2, t3;
126
var u1, u2, u3;
127
128
if (isEven(u) && isEven(v))
129
return 0;
130
131
u1 = 1;
132
u2 = 0;
133
u3 = u;
134
t1 = v;
135
t2 = u - 1;
136
t3 = v;
137
138
do {
139
do {
140
if (isEven(u3)) {
141
if (isOdd(u1) || isOdd(u2)) {
142
u1 += v;
143
u2 += u;
144
}
145
146
u1 >>= 1;
147
u2 >>= 1;
148
u3 >>= 1;
149
}
150
151
if (isEven(t3) || u3 < t3) {
152
u1^=t1, t1^=u1, u1^=t1;
153
u2^=t2, t2^=u2, u2^=t2;
154
u3^=t3, t3^=u3, u3^=t3;
155
}
156
} while (isEven(u3));
157
158
while ((u1 < t1) || (u2 < t2)) {
159
u1 += v;
160
u2 += u;
161
}
162
163
u1 -= t1;
164
u2 -= t2;
165
u3 -= t3;
166
} while (t3 > 0);
167
168
while (u1 > v && u2 >= u) {
169
u1 -= v;
170
u2 -= u;
171
}
172
173
return (u - u2);
174
}
175
176
//------------------------------------
177
// 函数名: isPrime
178
// 参数: int n
179
// 返回值: boolean
180
// 作用: 判断变量是否是素数
181
//------------------------------------
182
function isPrime(n) {
183
if (! isInt(n))
184
throw n + " is not a integer";
185
186
var ret = true;
187
188
for (var i = 2; i <= n - 1; i ++) {
189
if (! (n % i))
190
{
191
ret = false;
192
break;
193
}
194
}
195
196
return ret;
197
}
198
199
//------------------------------------
200
// 函数名: randomPrime
201
// 参数: int n
202
// 返回值: int
203
// 作用: 产生随机十进制n位素数
204
//------------------------------------
205
function randomPrime(n) {
206
var x;
207
208
do {
209
x = Math.random();
210
x = parseInt(x * Math.pow(10, n));
211
} while (! isPrime(x) || x.toString().length != n);
212
213
return x;
214
}
215
216
//------------------------------------
217
// 函数名: randomPrimeOfX
218
// 参数: int n
219
// 返回值: int
220
// 作用: 产生随机十进制n位与X互素的数
221
//------------------------------------
222
function randomPrimeOfX(x, n) {
223
var y;
224
225
do {
226
y = Math.random();
227
y = parseInt(y * Math.pow(10, n));
228
} while (gcd(x, y) != 1 || y.toString().length != n);
229
230
return y;
231
}
232
233
234
//------------------------------------
235
// 类名: RSAEncoder
236
// 作用: RSA加密解密
237
//------------------------------------
238
function RSAEncoder() {
239
this.p = randomPrime(2);
240
this.q = randomPrime(2);
241
242
this.n = 0;
243
this.e = 0;
244
this.d = 0;
245
246
this.init = init;
247
248
this.encode = encode;
249
250
this.decode = decode;
251
}
252
253
//------------------------------------
254
// 函数名: init
255
// 作用: RSAEncoder初始化
256
//------------------------------------
257
function init() {
258
this.n = this.p * this.q;
259
this.e = randomPrimeOfX((this.p - 1) * (this.q - 1), 2);
260
this.d = inverse((this.p - 1) * (this.q - 1), this.e);
261
}
262
263
//------------------------------------
264
// 函数名: encode
265
// 参数: int m
266
// 返回值: int
267
// 作用: 加密信息
268
//------------------------------------
269
function encode(m) {
270
return (Math.pow(m, this.e) % this.n);
271
}
272
273
274
//------------------------------------
275
// 函数名: decode
276
// 参数: int m
277
// 返回值: int
278
// 作用: 解密信息
279
//------------------------------------
280
function decode(c) {
281
return (Math.pow(c, this.d) % this.n);
282
}
283

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
