Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.94 KB | None | 0 0
  1. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  2. /* Block TEA (xxtea) Tiny Encryption Algorithm implementation in JavaScript */
  3. /* (c) Chris Veness 2002-2012: www.movable-type.co.uk/tea-block.html */
  4. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  5.  
  6. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  7. /* Algorithm: David Wheeler & Roger Needham, Cambridge University Computer Lab */
  8. /* http://www.cl.cam.ac.uk/ftp/papers/djw-rmn/djw-rmn-tea.html (1994) */
  9. /* http://www.cl.cam.ac.uk/ftp/users/djw3/xtea.ps (1997) */
  10. /* http://www.cl.cam.ac.uk/ftp/users/djw3/xxtea.ps (1998) */
  11. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  12.  
  13. var Tea = {}; // Tea namespace
  14. /*
  15. * encrypt text using Corrected Block TEA (xxtea) algorithm
  16. *
  17. * @param {string} plaintext String to be encrypted (multi-byte safe)
  18. * @param {string} password Password to be used for encryption (1st 16 chars)
  19. * @returns {string} encrypted text
  20. */
  21. Tea.encrypt = function(plaintext, password) {
  22. if (plaintext.length == 0) return (''); // nothing to encrypt
  23. // convert string to array of longs after converting any multi-byte chars to UTF-8
  24. var v = Tea.strToLongs(Utf8.encode(plaintext));
  25. if (v.length <= 1) v[1] = 0; // algorithm doesn't work for n<2 so fudge by adding a null
  26. // simply convert first 16 chars of password as key
  27. var k = Tea.strToLongs(Utf8.encode(password).slice(0, 16));
  28. var n = v.length;
  29.  
  30. // ---- <TEA coding> ----
  31. var z = v[n - 1],
  32. y = v[0],
  33. delta = 0x9E3779B9;
  34. var mx, e, q = Math.floor(6 + 52 / n),
  35. sum = 0;
  36.  
  37. while (q-- > 0) { // 6 + 52/n operations gives between 6 & 32 mixes on each word
  38. sum += delta;
  39. e = sum >>> 2 & 3;
  40. for (var p = 0; p < n; p++) {
  41. y = v[(p + 1) % n];
  42. mx = (z >>> 5 ^ y << 2) + (y >>> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z);
  43. z = v[p] += mx;
  44. }
  45. }
  46.  
  47. // ---- </TEA> ----
  48. var ciphertext = Tea.longsToStr(v);
  49.  
  50. return Base64.encode(ciphertext);
  51. }
  52.  
  53. /*
  54. * decrypt text using Corrected Block TEA (xxtea) algorithm
  55. *
  56. * @param {string} ciphertext String to be decrypted
  57. * @param {string} password Password to be used for decryption (1st 16 chars)
  58. * @returns {string} decrypted text
  59. */
  60. Tea.decrypt = function(ciphertext, password) {
  61. if (ciphertext.length == 0) return ('');
  62. var v = Tea.strToLongs(Base64.decode(ciphertext));
  63. var k = Tea.strToLongs(Utf8.encode(password).slice(0, 16));
  64. var n = v.length;
  65.  
  66. // ---- <TEA decoding> ----
  67. var z = v[n - 1],
  68. y = v[0],
  69. delta = 0x9E3779B9;
  70. var mx, e, q = Math.floor(6 + 52 / n),
  71. sum = q * delta;
  72.  
  73. while (sum != 0) {
  74. e = sum >>> 2 & 3;
  75. for (var p = n - 1; p >= 0; p--) {
  76. z = v[p > 0 ? p - 1 : n - 1];
  77. mx = (z >>> 5 ^ y << 2) + (y >>> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z);
  78. y = v[p] -= mx;
  79. }
  80. sum -= delta;
  81. }
  82.  
  83. // ---- </TEA> ----
  84. var plaintext = Tea.longsToStr(v);
  85.  
  86. // strip trailing null chars resulting from filling 4-char blocks:
  87. plaintext = plaintext.replace(/\0+$/, '');
  88.  
  89. return Utf8.decode(plaintext);
  90. }
  91.  
  92. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  93.  
  94. // supporting functions
  95. Tea.strToLongs = function(s) { // convert string to array of longs, each containing 4 chars
  96. // note chars must be within ISO-8859-1 (with Unicode code-point < 256) to fit 4/long
  97. var l = new Array(Math.ceil(s.length / 4));
  98. for (var i = 0; i < l.length; i++) {
  99. // note little-endian encoding - endianness is irrelevant as long as
  100. // it is the same in longsToStr()
  101. l[i] = s.charCodeAt(i * 4) + (s.charCodeAt(i * 4 + 1) << 8) + (s.charCodeAt(i * 4 + 2) << 16) + (s.charCodeAt(i * 4 + 3) << 24);
  102. }
  103. return l; // note running off the end of the string generates nulls since
  104. } // bitwise operators treat NaN as 0
  105. Tea.longsToStr = function(l) { // convert array of longs back to string
  106. var a = new Array(l.length);
  107. for (var i = 0; i < l.length; i++) {
  108. a[i] = String.fromCharCode(l[i] & 0xFF, l[i] >>> 8 & 0xFF, l[i] >>> 16 & 0xFF, l[i] >>> 24 & 0xFF);
  109. }
  110. return a.join(''); // use Array.join() rather than repeated string appends for efficiency in IE
  111. }
  112.  
  113.  
  114. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  115. /* Base64 class: Base 64 encoding / decoding (c) Chris Veness 2002-2012 */
  116. /* note: depends on Utf8 class */
  117. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  118.  
  119. var Base64 = {}; // Base64 namespace
  120. Base64.code = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  121.  
  122. /**
  123. * Encode string into Base64, as defined by RFC 4648 [http://tools.ietf.org/html/rfc4648]
  124. * (instance method extending String object). As per RFC 4648, no newlines are added.
  125. *
  126. * @param {String} str The string to be encoded as base-64
  127. * @param {Boolean} [utf8encode=false] Flag to indicate whether str is Unicode string to be encoded
  128. * to UTF8 before conversion to base64; otherwise string is assumed to be 8-bit characters
  129. * @returns {String} Base64-encoded string
  130. */
  131. Base64.encode = function(str, utf8encode) { // http://tools.ietf.org/html/rfc4648
  132. utf8encode = (typeof utf8encode == 'undefined') ? false : utf8encode;
  133. var o1, o2, o3, bits, h1, h2, h3, h4, e = [],
  134. pad = '',
  135. c, plain, coded;
  136. var b64 = Base64.code;
  137.  
  138. plain = utf8encode ? Utf8.encode(str) : str;
  139.  
  140. c = plain.length % 3; // pad string to length of multiple of 3
  141. if (c > 0) {
  142. while (c++ < 3) {
  143. pad += '=';
  144. plain += '\0';
  145. }
  146. }
  147. // note: doing padding here saves us doing special-case packing for trailing 1 or 2 chars
  148. for (c = 0; c < plain.length; c += 3) { // pack three octets into four hexets
  149. o1 = plain.charCodeAt(c);
  150. o2 = plain.charCodeAt(c + 1);
  151. o3 = plain.charCodeAt(c + 2);
  152.  
  153. bits = o1 << 16 | o2 << 8 | o3;
  154.  
  155. h1 = bits >> 18 & 0x3f;
  156. h2 = bits >> 12 & 0x3f;
  157. h3 = bits >> 6 & 0x3f;
  158. h4 = bits & 0x3f;
  159.  
  160. // use hextets to index into code string
  161. e[c / 3] = b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4);
  162. }
  163. coded = e.join(''); // join() is far faster than repeated string concatenation in IE
  164. // replace 'A's from padded nulls with '='s
  165. coded = coded.slice(0, coded.length - pad.length) + pad;
  166.  
  167. return coded;
  168. }
  169.  
  170. /**
  171. * Decode string from Base64, as defined by RFC 4648 [http://tools.ietf.org/html/rfc4648]
  172. * (instance method extending String object). As per RFC 4648, newlines are not catered for.
  173. *
  174. * @param {String} str The string to be decoded from base-64
  175. * @param {Boolean} [utf8decode=false] Flag to indicate whether str is Unicode string to be decoded
  176. * from UTF8 after conversion from base64
  177. * @returns {String} decoded string
  178. */
  179. Base64.decode = function(str, utf8decode) {
  180. utf8decode = (typeof utf8decode == 'undefined') ? false : utf8decode;
  181. var o1, o2, o3, h1, h2, h3, h4, bits, d = [],
  182. plain, coded;
  183. var b64 = Base64.code;
  184.  
  185. coded = utf8decode ? Utf8.decode(str) : str;
  186.  
  187.  
  188. for (var c = 0; c < coded.length; c += 4) { // unpack four hexets into three octets
  189. h1 = b64.indexOf(coded.charAt(c));
  190. h2 = b64.indexOf(coded.charAt(c + 1));
  191. h3 = b64.indexOf(coded.charAt(c + 2));
  192. h4 = b64.indexOf(coded.charAt(c + 3));
  193.  
  194. bits = h1 << 18 | h2 << 12 | h3 << 6 | h4;
  195.  
  196. o1 = bits >>> 16 & 0xff;
  197. o2 = bits >>> 8 & 0xff;
  198. o3 = bits & 0xff;
  199.  
  200. d[c / 4] = String.fromCharCode(o1, o2, o3);
  201. // check for padding
  202. if (h4 == 0x40) d[c / 4] = String.fromCharCode(o1, o2);
  203. if (h3 == 0x40) d[c / 4] = String.fromCharCode(o1);
  204. }
  205. plain = d.join(''); // join() is far faster than repeated string concatenation in IE
  206. return utf8decode ? Utf8.decode(plain) : plain;
  207. }
  208.  
  209.  
  210. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  211. /* Utf8 class: encode / decode between multi-byte Unicode characters and UTF-8 multiple */
  212. /* single-byte character encoding (c) Chris Veness 2002-2012 */
  213. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  214.  
  215. var Utf8 = {}; // Utf8 namespace
  216. /**
  217. * Encode multi-byte Unicode string into utf-8 multiple single-byte characters
  218. * (BMP / basic multilingual plane only)
  219. *
  220. * Chars in range U+0080 - U+07FF are encoded in 2 chars, U+0800 - U+FFFF in 3 chars
  221. *
  222. * @param {String} strUni Unicode string to be encoded as UTF-8
  223. * @returns {String} encoded string
  224. */
  225. Utf8.encode = function(strUni) {
  226. // use regular expressions & String.replace callback function for better efficiency
  227. // than procedural approaches
  228. var strUtf = strUni.replace(/[\u0080-\u07ff]/g, // U+0080 - U+07FF => 2 bytes 110yyyyy, 10zzzzzz
  229.  
  230.  
  231. function(c) {
  232. var cc = c.charCodeAt(0);
  233. return String.fromCharCode(0xc0 | cc >> 6, 0x80 | cc & 0x3f);
  234. });
  235. strUtf = strUtf.replace(/[\u0800-\uffff]/g, // U+0800 - U+FFFF => 3 bytes 1110xxxx, 10yyyyyy, 10zzzzzz
  236.  
  237.  
  238. function(c) {
  239. var cc = c.charCodeAt(0);
  240. return String.fromCharCode(0xe0 | cc >> 12, 0x80 | cc >> 6 & 0x3F, 0x80 | cc & 0x3f);
  241. });
  242. return strUtf;
  243. }
  244.  
  245. /**
  246. * Decode utf-8 encoded string back into multi-byte Unicode characters
  247. *
  248. * @param {String} strUtf UTF-8 string to be decoded back to Unicode
  249. * @returns {String} decoded string
  250. */
  251. Utf8.decode = function(strUtf) {
  252. // note: decode 3-byte chars first as decoded 2-byte strings could appear to be 3-byte char!
  253. var strUni = strUtf.replace(/[\u00e0-\u00ef][\u0080-\u00bf][\u0080-\u00bf]/g, // 3-byte chars
  254.  
  255.  
  256. function(c) { // (note parentheses for precence)
  257. var cc = ((c.charCodeAt(0) & 0x0f) << 12) | ((c.charCodeAt(1) & 0x3f) << 6) | (c.charCodeAt(2) & 0x3f);
  258. return String.fromCharCode(cc);
  259. });
  260. strUni = strUni.replace(/[\u00c0-\u00df][\u0080-\u00bf]/g, // 2-byte chars
  261.  
  262.  
  263. function(c) { // (note parentheses for precence)
  264. var cc = (c.charCodeAt(0) & 0x1f) << 6 | c.charCodeAt(1) & 0x3f;
  265. return String.fromCharCode(cc);
  266. });
  267. return strUni;
  268. }
  269.  
  270. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement