willyb321

encrypt

Jun 9th, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.   <title></title>
  5. </head>
  6. <body>
  7. <script>// use (16 chars of) 'password' to encrypt 'plaintext'
  8.  
  9. function encrypt(plaintext, password) {
  10.   var v = new Array(2), k = new Array(4), s = "", i;
  11.  
  12.   plaintext = escape(plaintext);  // use escape() so only have single-byte chars to encode
  13.  
  14.   // build key directly from 1st 16 chars of password
  15.   for (var i=0; i<4; i++) k[i] = Str4ToLong(password.slice(i*4,(i+1)*4));
  16.  
  17.   for (i=0; i<plaintext.length; i+=8) {  // encode plaintext into s in 64-bit (8 char) blocks
  18.     v[0] = Str4ToLong(plaintext.slice(i,i+4));  // ... note this is 'electronic codebook' mode
  19.     v[1] = Str4ToLong(plaintext.slice(i+4,i+8));
  20.     code(v, k);
  21.     s += LongToStr4(v[0]) + LongToStr4(v[1]);
  22.   }
  23.  
  24.   return escCtrlCh(s);
  25.   // note: if plaintext or password are passed as string objects, rather than strings, this
  26.   // function will throw an 'Object doesn't support this property or method' error
  27. }
  28.  
  29. // use (16 chars of) 'password' to decrypt 'ciphertext' with xTEA
  30.  
  31. function decrypt(ciphertext, password) {
  32.   var v = new Array(2), k = new Array(4), s = "", i;
  33.  
  34.   for (var i=0; i<4; i++) k[i] = Str4ToLong(password.slice(i*4,(i+1)*4));
  35.  
  36.   ciphertext = unescCtrlCh(ciphertext);
  37.   for (i=0; i<ciphertext.length; i+=8) {  // decode ciphertext into s in 64-bit (8 char) blocks
  38.     v[0] = Str4ToLong(ciphertext.slice(i,i+4));
  39.     v[1] = Str4ToLong(ciphertext.slice(i+4,i+8));
  40.     decode(v, k);
  41.     s += LongToStr4(v[0]) + LongToStr4(v[1]);
  42.   }
  43.  
  44.   // strip trailing null chars resulting from filling 4-char blocks:
  45.   s = s.replace(/\0+$/, '');
  46.  
  47.   return unescape(s);
  48. }
  49.  
  50.  
  51. function code(v, k) {
  52.   // Extended TEA: this is the 1997 revised version of Needham & Wheeler's algorithm
  53.   // params: v[2] 64-bit value block; k[4] 128-bit key
  54.   var y = v[0], z = v[1];
  55.   var delta = 0x9E3779B9, limit = delta*32, sum = 0;
  56.  
  57.   while (sum != limit) {
  58.     y += (z<<4 ^ z>>>5)+z ^ sum+k[sum & 3];
  59.     sum += delta;
  60.     z += (y<<4 ^ y>>>5)+y ^ sum+k[sum>>>11 & 3];
  61.     // note: unsigned right-shift '>>>' is used in place of original '>>', due to lack
  62.     // of 'unsigned' type declaration in JavaScript (thanks to Karsten Kraus for this)
  63.   }
  64.   v[0] = y; v[1] = z;
  65. }
  66.  
  67. function decode(v, k) {
  68.   var y = v[0], z = v[1];
  69.   var delta = 0x9E3779B9, sum = delta*32;
  70.  
  71.   while (sum != 0) {
  72.     z -= (y<<4 ^ y>>>5)+y ^ sum+k[sum>>>11 & 3];
  73.     sum -= delta;
  74.     y -= (z<<4 ^ z>>>5)+z ^ sum+k[sum & 3];
  75.   }
  76.   v[0] = y; v[1] = z;
  77. }
  78.  
  79.  
  80. // supporting functions
  81.  
  82. function Str4ToLong(s) {  // convert 4 chars of s to a numeric long
  83.   var v = 0;
  84.   for (var i=0; i<4; i++) v |= s.charCodeAt(i) << i*8;
  85.   return isNaN(v) ? 0 : v;
  86. }
  87.  
  88. function LongToStr4(v) {  // convert a numeric long to 4 char string
  89.   var s = String.fromCharCode(v & 0xFF, v>>8 & 0xFF, v>>16 & 0xFF, v>>24 & 0xFF);
  90.   return s;
  91. }
  92.  
  93. function escCtrlCh(str) {  // escape control chars which might cause problems with encrypted texts
  94.   return str.replace(/[\0\t\n\v\f\r\xa0'"!]/g, function(c) { return '!' + c.charCodeAt(0) + '!'; });
  95. }
  96.  
  97. function unescCtrlCh(str) {  // unescape potentially problematic nulls and control characters
  98.   return str.replace(/!\d\d?\d?!/g, function(c) { return String.fromCharCode(c.slice(1,-1)); });
  99. }</script>
  100. </body>
  101. </html>
Advertisement
Add Comment
Please, Sign In to add comment