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
|
/*
Copyright (c) 2004-2011, The Dojo Foundation All Rights Reserved.
Available via Academic Free License >= 2.1 OR the modified BSD license.
see: http://dojotoolkit.org/license for details
*/
if(!dojo._hasResource["dojox.encoding.crypto.RSAKey-ext"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
dojo._hasResource["dojox.encoding.crypto.RSAKey-ext"] = true;
dojo.provide("dojox.encoding.crypto.RSAKey-ext");
dojo.require("dojox.encoding.crypto.RSAKey");
dojo.require("dojox.math.BigInteger-ext");
dojo.experimental("dojox.encoding.crypto.RSAKey-ext");
(function(){
var BigInteger = dojox.math.BigInteger;
// Undo PKCS#1 (type 2, random) padding and, if valid, return the plaintext
function pkcs1unpad2(d, n){
var b = d.toByteArray();
for(var i = 0, len = b.length; i < len && !b[i]; ++i);
if(b.length - i !== n - 1 || b[i] !== 2){
return null;
}
for(++i; b[i];){
if(++i >= len){
return null;
}
}
var ret = "";
while(++i < len){
ret += String.fromCharCode(b[i]);
}
return ret;
}
dojo.extend(dojox.encoding.crypto.RSAKey, {
setPrivate: function(N, E, D){
// summary:
// Set the private key fields N, e, d and CRT params from hex strings
if(N && E && N.length && E.length){
this.n = new BigInteger(N, 16);
this.e = parseInt(E, 16);
this.d = new BigInteger(D, 16);
}else{
throw new Error("Invalid RSA private key");
}
},
setPrivateEx: function(N, E, D, P, Q, DP, DQ, C) {
// summary:
// Set the private key fields N, e, d and CRT params from hex strings
if(N && E && N.length && E.length){
this.n = new BigInteger(N, 16);
this.e = parseInt(E, 16);
this.d = new BigInteger(D, 16);
this.p = new BigInteger(P, 16);
this.q = new BigInteger(Q, 16);
this.dmp1 = new BigInteger(DP, 16);
this.dmq1 = new BigInteger(DQ, 16);
this.coeff = new BigInteger(C, 16);
}else{
throw new Error("Invalid RSA private key");
}
},
generate: function(B, E){
// summary:
// Generate a new random private key B bits long, using public expt E
var rng = this.rngf(), qs = B >> 1;
this.e = parseInt(E, 16);
var ee = new BigInteger(E, 16);
for(;;) {
for(;;) {
this.p = new BigInteger(B - qs, 1, rng);
if(!this.p.subtract(BigInteger.ONE).gcd(ee).compareTo(BigInteger.ONE) && this.p.isProbablePrime(10)){
break;
}
}
for(;;) {
this.q = new BigInteger(qs, 1, rng);
if(!this.q.subtract(BigInteger.ONE).gcd(ee).compareTo(BigInteger.ONE) && this.q.isProbablePrime(10)){
break;
}
}
if(this.p.compareTo(this.q) <= 0) {
var t = this.p;
this.p = this.q;
this.q = t;
}
var p1 = this.p.subtract(BigInteger.ONE);
var q1 = this.q.subtract(BigInteger.ONE);
var phi = p1.multiply(q1);
if(!phi.gcd(ee).compareTo(BigInteger.ONE)) {
this.n = this.p.multiply(this.q);
this.d = ee.modInverse(phi);
this.dmp1 = this.d.mod(p1);
this.dmq1 = this.d.mod(q1);
this.coeff = this.q.modInverse(this.p);
break;
}
}
rng.destroy();
},
decrypt: function(ctext){
// summary:
// Return the PKCS#1 RSA decryption of "ctext".
// ctext: String:
// an even-length hex string
// returns: a plain string.
var c = new BigInteger(ctext, 16), m;
if(!this.p || !this.q){
m = c.modPow(this.d, this.n);
}else{
// TODO: re-calculate any missing CRT params
var cp = c.mod(this.p).modPow(this.dmp1, this.p),
cq = c.mod(this.q).modPow(this.dmq1, this.q);
while(cp.compareTo(cq) < 0){
cp = cp.add(this.p);
}
m = cp.subtract(cq).multiply(this.coeff).mod(this.p).multiply(this.q).add(cq);
}
if(!m){
return null;
}
return pkcs1unpad2(m, (this.n.bitLength() + 7) >> 3);
}
});
})();
}
|