Advertisement
rg443

javascript des

Jan 19th, 2013
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* des.js - Des library for JavaScript
  2.  *
  3.  * http://www.onicos.com/staff/iz/amuse/javascript/expert/des.txt
  4.  * LastModified: Jan  6 2004
  5.  *
  6.  *  This library is free.  You can redistribute it and/or modify it.
  7.  */
  8.  
  9. /*
  10.  * Interfaces:
  11.  * ev = des_encrypt(key, val);
  12.  * name = des_escape(ev);
  13.  * ev = des_unescape(name);
  14.  * val  = des_decrypt(key, ev);
  15.  *
  16.  * ev  = des_cbc_encrypt(key, str);
  17.  * str = des_cbc_decrypt(key, ev);
  18.  */
  19.  
  20. function des_encrypt(key, val) {
  21.     var i, isstr;
  22.     var out;
  23.     if(typeof key == "string")
  24.     key = des_str2arr(key);
  25.     if(typeof val == "string") {
  26.     val = des_str2arr(val);
  27.     isstr = true;
  28.     } else
  29.     isstr = false;
  30.  
  31.     out = new Array(8);
  32.     for(i = 0; i < 8; i++)
  33.     out[i] = val[i];
  34.  
  35.     des_odd_key_parity(key);
  36.     des_set_key(key);
  37.     des_convert(out, true);
  38.     return isstr ? des_arr2str(out) : out;
  39. }
  40.  
  41. function des_decrypt(key, val) {
  42.     var i, isstr;
  43.     var out = new Array(8);
  44.  
  45.     if(typeof key == "string")
  46.     key = des_str2arr(key);
  47.     if(typeof val == "string") {
  48.     val = des_str2arr(val);
  49.     isstr = true;
  50.     } else
  51.     isstr = false;
  52.     out = new Array(8);
  53.     for(i = 0; i < 8; i++)
  54.     out[i] = val[i];
  55.  
  56.     des_odd_key_parity(key);
  57.     des_set_key(key);
  58.  
  59.     des_convert(out, false);
  60.  
  61.     return isstr ? des_arr2str(out) : out;
  62. }
  63.  
  64. var des_hexString = new Array("0", "1", "2", "3", "4", "5", "6", "7",
  65.                   "8", "9", "A", "B", "C", "D", "E", "F");
  66. function des_escape(val) {
  67.     var i, c, len, out;
  68.  
  69.     len = val.length;
  70.     out = "";
  71.     for(i = 0; i < len; i++) {
  72.     c = val.charCodeAt(i);
  73.     out += des_hexString[c >> 4];
  74.     out += des_hexString[c & 0xf];
  75.     }
  76.     return out;
  77. }
  78.  
  79. function des_unescape(val) {
  80.     var i, c1, c2, len, out;
  81.  
  82.     len = val.length;
  83.     out = "";
  84.  
  85.     i = 0;
  86.     while(i < len) {
  87.     do {
  88.         c1 = "0123456789ABCDEF".indexOf(val.substr(i++, 1));
  89.     } while(c1 == -1 && i < len);
  90.     if(c1 == -1) break;
  91.     do {
  92.         c2 = "0123456789ABCDEF".indexOf(val.substr(i++, 1));
  93.     } while(c2 == -1 && i < len);
  94.     if(c2 == -1) break;
  95.     out += String.fromCharCode((c1<<4)|c2);
  96.     }
  97.  
  98.     return out;
  99. }
  100.  
  101.  
  102. function des_cbc_encrypt(key, str) {
  103.     var i, j, ilimit, rem;
  104.     var input, tin;
  105.     var out;
  106.  
  107.     if(typeof key == "string") {
  108.     if(key.length > 8) {
  109.         key = des_split_key(key);
  110.         for(i = 0; i < key.length; i++)
  111.         str = des_cbc_encrypt(key[i], str);
  112.         return str;
  113.     }
  114.     key = des_str2arr(key);
  115.     }
  116.     ilimit = str.length - 7;
  117.     des_odd_key_parity(key);
  118.     des_set_key(key);
  119.  
  120.     input = new Array(8);
  121.     tin   = new Array(8);
  122.     for(i = 0; i < 8; i++)
  123.     tin[i] = 0;
  124.     out = "";
  125.  
  126.     for(i = 0; i < ilimit; i += 8) {
  127.     for(j = 0; j < 8; j++) {
  128.         input[j] = str.charCodeAt(i + j);
  129.         input[j] ^= tin[j];
  130.     }
  131.     des_convert(input, true);
  132.     for(j = 0; j < 8; j++)
  133.         tin[j] = input[j];
  134.     for(j = 0; j < 8; j++)
  135.         out += String.fromCharCode(input[j]);
  136.     }
  137.     rem = str.length - i;
  138.     for(j = 0; j < rem; j++) {
  139.     input[j] = str.charCodeAt(i + j);
  140.     input[j] ^= tin[j];
  141.     }
  142.     for(i = 7 - rem; i > 0; i--, j++) {
  143.     input[j] = String.fromCharCode(Math.floor(Math.random() * 256));
  144.         input[j] ^= tin[j];
  145.     }
  146.     input[7] = rem;
  147.     input[7] ^= tin[7];
  148.     des_convert(input, true);
  149.     for(j = 0; j < 8; j++)
  150.     tin[i] = input[i];
  151.     for(j = 0; j < 8; j++)
  152.     out += String.fromCharCode(input[j]);
  153.     return out;
  154. }
  155.  
  156. function des_cbc_decrypt(key, str) {
  157.     var i, j, ilimit, rem;
  158.     var input, tin, inxor;
  159.     var out;
  160.  
  161.     if(typeof key == "string") {
  162.     if(key.length > 8) {
  163.         key = des_split_key(key);
  164.         for(i = key.length - 1; i >= 0; i--)
  165.         str = des_cbc_decrypt(key[i], str);
  166.         return str;
  167.     }
  168.     key = des_str2arr(key);
  169.     }
  170.     ilimit = str.length - 8;
  171.     des_odd_key_parity(key);
  172.     des_set_key(key);
  173.  
  174.     input = new Array(8);
  175.     tin   = new Array(8);
  176.     inxor = new Array(8);
  177.     for(i = 0; i < 8; i++)
  178.     inxor[i] = 0;
  179.     out = "";
  180.     for(i = 0; i < ilimit; i += 8) {
  181.     for(j = 0; j < 8; j++)
  182.         tin[j] = input[j] = str.charCodeAt(i + j);
  183.     des_convert(input, false);
  184.     for(j = 0; j < 8; j++) {
  185.         input[j] ^= inxor[j];
  186.         inxor[j] = tin[j];
  187.     }
  188.     for(j = 0; j < 8; j++)
  189.         out += String.fromCharCode(input[j]);
  190.     }
  191.  
  192.     for(j = 0; j < 8; j++)
  193.     tin[j] = input[j] = str.charCodeAt(i + j);
  194.     des_convert(input, false);
  195.     for(j = 0; j < 8; j++) {
  196.     input[j] ^= inxor[j];
  197.     inxor[j] = tin[j];
  198.     }
  199.     rem = input[7] & 0x7;
  200.     for(j = 0; j < rem; j++)
  201.     out += String.fromCharCode(input[j]);
  202.     return out;
  203. }
  204.  
  205. /*
  206.  * Internal variable and functions
  207.  */
  208.  
  209. // for C bits (numbered as per FIPS 46) 1 2 3 4 5 6
  210. var des_skb0 = new Array(
  211.     0x00000000,0x00000010,0x20000000,0x20000010,
  212.     0x00010000,0x00010010,0x20010000,0x20010010,
  213.     0x00000800,0x00000810,0x20000800,0x20000810,
  214.     0x00010800,0x00010810,0x20010800,0x20010810,
  215.     0x00000020,0x00000030,0x20000020,0x20000030,
  216.     0x00010020,0x00010030,0x20010020,0x20010030,
  217.     0x00000820,0x00000830,0x20000820,0x20000830,
  218.     0x00010820,0x00010830,0x20010820,0x20010830,
  219.     0x00080000,0x00080010,0x20080000,0x20080010,
  220.     0x00090000,0x00090010,0x20090000,0x20090010,
  221.     0x00080800,0x00080810,0x20080800,0x20080810,
  222.     0x00090800,0x00090810,0x20090800,0x20090810,
  223.     0x00080020,0x00080030,0x20080020,0x20080030,
  224.     0x00090020,0x00090030,0x20090020,0x20090030,
  225.     0x00080820,0x00080830,0x20080820,0x20080830,
  226.     0x00090820,0x00090830,0x20090820,0x20090830);
  227.  
  228. // for C bits (numbered as per FIPS 46) 7 8 10 11 12 13
  229. var des_skb1 = new Array(
  230.     0x00000000,0x02000000,0x00002000,0x02002000,
  231.     0x00200000,0x02200000,0x00202000,0x02202000,
  232.     0x00000004,0x02000004,0x00002004,0x02002004,
  233.     0x00200004,0x02200004,0x00202004,0x02202004,
  234.     0x00000400,0x02000400,0x00002400,0x02002400,
  235.     0x00200400,0x02200400,0x00202400,0x02202400,
  236.     0x00000404,0x02000404,0x00002404,0x02002404,
  237.     0x00200404,0x02200404,0x00202404,0x02202404,
  238.     0x10000000,0x12000000,0x10002000,0x12002000,
  239.     0x10200000,0x12200000,0x10202000,0x12202000,
  240.     0x10000004,0x12000004,0x10002004,0x12002004,
  241.     0x10200004,0x12200004,0x10202004,0x12202004,
  242.     0x10000400,0x12000400,0x10002400,0x12002400,
  243.     0x10200400,0x12200400,0x10202400,0x12202400,
  244.     0x10000404,0x12000404,0x10002404,0x12002404,
  245.     0x10200404,0x12200404,0x10202404,0x12202404);
  246.  
  247. // for C bits (numbered as per FIPS 46) 14 15 16 17 19 20
  248. var des_skb2 = new Array(
  249.     0x00000000,0x00000001,0x00040000,0x00040001,
  250.     0x01000000,0x01000001,0x01040000,0x01040001,
  251.     0x00000002,0x00000003,0x00040002,0x00040003,
  252.     0x01000002,0x01000003,0x01040002,0x01040003,
  253.     0x00000200,0x00000201,0x00040200,0x00040201,
  254.     0x01000200,0x01000201,0x01040200,0x01040201,
  255.     0x00000202,0x00000203,0x00040202,0x00040203,
  256.     0x01000202,0x01000203,0x01040202,0x01040203,
  257.     0x08000000,0x08000001,0x08040000,0x08040001,
  258.     0x09000000,0x09000001,0x09040000,0x09040001,
  259.     0x08000002,0x08000003,0x08040002,0x08040003,
  260.     0x09000002,0x09000003,0x09040002,0x09040003,
  261.     0x08000200,0x08000201,0x08040200,0x08040201,
  262.     0x09000200,0x09000201,0x09040200,0x09040201,
  263.     0x08000202,0x08000203,0x08040202,0x08040203,
  264.     0x09000202,0x09000203,0x09040202,0x09040203);
  265.  
  266. // for C bits (numbered as per FIPS 46) 21 23 24 26 27 28
  267. var des_skb3 = new Array(
  268.     0x00000000,0x00100000,0x00000100,0x00100100,
  269.     0x00000008,0x00100008,0x00000108,0x00100108,
  270.     0x00001000,0x00101000,0x00001100,0x00101100,
  271.     0x00001008,0x00101008,0x00001108,0x00101108,
  272.     0x04000000,0x04100000,0x04000100,0x04100100,
  273.     0x04000008,0x04100008,0x04000108,0x04100108,
  274.     0x04001000,0x04101000,0x04001100,0x04101100,
  275.     0x04001008,0x04101008,0x04001108,0x04101108,
  276.     0x00020000,0x00120000,0x00020100,0x00120100,
  277.     0x00020008,0x00120008,0x00020108,0x00120108,
  278.     0x00021000,0x00121000,0x00021100,0x00121100,
  279.     0x00021008,0x00121008,0x00021108,0x00121108,
  280.     0x04020000,0x04120000,0x04020100,0x04120100,
  281.     0x04020008,0x04120008,0x04020108,0x04120108,
  282.     0x04021000,0x04121000,0x04021100,0x04121100,
  283.     0x04021008,0x04121008,0x04021108,0x04121108);
  284.  
  285. // for D bits (numbered as per FIPS 46) 1 2 3 4 5 6
  286. var des_skb4 = new Array(
  287.     0x00000000,0x10000000,0x00010000,0x10010000,
  288.     0x00000004,0x10000004,0x00010004,0x10010004,
  289.     0x20000000,0x30000000,0x20010000,0x30010000,
  290.     0x20000004,0x30000004,0x20010004,0x30010004,
  291.     0x00100000,0x10100000,0x00110000,0x10110000,
  292.     0x00100004,0x10100004,0x00110004,0x10110004,
  293.     0x20100000,0x30100000,0x20110000,0x30110000,
  294.     0x20100004,0x30100004,0x20110004,0x30110004,
  295.     0x00001000,0x10001000,0x00011000,0x10011000,
  296.     0x00001004,0x10001004,0x00011004,0x10011004,
  297.     0x20001000,0x30001000,0x20011000,0x30011000,
  298.     0x20001004,0x30001004,0x20011004,0x30011004,
  299.     0x00101000,0x10101000,0x00111000,0x10111000,
  300.     0x00101004,0x10101004,0x00111004,0x10111004,
  301.     0x20101000,0x30101000,0x20111000,0x30111000,
  302.     0x20101004,0x30101004,0x20111004,0x30111004);
  303.  
  304. // for D bits (numbered as per FIPS 46) 8 9 11 12 13 14
  305. var des_skb5 = new Array(
  306.     0x00000000,0x08000000,0x00000008,0x08000008,
  307.     0x00000400,0x08000400,0x00000408,0x08000408,
  308.     0x00020000,0x08020000,0x00020008,0x08020008,
  309.     0x00020400,0x08020400,0x00020408,0x08020408,
  310.     0x00000001,0x08000001,0x00000009,0x08000009,
  311.     0x00000401,0x08000401,0x00000409,0x08000409,
  312.     0x00020001,0x08020001,0x00020009,0x08020009,
  313.     0x00020401,0x08020401,0x00020409,0x08020409,
  314.     0x02000000,0x0A000000,0x02000008,0x0A000008,
  315.     0x02000400,0x0A000400,0x02000408,0x0A000408,
  316.     0x02020000,0x0A020000,0x02020008,0x0A020008,
  317.     0x02020400,0x0A020400,0x02020408,0x0A020408,
  318.     0x02000001,0x0A000001,0x02000009,0x0A000009,
  319.     0x02000401,0x0A000401,0x02000409,0x0A000409,
  320.     0x02020001,0x0A020001,0x02020009,0x0A020009,
  321.     0x02020401,0x0A020401,0x02020409,0x0A020409);
  322.  
  323. // for D bits (numbered as per FIPS 46) 16 17 18 19 20 21
  324. var des_skb6 = new Array(
  325.     0x00000000,0x00000100,0x00080000,0x00080100,
  326.     0x01000000,0x01000100,0x01080000,0x01080100,
  327.     0x00000010,0x00000110,0x00080010,0x00080110,
  328.     0x01000010,0x01000110,0x01080010,0x01080110,
  329.     0x00200000,0x00200100,0x00280000,0x00280100,
  330.     0x01200000,0x01200100,0x01280000,0x01280100,
  331.     0x00200010,0x00200110,0x00280010,0x00280110,
  332.     0x01200010,0x01200110,0x01280010,0x01280110,
  333.     0x00000200,0x00000300,0x00080200,0x00080300,
  334.     0x01000200,0x01000300,0x01080200,0x01080300,
  335.     0x00000210,0x00000310,0x00080210,0x00080310,
  336.     0x01000210,0x01000310,0x01080210,0x01080310,
  337.     0x00200200,0x00200300,0x00280200,0x00280300,
  338.     0x01200200,0x01200300,0x01280200,0x01280300,
  339.     0x00200210,0x00200310,0x00280210,0x00280310,
  340.     0x01200210,0x01200310,0x01280210,0x01280310);
  341.  
  342. // for D bits (numbered as per FIPS 46) 22 23 24 25 27 28
  343. var des_skb7 = new Array(
  344.     0x00000000,0x04000000,0x00040000,0x04040000,
  345.     0x00000002,0x04000002,0x00040002,0x04040002,
  346.     0x00002000,0x04002000,0x00042000,0x04042000,
  347.     0x00002002,0x04002002,0x00042002,0x04042002,
  348.     0x00000020,0x04000020,0x00040020,0x04040020,
  349.     0x00000022,0x04000022,0x00040022,0x04040022,
  350.     0x00002020,0x04002020,0x00042020,0x04042020,
  351.     0x00002022,0x04002022,0x00042022,0x04042022,
  352.     0x00000800,0x04000800,0x00040800,0x04040800,
  353.     0x00000802,0x04000802,0x00040802,0x04040802,
  354.     0x00002800,0x04002800,0x00042800,0x04042800,
  355.     0x00002802,0x04002802,0x00042802,0x04042802,
  356.     0x00000820,0x04000820,0x00040820,0x04040820,
  357.     0x00000822,0x04000822,0x00040822,0x04040822,
  358.     0x00002820,0x04002820,0x00042820,0x04042820,
  359.     0x00002822,0x04002822,0x00042822,0x04042822);
  360.  
  361. var des_shifts2 = new Array(
  362.     false,false,true,true,true,true,true,true,
  363.     false,true,true,true,true,true,true,false);
  364.  
  365. // used in encrypt or decrypt
  366. var des_SP0 = new Array(
  367.     0x00410100, 0x00010000, 0x40400000, 0x40410100,
  368.     0x00400000, 0x40010100, 0x40010000, 0x40400000,
  369.     0x40010100, 0x00410100, 0x00410000, 0x40000100,
  370.     0x40400100, 0x00400000, 0x00000000, 0x40010000,
  371.     0x00010000, 0x40000000, 0x00400100, 0x00010100,
  372.     0x40410100, 0x00410000, 0x40000100, 0x00400100,
  373.     0x40000000, 0x00000100, 0x00010100, 0x40410000,
  374.     0x00000100, 0x40400100, 0x40410000, 0x00000000,
  375.     0x00000000, 0x40410100, 0x00400100, 0x40010000,
  376.     0x00410100, 0x00010000, 0x40000100, 0x00400100,
  377.     0x40410000, 0x00000100, 0x00010100, 0x40400000,
  378.     0x40010100, 0x40000000, 0x40400000, 0x00410000,
  379.     0x40410100, 0x00010100, 0x00410000, 0x40400100,
  380.     0x00400000, 0x40000100, 0x40010000, 0x00000000,
  381.     0x00010000, 0x00400000, 0x40400100, 0x00410100,
  382.     0x40000000, 0x40410000, 0x00000100, 0x40010100);
  383.  
  384. var des_SP1 = new Array(
  385.     0x08021002, 0x00000000, 0x00021000, 0x08020000,
  386.     0x08000002, 0x00001002, 0x08001000, 0x00021000,
  387.     0x00001000, 0x08020002, 0x00000002, 0x08001000,
  388.     0x00020002, 0x08021000, 0x08020000, 0x00000002,
  389.     0x00020000, 0x08001002, 0x08020002, 0x00001000,
  390.     0x00021002, 0x08000000, 0x00000000, 0x00020002,
  391.     0x08001002, 0x00021002, 0x08021000, 0x08000002,
  392.     0x08000000, 0x00020000, 0x00001002, 0x08021002,
  393.     0x00020002, 0x08021000, 0x08001000, 0x00021002,
  394.     0x08021002, 0x00020002, 0x08000002, 0x00000000,
  395.     0x08000000, 0x00001002, 0x00020000, 0x08020002,
  396.     0x00001000, 0x08000000, 0x00021002, 0x08001002,
  397.     0x08021000, 0x00001000, 0x00000000, 0x08000002,
  398.     0x00000002, 0x08021002, 0x00021000, 0x08020000,
  399.     0x08020002, 0x00020000, 0x00001002, 0x08001000,
  400.     0x08001002, 0x00000002, 0x08020000, 0x00021000);
  401.  
  402. var des_SP2 = new Array(
  403.     0x20800000, 0x00808020, 0x00000020, 0x20800020,
  404.     0x20008000, 0x00800000, 0x20800020, 0x00008020,
  405.     0x00800020, 0x00008000, 0x00808000, 0x20000000,
  406.     0x20808020, 0x20000020, 0x20000000, 0x20808000,
  407.     0x00000000, 0x20008000, 0x00808020, 0x00000020,
  408.     0x20000020, 0x20808020, 0x00008000, 0x20800000,
  409.     0x20808000, 0x00800020, 0x20008020, 0x00808000,
  410.     0x00008020, 0x00000000, 0x00800000, 0x20008020,
  411.     0x00808020, 0x00000020, 0x20000000, 0x00008000,
  412.     0x20000020, 0x20008000, 0x00808000, 0x20800020,
  413.     0x00000000, 0x00808020, 0x00008020, 0x20808000,
  414.     0x20008000, 0x00800000, 0x20808020, 0x20000000,
  415.     0x20008020, 0x20800000, 0x00800000, 0x20808020,
  416.     0x00008000, 0x00800020, 0x20800020, 0x00008020,
  417.     0x00800020, 0x00000000, 0x20808000, 0x20000020,
  418.     0x20800000, 0x20008020, 0x00000020, 0x00808000);
  419.  
  420. var des_SP3 = new Array(
  421.     0x00080201, 0x02000200, 0x00000001, 0x02080201,
  422.     0x00000000, 0x02080000, 0x02000201, 0x00080001,
  423.     0x02080200, 0x02000001, 0x02000000, 0x00000201,
  424.     0x02000001, 0x00080201, 0x00080000, 0x02000000,
  425.     0x02080001, 0x00080200, 0x00000200, 0x00000001,
  426.     0x00080200, 0x02000201, 0x02080000, 0x00000200,
  427.     0x00000201, 0x00000000, 0x00080001, 0x02080200,
  428.     0x02000200, 0x02080001, 0x02080201, 0x00080000,
  429.     0x02080001, 0x00000201, 0x00080000, 0x02000001,
  430.     0x00080200, 0x02000200, 0x00000001, 0x02080000,
  431.     0x02000201, 0x00000000, 0x00000200, 0x00080001,
  432.     0x00000000, 0x02080001, 0x02080200, 0x00000200,
  433.     0x02000000, 0x02080201, 0x00080201, 0x00080000,
  434.     0x02080201, 0x00000001, 0x02000200, 0x00080201,
  435.     0x00080001, 0x00080200, 0x02080000, 0x02000201,
  436.     0x00000201, 0x02000000, 0x02000001, 0x02080200);
  437.  
  438. var des_SP4 = new Array(
  439.     0x01000000, 0x00002000, 0x00000080, 0x01002084,
  440.     0x01002004, 0x01000080, 0x00002084, 0x01002000,
  441.     0x00002000, 0x00000004, 0x01000004, 0x00002080,
  442.     0x01000084, 0x01002004, 0x01002080, 0x00000000,
  443.     0x00002080, 0x01000000, 0x00002004, 0x00000084,
  444.     0x01000080, 0x00002084, 0x00000000, 0x01000004,
  445.     0x00000004, 0x01000084, 0x01002084, 0x00002004,
  446.     0x01002000, 0x00000080, 0x00000084, 0x01002080,
  447.     0x01002080, 0x01000084, 0x00002004, 0x01002000,
  448.     0x00002000, 0x00000004, 0x01000004, 0x01000080,
  449.     0x01000000, 0x00002080, 0x01002084, 0x00000000,
  450.     0x00002084, 0x01000000, 0x00000080, 0x00002004,
  451.     0x01000084, 0x00000080, 0x00000000, 0x01002084,
  452.     0x01002004, 0x01002080, 0x00000084, 0x00002000,
  453.     0x00002080, 0x01002004, 0x01000080, 0x00000084,
  454.     0x00000004, 0x00002084, 0x01002000, 0x01000004);
  455.  
  456. var des_SP5 = new Array(
  457.     0x10000008, 0x00040008, 0x00000000, 0x10040400,
  458.     0x00040008, 0x00000400, 0x10000408, 0x00040000,
  459.     0x00000408, 0x10040408, 0x00040400, 0x10000000,
  460.     0x10000400, 0x10000008, 0x10040000, 0x00040408,
  461.     0x00040000, 0x10000408, 0x10040008, 0x00000000,
  462.     0x00000400, 0x00000008, 0x10040400, 0x10040008,
  463.     0x10040408, 0x10040000, 0x10000000, 0x00000408,
  464.     0x00000008, 0x00040400, 0x00040408, 0x10000400,
  465.     0x00000408, 0x10000000, 0x10000400, 0x00040408,
  466.     0x10040400, 0x00040008, 0x00000000, 0x10000400,
  467.     0x10000000, 0x00000400, 0x10040008, 0x00040000,
  468.     0x00040008, 0x10040408, 0x00040400, 0x00000008,
  469.     0x10040408, 0x00040400, 0x00040000, 0x10000408,
  470.     0x10000008, 0x10040000, 0x00040408, 0x00000000,
  471.     0x00000400, 0x10000008, 0x10000408, 0x10040400,
  472.     0x10040000, 0x00000408, 0x00000008, 0x10040008);
  473.  
  474. var des_SP6 = new Array(
  475.     0x00000800, 0x00000040, 0x00200040,-0x7fe00000,
  476.    -0x7fdff7c0,-0x7ffff800, 0x00000840, 0x00000000,
  477.     0x00200000,-0x7fdfffc0,-0x7fffffc0, 0x00200800,
  478.    -0x80000000, 0x00200840, 0x00200800,-0x7fffffc0,
  479.    -0x7fdfffc0, 0x00000800,-0x7ffff800,-0x7fdff7c0,
  480.     0x00000000, 0x00200040,-0x7fe00000, 0x00000840,
  481.    -0x7fdff800,-0x7ffff7c0, 0x00200840,-0x80000000,
  482.    -0x7ffff7c0,-0x7fdff800, 0x00000040, 0x00200000,
  483.    -0x7ffff7c0, 0x00200800,-0x7fdff800,-0x7fffffc0,
  484.     0x00000800, 0x00000040, 0x00200000,-0x7fdff800,
  485.    -0x7fdfffc0,-0x7ffff7c0, 0x00000840, 0x00000000,
  486.     0x00000040,-0x7fe00000,-0x80000000, 0x00200040,
  487.     0x00000000,-0x7fdfffc0, 0x00200040, 0x00000840,
  488.    -0x7fffffc0, 0x00000800,-0x7fdff7c0, 0x00200000,
  489.     0x00200840,-0x80000000,-0x7ffff800,-0x7fdff7c0,
  490.    -0x7fe00000, 0x00200840, 0x00200800,-0x7ffff800);
  491.  
  492. var des_SP7 = new Array(
  493.     0x04100010, 0x04104000, 0x00004010, 0x00000000,
  494.     0x04004000, 0x00100010, 0x04100000, 0x04104010,
  495.     0x00000010, 0x04000000, 0x00104000, 0x00004010,
  496.     0x00104010, 0x04004010, 0x04000010, 0x04100000,
  497.     0x00004000, 0x00104010, 0x00100010, 0x04004000,
  498.     0x04104010, 0x04000010, 0x00000000, 0x00104000,
  499.     0x04000000, 0x00100000, 0x04004010, 0x04100010,
  500.     0x00100000, 0x00004000, 0x04104000, 0x00000010,
  501.     0x00100000, 0x00004000, 0x04000010, 0x04104010,
  502.     0x00004010, 0x04000000, 0x00000000, 0x00104000,
  503.     0x04100010, 0x04004010, 0x04004000, 0x00100010,
  504.     0x04104000, 0x00000010, 0x00100010, 0x04004000,
  505.     0x04104010, 0x00100000, 0x04100000, 0x04000010,
  506.     0x00104000, 0x00004010, 0x04004010, 0x04100000,
  507.     0x00000010, 0x04104000, 0x00104010, 0x00000000,
  508.     0x04000000, 0x04100010, 0x00004000, 0x00104010);
  509.  
  510. // Odd parity table
  511. var des_parity = new Array(
  512.     0x80, 0x01, 0x02, 0x83, 0x04, 0x85, 0x86, 0x07,
  513.     0x08, 0x89, 0x8a, 0x0b, 0x8c, 0x0d, 0x0e, 0x8f,
  514.     0x10, 0x91, 0x92, 0x13, 0x94, 0x15, 0x16, 0x97,
  515.     0x98, 0x19, 0x1a, 0x9b, 0x1c, 0x9d, 0x9e, 0x1f,
  516.     0x20, 0xa1, 0xa2, 0x23, 0xa4, 0x25, 0x26, 0xa7,
  517.     0xa8, 0x29, 0x2a, 0xab, 0x2c, 0xad, 0xae, 0x2f,
  518.     0xb0, 0x31, 0x32, 0xb3, 0x34, 0xb5, 0xb6, 0x37,
  519.     0x38, 0xb9, 0xba, 0x3b, 0xbc, 0x3d, 0x3e, 0xbf,
  520.     0x40, 0xc1, 0xc2, 0x43, 0xc4, 0x45, 0x46, 0xc7,
  521.     0xc8, 0x49, 0x4a, 0xcb, 0x4c, 0xcd, 0xce, 0x4f,
  522.     0xd0, 0x51, 0x52, 0xd3, 0x54, 0xd5, 0xd6, 0x57,
  523.     0x58, 0xd9, 0xda, 0x5b, 0xdc, 0x5d, 0x5e, 0xdf,
  524.     0xe0, 0x61, 0x62, 0xe3, 0x64, 0xe5, 0xe6, 0x67,
  525.     0x68, 0xe9, 0xea, 0x6b, 0xec, 0x6d, 0x6e, 0xef,
  526.     0x70, 0xf1, 0xf2, 0x73, 0xf4, 0x75, 0x76, 0xf7,
  527.     0xf8, 0x79, 0x7a, 0xfb, 0x7c, 0xfd, 0xfe, 0x7f,
  528.     0x80, 0x01, 0x02, 0x83, 0x04, 0x85, 0x86, 0x07,
  529.     0x08, 0x89, 0x8a, 0x0b, 0x8c, 0x0d, 0x0e, 0x8f,
  530.     0x10, 0x91, 0x92, 0x13, 0x94, 0x15, 0x16, 0x97,
  531.     0x98, 0x19, 0x1a, 0x9b, 0x1c, 0x9d, 0x9e, 0x1f,
  532.     0x20, 0xa1, 0xa2, 0x23, 0xa4, 0x25, 0x26, 0xa7,
  533.     0xa8, 0x29, 0x2a, 0xab, 0x2c, 0xad, 0xae, 0x2f,
  534.     0xb0, 0x31, 0x32, 0xb3, 0x34, 0xb5, 0xb6, 0x37,
  535.     0x38, 0xb9, 0xba, 0x3b, 0xbc, 0x3d, 0x3e, 0xbf,
  536.     0x40, 0xc1, 0xc2, 0x43, 0xc4, 0x45, 0x46, 0xc7,
  537.     0xc8, 0x49, 0x4a, 0xcb, 0x4c, 0xcd, 0xce, 0x4f,
  538.     0xd0, 0x51, 0x52, 0xd3, 0x54, 0xd5, 0xd6, 0x57,
  539.     0x58, 0xd9, 0xda, 0x5b, 0xdc, 0x5d, 0x5e, 0xdf,
  540.     0xe0, 0x61, 0x62, 0xe3, 0x64, 0xe5, 0xe6, 0x67,
  541.     0x68, 0xe9, 0xea, 0x6b, 0xec, 0x6d, 0x6e, 0xef,
  542.     0x70, 0xf1, 0xf2, 0x73, 0xf4, 0x75, 0x76, 0xf7,
  543.     0xf8, 0x79, 0x7a, 0xfb, 0x7c, 0xfd, 0xfe, 0x7f);
  544.  
  545. var des_ks = new Array(32); // Key Scheduler
  546. var des_l, des_r;       // Left&Right bits
  547.  
  548. // Internal functions
  549.  
  550. function des_set_key(key) {
  551.     var i, s, t;
  552.     var l, r;
  553.  
  554.     des_l = (key[0]  )|
  555.     (key[1] <<  8)|
  556.     (key[2] << 16)|
  557.     (key[3] << 24);
  558.     des_r = (key[4]  )|
  559.     (key[5] <<  8)|
  560.     (key[6] << 16)|
  561.     (key[7] << 24);
  562.  
  563.     doPC1();
  564.  
  565.     l = des_l;
  566.     r = des_r;
  567.     for(i = 0; i < 16; i++) {
  568.     if(des_shifts2[i]) {
  569.         l=(l>>2)|(l<<26);
  570.         r=(r>>2)|(r<<26);
  571.     } else {
  572.         l=(l>>1)|(l<<27);
  573.         r=(r>>1)|(r<<27);
  574.     }
  575.     l&=0x0fffffff;
  576.     r&=0x0fffffff;
  577.  
  578.     s=  des_skb0[ (l    )&0x3f        ]|
  579.         des_skb1[((l>> 6)&0x03)|((l>> 7)&0x3c)]|
  580.         des_skb2[((l>>13)&0x0f)|((l>>14)&0x30)]|
  581.         des_skb3[((l>>20)&0x01)|((l>>21)&0x06) |
  582.                      ((l>>22)&0x38)];
  583.     t=  des_skb4[ (r    )&0x3f        ]|
  584.         des_skb5[((r>> 7)&0x03)|((r>> 8)&0x3c)]|
  585.         des_skb6[ (r>>15)&0x3f        ]|
  586.         des_skb7[((r>>21)&0x0f)|((r>>22)&0x30)];
  587.       des_ks[2 * i    ] = (t<<16)|(s&0x0000ffff);
  588.       s=                  ((s>>16)&0x0000ffff)|(t&-65536);
  589.       des_ks[2 * i + 1] = (s<<4)|((s>>28)&0xf);
  590.     }
  591. }
  592.  
  593. function doPC1() {
  594.     var t;
  595.     var l, r;
  596.  
  597.     l = des_l;
  598.     r = des_r;
  599.  
  600.     t=((r>>4)^l)&0x0f0f0f0f;
  601.     r^=(t<<4); l^=t;
  602.  
  603.     // do l first
  604.     t=((l<<18)^l)&-859045888;
  605.     l=l^t^((t>>18)&0x00003fff);
  606.     t=((l<<17)^l)&-1431699456;
  607.     l=l^t^((t>>17)&0x00007fff);
  608.     t=((l<< 8)^l)&0x00ff0000;
  609.     l=l^t^((t>> 8)&0x00ffffff);
  610.     t=((l<<17)^l)&-1431699456;
  611.     l=l^t^((t>>17)&0x00007fff);
  612.  
  613.     // now do r
  614.     t=((r<<24)^r)&-16777216;
  615.     r=r^t^((t>>24)&0x000000ff);
  616.     t=((r<< 8)^r)&0x00ff0000;
  617.     r=r^t^((t>> 8)&0x00ffffff);
  618.     t=((r<<14)^r)&0x33330000;
  619.     r=r^t^((t>>14)&0x0003ffff);
  620.     r=((r&0x00aa00aa)<<7)|((r&0x55005500)>>7)|(r&-1437226411);
  621.     r=((r>>8)&0x00ffffff)|(((l&-268435456)>>4)&0x0fffffff);
  622.     l&=0x0fffffff;
  623.  
  624.     des_l = l;
  625.     des_r = r;
  626. }
  627.  
  628. function des_convert(input, encrypt) {
  629.     var i, t, u;
  630.     var l, r;
  631.  
  632.     // Get the bytes in the order we want.
  633.     des_l=     (input[0])|
  634.            (input[1]<< 8)|
  635.            (input[2]<<16)|
  636.            (input[3]<<24);
  637.     des_r=     (input[4])|
  638.            (input[5]<< 8)|
  639.            (input[6]<<16)|
  640.            (input[7]<<24);
  641.     doIP();
  642.  
  643.     l = des_l;
  644.     r = des_r;
  645.  
  646.     if(encrypt) {
  647.       for(i = 0; i < 32; i += 4) {
  648.     t=(((r&0x7fffffff)<<1)|((r>>31)&0x00000001));
  649.     u=t^des_ks[i  ];
  650.     t=t^des_ks[i+1];
  651.     t=(((t>>4)&0x0fffffff)|((t&0x0000000f)<<28));
  652.     l^=    des_SP1[ t     &0x3f]|
  653.            des_SP3[(t>> 8)&0x3f]|
  654.            des_SP5[(t>>16)&0x3f]|
  655.            des_SP7[(t>>24)&0x3f]|
  656.            des_SP0[ u     &0x3f]|
  657.            des_SP2[(u>> 8)&0x3f]|
  658.            des_SP4[(u>>16)&0x3f]|
  659.            des_SP6[(u>>24)&0x3f];
  660.  
  661.     t=(l<<1)|((l>>31)&0x1);
  662.     u=t^des_ks[i+2];
  663.     t=t^des_ks[i+3];
  664.     t=((t>>4)&0x0fffffff)|(t<<28);
  665.     r^=    des_SP1[ t     &0x3f]|
  666.            des_SP3[(t>> 8)&0x3f]|
  667.            des_SP5[(t>>16)&0x3f]|
  668.            des_SP7[(t>>24)&0x3f]|
  669.            des_SP0[ u     &0x3f]|
  670.            des_SP2[(u>> 8)&0x3f]|
  671.            des_SP4[(u>>16)&0x3f]|
  672.            des_SP6[(u>>24)&0x3f];
  673.       }
  674.     } else {
  675.       for(i = 30; i > 0; i -= 4)
  676.     {
  677.       t=(r<<1)|((r>>31)&0x1);
  678.       u=t^des_ks[i  ];
  679.       t=t^des_ks[i+1];
  680.       t=((t>>4)&0x0fffffff)|(t<<28)
  681.       l^=    des_SP1[ t     &0x3f]|
  682.          des_SP3[(t>> 8)&0x3f]|
  683.          des_SP5[(t>>16)&0x3f]|
  684.          des_SP7[(t>>24)&0x3f]|
  685.          des_SP0[ u     &0x3f]|
  686.          des_SP2[(u>> 8)&0x3f]|
  687.          des_SP4[(u>>16)&0x3f]|
  688.          des_SP6[(u>>24)&0x3f];
  689.  
  690.       t=(l<<1)|((l>>31)&0x1);
  691.       u=t^des_ks[i-2];
  692.       t=t^des_ks[i-1];
  693.       t=((t>>4)&0x0fffffff)|(t<<28);
  694.       r^=    des_SP1[ t     &0x3f]|
  695.          des_SP3[(t>> 8)&0x3f]|
  696.          des_SP5[(t>>16)&0x3f]|
  697.          des_SP7[(t>>24)&0x3f]|
  698.          des_SP0[ u     &0x3f]|
  699.          des_SP2[(u>> 8)&0x3f]|
  700.          des_SP4[(u>>16)&0x3f]|
  701.          des_SP6[(u>>24)&0x3f];
  702.     }
  703.     }
  704.     des_l = l;
  705.     des_r = r;
  706.     doFP();
  707.     l = des_l;
  708.     r = des_r;
  709.     input[0] = l&0xff;
  710.     input[1] = (l>> 8)&0xff;
  711.     input[2] = (l>>16)&0xff;
  712.     input[3] = (l>>24)&0xff;
  713.     input[4] = r&0xff;
  714.     input[5] = (r>> 8)&0xff;
  715.     input[6] = (r>>16)&0xff;
  716.     input[7] = (r>>24)&0xff;
  717. }
  718.  
  719. function doIP() {
  720.     var t;
  721.     var l, r;
  722.  
  723.     l = des_l;
  724.     r = des_r;
  725.  
  726.     t=((r>> 4)^l)&0x0f0f0f0f;
  727.     r^=(t<< 4); l^=t;
  728.     t=((l>>16)^r)&0x0000ffff;
  729.     l^=(t<<16); r^=t;
  730.     t=((r>> 2)^l)&0x33333333;
  731.     r^=(t<< 2); l^=t;
  732.     t=((l>> 8)^r)&0x00ff00ff;
  733.     l^=(t<< 8); r^=t;
  734.     t=((r>> 1)^l)&0x55555555;
  735.     r^=(t<< 1); l^=t;
  736.     t=l;
  737.     l=r;
  738.     r=t;
  739.  
  740.     des_l = l;
  741.     des_r = r;
  742. }
  743.  
  744. function doFP() {
  745.     var t;
  746.     var l, r;
  747.  
  748.     l = des_l;
  749.     r = des_r;
  750.  
  751.     t=((r>> 1)^l)&0x55555555;
  752.     r^=(t<< 1); l^=t;
  753.     t=((l>> 8)^r)&0x00ff00ff;
  754.     l^=(t<< 8); r^=t;
  755.     t=((r>> 2)^l)&0x33333333;
  756.     r^=(t<< 2); l^=t;
  757.     t=((l>>16)^r)&0x0000ffff;
  758.     l^=(t<<16); r^=t;
  759.     t=((r>> 4)^l)&0x0f0f0f0f;
  760.     r^=(t<< 4); l^=t;
  761.  
  762.     des_l = l;
  763.     des_r = r;
  764. }
  765.  
  766. function des_odd_key_parity(key) {
  767.     var i;
  768.     for(i = 0; i < 8; i++)
  769.       key[i] = des_parity[key[i] & 0xff];
  770. }
  771.  
  772. function des_str2arr(str) {
  773.     var a = new Array(8);
  774.     var i, len;
  775.  
  776.     if((len = str.length) > 8)
  777.     len = 8;
  778.     for(i = 0; i < len; i++)
  779.     a[i] = str.charCodeAt(i);
  780.     for(; i < 8; i++)
  781.     a[i] = 0;
  782.     return a;
  783. }
  784.  
  785. function des_arr2str(a) {
  786.     var i, s;
  787.     s = "";
  788.     for(i = 0; i < 8; i++)
  789.     s += String.fromCharCode(a[i]);
  790.     return s;
  791. }
  792.  
  793. function des_split_key(key) {
  794.     var kk, i, n;
  795.  
  796.     n = (key.length + 7) >> 3;
  797.     kk = new Array(n);
  798.     for(i = 0; i < n; i++)
  799.     kk[i] = key.substr(i * 8, 8);
  800.     return kk;
  801. }
  802.  
  803. /* for NN 3.0
  804. var des_ascii =
  805.     "\000\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017" +
  806.     "\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037" +
  807.     "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^" +
  808.     "_`abcdefghijklmnopqrstuvwxyz{|}~\177\200\201\202\203\204\205\206" +
  809.     "\207\210\211\212\213\214\215\216\217\220\221\222\223\224\225\226" +
  810.     "\227\230\231\232\233\234\235\236\237\240\241\242\243\244\245\246" +
  811.     "\247\250\251\252\253\254\255\256\257\260\261\262\263\264\265\266" +
  812.     "\267\270\271\272\273\274\275\276\277\300\301\302\303\304\305\306" +
  813.     "\307\310\311\312\313\314\315\316\317\320\321\322\323\324\325\326" +
  814.     "\327\330\331\332\333\334\335\336\337\340\341\342\343\344\345\346" +
  815.     "\347\350\351\352\353\354\355\356\357\360\361\362\363\364\365\366" +
  816.     "\367\370\371\372\373\374\375\376\377";
  817. function fromCharCode(code) {
  818.     return des_ascii.substr(code & 0xff, 1);
  819. }
  820. function charCodeAt(str, idx) {
  821.     var i;
  822.     if((i = des_ascii.indexOf(str.substr(idx, 1))) < 0)
  823.     i = 0;
  824.     return i;
  825. }
  826. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement