This repository has been archived by the owner on Nov 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
oRc4.js
80 lines (55 loc) · 1.45 KB
/
oRc4.js
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
(function (thiz) {
var O = thiz.O;
O.__.RC4 = {
// adapted for O by Francesco Sullo
endecrypt: function (txt,key,N,dval) {
function exc(v,a,b) {
var t = v[a];
v[a] = v[b];
v[b] = t;
}
var num = [], sbox = [], b=0;
for (var i=0;i<=255;i++) {
sbox[i] = i;
num[i] = key.charCodeAt(i % key.length);
}
// improvement (with some bugs - for example with N=5
for (var u=0;u<N;u++) {
for (var i=0;i<=255;i++) {
b = (b + sbox[i] + num[i]) % 256;
exc(sbox,i,b);
}
}
//
var k=0, j=0, ret = "", val;
for (var i=0;i<txt.length;i++) {
k = (k + 1) % 256;
j = (j + sbox[k]) % 256;
exc(sbox,k,j);
val = txt.charCodeAt(i) ^ sbox[(sbox[k] + sbox[j]) % 256];
// dval produces not-reversible numeric strings:
ret += dval ? val : String.fromCharCode(val);
}
return ret;
}
};
O.expand({
toFromRC4: function (str,key,rounds) {
return O.__.RC4.endecrypt(
str,
key,
rounds ? rounds : 1
);
}
});
O.extend({
toFromRC4: function (key,rounds) {
this.O = O.toFromRC4(
this.O.toString(),
key,
rounds
);
return this;
}
});
})(this);