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
/
oXxtea.js
157 lines (125 loc) · 4.05 KB
/
oXxtea.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
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
(function (thiz) {
var O = thiz.O;
O.__.xxTEA = {
//
// 'Block' Tiny Encryption Algorithm xxtea
// (c) 2002-2006 Chris Veness <[email protected]>
//
// Algorithm: David Wheeler & Roger Needham, Cambridge University Computer Lab
// http://www.cl.cam.ac.uk/ftp/papers/djw-rmn/djw-rmn-tea.html (1994)
// http://www.cl.cam.ac.uk/ftp/users/djw3/xtea.ps (1997)
// http://www.cl.cam.ac.uk/ftp/users/djw3/xxtea.ps (1998)
//
// JavaScript implementation: Chris Veness, Movable Type Ltd: www.movable-type.co.uk
// http://www.movable-type.co.uk/scripts/TEAblock.html
//
// You are welcome to re-use these scripts [without any warranty express or implied] provided
// you retain my copyright notice and when possible a link to my website (under LGPL license).
// If you have any queries or find any problems, please contact Chris Veness.
//
//
//
// adapted for O by Francesco Sullo
teaencrypt: function (plaintext, key) {
if (plaintext.length == 0) return('');
var v = this.strToLongs(escape(plaintext).replace(/%20/g,' '));
if (v.length <= 1) v[1] = 0;
var k = this.strToLongs(key.slice(0,16)),
n = v.length,
z = v[n-1],
y = v[0],
delta = 0x9E3779B9,
mx,
e,
q = Math.floor(6 + 52/n),
sum = 0;
while (q-- > 0) {
sum += delta;
e = sum>>>2 & 3;
for (var p = 0; p < n; p++) {
y = v[(p+1) % n];
mx = (z>>>5 ^ y<<2) + (y>>>3 ^ z<<4) ^ (sum^y) + (k[p&3 ^ e] ^ z);
z = v[p] += mx;
}
}
return this.escCtrlCh(this.longsToStr(v));
},
teadecrypt: function (ciphertext, key) {
if (ciphertext.length == 0) return('');
var k = this.strToLongs(key.slice(0,16)),
v = this.strToLongs(this.unescCtrlCh(ciphertext)),
n = v.length;
var z = v[n-1], y = v[0], delta = 0x9E3779B9;
var mx, e, q = Math.floor(6 + 52/n), sum = q*delta;
while (sum != 0) {
e = sum>>>2 & 3;
for (var p = n-1; p >= 0; p--) {
z = v[p>0 ? p-1 : n-1];
mx = (z>>>5 ^ y<<2) + (y>>>3 ^ z<<4) ^ (sum^y) + (k[p&3 ^ e] ^ z);
y = v[p] -= mx;
}
sum -= delta;
}
return unescape(this.longsToStr(v).replace(/\0+$/,''));
},
strToLongs: function (s) {
var ll = Math.ceil(s.length/4);
var l = new Array(ll);
for (var i=0; i<ll; i++) {
l[i] = s.charCodeAt(i*4)
+ (s.charCodeAt(i*4+1)<<8)
+ (s.charCodeAt(i*4+2)<<16)
+ (s.charCodeAt(i*4+3)<<24);
}
return l;
},
longsToStr: function (l) {
var a = new Array(l.length);
for (var i=0; i<l.length; i++) {
a[i] = String.fromCharCode(
l[i] & 0xFF,
l[i]>>>8 & 0xFF,
l[i]>>>16 & 0xFF,
l[i]>>>24 & 0xFF
);
}
return a.join('');
},
escCtrlCh: function (str) { // escape control chars etc which might cause problems with encrypted texts
return str.replace(/[\0\t\n\v\f\r\xa0'"!]/g, function(c) { return '!' + c.charCodeAt(0) + '!'; });
},
unescCtrlCh: function (str) { // unescape potentially problematic nulls and control characters
return str.replace(/!\d\d?\d?!/g, function(c) { return String.fromCharCode(c.slice(1,-1)); });
}
};
O.expand({
fromTEA: function (str,key) {
return O.__.xxTEA.teadecrypt(
str,
key
);
},
toTEA: function (str,key) {
return O.__.xxTEA.teaencrypt(
str,
key
);
}
});
O.extend({
fromTEA: function (key) {
this.O = O.__.xxTEA.teadecrypt(
this.O.toString(),
key
);
return this;
},
toTEA: function (key) {
this.O = O.__.xxTEA.teaencrypt(
this.O.toString(),
key
);
return this;
}
});
})(this);