picc.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  1. /*
  2. * encrypt the string to string made up of hex
  3. * return the encrypted string
  4. */
  5. function strEnc(data,firstKey,secondKey,thirdKey){
  6. var leng = data.length;
  7. var encData = "";
  8. var firstKeyBt,secondKeyBt,thirdKeyBt,firstLength,secondLength,thirdLength;
  9. if(firstKey != null && firstKey != ""){
  10. firstKeyBt = getKeyBytes(firstKey);
  11. firstLength = firstKeyBt.length;
  12. }
  13. if(secondKey != null && secondKey != ""){
  14. secondKeyBt = getKeyBytes(secondKey);
  15. secondLength = secondKeyBt.length;
  16. }
  17. if(thirdKey != null && thirdKey != ""){
  18. thirdKeyBt = getKeyBytes(thirdKey);
  19. thirdLength = thirdKeyBt.length;
  20. }
  21. if(leng > 0){
  22. if(leng < 4){
  23. var bt = strToBt(data);
  24. var encByte ;
  25. if(firstKey != null && firstKey !="" && secondKey != null && secondKey != "" && thirdKey != null && thirdKey != ""){
  26. var tempBt;
  27. var x,y,z;
  28. tempBt = bt;
  29. for(x = 0;x < firstLength ;x ++){
  30. tempBt = enc(tempBt,firstKeyBt[x]);
  31. }
  32. for(y = 0;y < secondLength ;y ++){
  33. tempBt = enc(tempBt,secondKeyBt[y]);
  34. }
  35. for(z = 0;z < thirdLength ;z ++){
  36. tempBt = enc(tempBt,thirdKeyBt[z]);
  37. }
  38. encByte = tempBt;
  39. }else{
  40. if(firstKey != null && firstKey !="" && secondKey != null && secondKey != ""){
  41. var tempBt;
  42. var x,y;
  43. tempBt = bt;
  44. for(x = 0;x < firstLength ;x ++){
  45. tempBt = enc(tempBt,firstKeyBt[x]);
  46. }
  47. for(y = 0;y < secondLength ;y ++){
  48. tempBt = enc(tempBt,secondKeyBt[y]);
  49. }
  50. encByte = tempBt;
  51. }else{
  52. if(firstKey != null && firstKey !=""){
  53. var tempBt;
  54. var x = 0;
  55. tempBt = bt;
  56. for(x = 0;x < firstLength ;x ++){
  57. tempBt = enc(tempBt,firstKeyBt[x]);
  58. }
  59. encByte = tempBt;
  60. }
  61. }
  62. }
  63. encData = bt64ToHex(encByte);
  64. }else{
  65. var iterator = parseInt(leng/4);
  66. var remainder = leng%4;
  67. var i=0;
  68. for(i = 0;i < iterator;i++){
  69. var tempData = data.substring(i*4+0,i*4+4);
  70. var tempByte = strToBt(tempData);
  71. var encByte ;
  72. if(firstKey != null && firstKey !="" && secondKey != null && secondKey != "" && thirdKey != null && thirdKey != ""){
  73. var tempBt;
  74. var x,y,z;
  75. tempBt = tempByte;
  76. for(x = 0;x < firstLength ;x ++){
  77. tempBt = enc(tempBt,firstKeyBt[x]);
  78. }
  79. for(y = 0;y < secondLength ;y ++){
  80. tempBt = enc(tempBt,secondKeyBt[y]);
  81. }
  82. for(z = 0;z < thirdLength ;z ++){
  83. tempBt = enc(tempBt,thirdKeyBt[z]);
  84. }
  85. encByte = tempBt;
  86. }else{
  87. if(firstKey != null && firstKey !="" && secondKey != null && secondKey != ""){
  88. var tempBt;
  89. var x,y;
  90. tempBt = tempByte;
  91. for(x = 0;x < firstLength ;x ++){
  92. tempBt = enc(tempBt,firstKeyBt[x]);
  93. }
  94. for(y = 0;y < secondLength ;y ++){
  95. tempBt = enc(tempBt,secondKeyBt[y]);
  96. }
  97. encByte = tempBt;
  98. }else{
  99. if(firstKey != null && firstKey !=""){
  100. var tempBt;
  101. var x;
  102. tempBt = tempByte;
  103. for(x = 0;x < firstLength ;x ++){
  104. tempBt = enc(tempBt,firstKeyBt[x]);
  105. }
  106. encByte = tempBt;
  107. }
  108. }
  109. }
  110. encData += bt64ToHex(encByte);
  111. }
  112. if(remainder > 0){
  113. var remainderData = data.substring(iterator*4+0,leng);
  114. var tempByte = strToBt(remainderData);
  115. var encByte ;
  116. if(firstKey != null && firstKey !="" && secondKey != null && secondKey != "" && thirdKey != null && thirdKey != ""){
  117. var tempBt;
  118. var x,y,z;
  119. tempBt = tempByte;
  120. for(x = 0;x < firstLength ;x ++){
  121. tempBt = enc(tempBt,firstKeyBt[x]);
  122. }
  123. for(y = 0;y < secondLength ;y ++){
  124. tempBt = enc(tempBt,secondKeyBt[y]);
  125. }
  126. for(z = 0;z < thirdLength ;z ++){
  127. tempBt = enc(tempBt,thirdKeyBt[z]);
  128. }
  129. encByte = tempBt;
  130. }else{
  131. if(firstKey != null && firstKey !="" && secondKey != null && secondKey != ""){
  132. var tempBt;
  133. var x,y;
  134. tempBt = tempByte;
  135. for(x = 0;x < firstLength ;x ++){
  136. tempBt = enc(tempBt,firstKeyBt[x]);
  137. }
  138. for(y = 0;y < secondLength ;y ++){
  139. tempBt = enc(tempBt,secondKeyBt[y]);
  140. }
  141. encByte = tempBt;
  142. }else{
  143. if(firstKey != null && firstKey !=""){
  144. var tempBt;
  145. var x;
  146. tempBt = tempByte;
  147. for(x = 0;x < firstLength ;x ++){
  148. tempBt = enc(tempBt,firstKeyBt[x]);
  149. }
  150. encByte = tempBt;
  151. }
  152. }
  153. }
  154. encData += bt64ToHex(encByte);
  155. }
  156. }
  157. }
  158. return encData;
  159. }
  160. /*
  161. * decrypt the encrypted string to the original string
  162. *
  163. * return the original string
  164. */
  165. function strDec(data,firstKey,secondKey,thirdKey){
  166. var leng = data.length;
  167. var decStr = "";
  168. var firstKeyBt,secondKeyBt,thirdKeyBt,firstLength,secondLength,thirdLength;
  169. if(firstKey != null && firstKey != ""){
  170. firstKeyBt = getKeyBytes(firstKey);
  171. firstLength = firstKeyBt.length;
  172. }
  173. if(secondKey != null && secondKey != ""){
  174. secondKeyBt = getKeyBytes(secondKey);
  175. secondLength = secondKeyBt.length;
  176. }
  177. if(thirdKey != null && thirdKey != ""){
  178. thirdKeyBt = getKeyBytes(thirdKey);
  179. thirdLength = thirdKeyBt.length;
  180. }
  181. var iterator = parseInt(leng/16);
  182. var i=0;
  183. for(i = 0;i < iterator;i++){
  184. var tempData = data.substring(i*16+0,i*16+16);
  185. var strByte = hexToBt64(tempData);
  186. var intByte = new Array(64);
  187. var j = 0;
  188. for(j = 0;j < 64; j++){
  189. intByte[j] = parseInt(strByte.substring(j,j+1));
  190. }
  191. var decByte;
  192. if(firstKey != null && firstKey !="" && secondKey != null && secondKey != "" && thirdKey != null && thirdKey != ""){
  193. var tempBt;
  194. var x,y,z;
  195. tempBt = intByte;
  196. for(x = thirdLength - 1;x >= 0;x --){
  197. tempBt = dec(tempBt,thirdKeyBt[x]);
  198. }
  199. for(y = secondLength - 1;y >= 0;y --){
  200. tempBt = dec(tempBt,secondKeyBt[y]);
  201. }
  202. for(z = firstLength - 1;z >= 0 ;z --){
  203. tempBt = dec(tempBt,firstKeyBt[z]);
  204. }
  205. decByte = tempBt;
  206. }else{
  207. if(firstKey != null && firstKey !="" && secondKey != null && secondKey != ""){
  208. var tempBt;
  209. var x,y,z;
  210. tempBt = intByte;
  211. for(x = secondLength - 1;x >= 0 ;x --){
  212. tempBt = dec(tempBt,secondKeyBt[x]);
  213. }
  214. for(y = firstLength - 1;y >= 0 ;y --){
  215. tempBt = dec(tempBt,firstKeyBt[y]);
  216. }
  217. decByte = tempBt;
  218. }else{
  219. if(firstKey != null && firstKey !=""){
  220. var tempBt;
  221. var x,y,z;
  222. tempBt = intByte;
  223. for(x = firstLength - 1;x >= 0 ;x --){
  224. tempBt = dec(tempBt,firstKeyBt[x]);
  225. }
  226. decByte = tempBt;
  227. }
  228. }
  229. }
  230. decStr += byteToString(decByte);
  231. }
  232. return decStr;
  233. }
  234. /*
  235. * chang the string into the bit array
  236. *
  237. * return bit array(it's length % 64 = 0)
  238. */
  239. function getKeyBytes(key){
  240. var keyBytes = new Array();
  241. var leng = key.length;
  242. var iterator = parseInt(leng/4);
  243. var remainder = leng%4;
  244. var i = 0;
  245. for(i = 0;i < iterator; i ++){
  246. keyBytes[i] = strToBt(key.substring(i*4+0,i*4+4));
  247. }
  248. if(remainder > 0){
  249. keyBytes[i] = strToBt(key.substring(i*4+0,leng));
  250. }
  251. return keyBytes;
  252. }
  253. /*
  254. * chang the string(it's length <= 4) into the bit array
  255. *
  256. * return bit array(it's length = 64)
  257. */
  258. function strToBt(str){
  259. var leng = str.length;
  260. var bt = new Array(64);
  261. if(leng < 4){
  262. var i=0,j=0,p=0,q=0;
  263. for(i = 0;i<leng;i++){
  264. var k = str.charCodeAt(i);
  265. for(j=0;j<16;j++){
  266. var pow=1,m=0;
  267. for(m=15;m>j;m--){
  268. pow *= 2;
  269. }
  270. bt[16*i+j]=parseInt(k/pow)%2;
  271. }
  272. }
  273. for(p = leng;p<4;p++){
  274. var k = 0;
  275. for(q=0;q<16;q++){
  276. var pow=1,m=0;
  277. for(m=15;m>q;m--){
  278. pow *= 2;
  279. }
  280. bt[16*p+q]=parseInt(k/pow)%2;
  281. }
  282. }
  283. }else{
  284. for(i = 0;i<4;i++){
  285. var k = str.charCodeAt(i);
  286. for(j=0;j<16;j++){
  287. var pow=1;
  288. for(m=15;m>j;m--){
  289. pow *= 2;
  290. }
  291. bt[16*i+j]=parseInt(k/pow)%2;
  292. }
  293. }
  294. }
  295. return bt;
  296. }
  297. /*
  298. * chang the bit(it's length = 4) into the hex
  299. *
  300. * return hex
  301. */
  302. function bt4ToHex(binary) {
  303. var hex;
  304. switch (binary) {
  305. case "0000" : hex = "0"; break;
  306. case "0001" : hex = "1"; break;
  307. case "0010" : hex = "2"; break;
  308. case "0011" : hex = "3"; break;
  309. case "0100" : hex = "4"; break;
  310. case "0101" : hex = "5"; break;
  311. case "0110" : hex = "6"; break;
  312. case "0111" : hex = "7"; break;
  313. case "1000" : hex = "8"; break;
  314. case "1001" : hex = "9"; break;
  315. case "1010" : hex = "A"; break;
  316. case "1011" : hex = "B"; break;
  317. case "1100" : hex = "C"; break;
  318. case "1101" : hex = "D"; break;
  319. case "1110" : hex = "E"; break;
  320. case "1111" : hex = "F"; break;
  321. }
  322. return hex;
  323. }
  324. /*
  325. * chang the hex into the bit(it's length = 4)
  326. *
  327. * return the bit(it's length = 4)
  328. */
  329. function hexToBt4(hex) {
  330. var binary;
  331. switch (hex) {
  332. case "0" : binary = "0000"; break;
  333. case "1" : binary = "0001"; break;
  334. case "2" : binary = "0010"; break;
  335. case "3" : binary = "0011"; break;
  336. case "4" : binary = "0100"; break;
  337. case "5" : binary = "0101"; break;
  338. case "6" : binary = "0110"; break;
  339. case "7" : binary = "0111"; break;
  340. case "8" : binary = "1000"; break;
  341. case "9" : binary = "1001"; break;
  342. case "A" : binary = "1010"; break;
  343. case "B" : binary = "1011"; break;
  344. case "C" : binary = "1100"; break;
  345. case "D" : binary = "1101"; break;
  346. case "E" : binary = "1110"; break;
  347. case "F" : binary = "1111"; break;
  348. }
  349. return binary;
  350. }
  351. /*
  352. * chang the bit(it's length = 64) into the string
  353. *
  354. * return string
  355. */
  356. function byteToString(byteData){
  357. var str="";
  358. for(i = 0;i<4;i++){
  359. var count=0;
  360. for(j=0;j<16;j++){
  361. var pow=1;
  362. for(m=15;m>j;m--){
  363. pow*=2;
  364. }
  365. count+=byteData[16*i+j]*pow;
  366. }
  367. if(count != 0){
  368. str+=String.fromCharCode(count);
  369. }
  370. }
  371. return str;
  372. }
  373. function bt64ToHex(byteData){
  374. var hex = "";
  375. for(i = 0;i<16;i++){
  376. var bt = "";
  377. for(j=0;j<4;j++){
  378. bt += byteData[i*4+j];
  379. }
  380. hex+=bt4ToHex(bt);
  381. }
  382. return hex;
  383. }
  384. function hexToBt64(hex){
  385. var binary = "";
  386. for(i = 0;i<16;i++){
  387. binary+=hexToBt4(hex.substring(i,i+1));
  388. }
  389. return binary;
  390. }
  391. /*
  392. * the 64 bit des core arithmetic
  393. */
  394. function enc(dataByte,keyByte){
  395. var keys = generateKeys(keyByte);
  396. var ipByte = initPermute(dataByte);
  397. var ipLeft = new Array(32);
  398. var ipRight = new Array(32);
  399. var tempLeft = new Array(32);
  400. var i = 0,j = 0,k = 0,m = 0, n = 0;
  401. for(k = 0;k < 32;k ++){
  402. ipLeft[k] = ipByte[k];
  403. ipRight[k] = ipByte[32+k];
  404. }
  405. for(i = 0;i < 16;i ++){
  406. for(j = 0;j < 32;j ++){
  407. tempLeft[j] = ipLeft[j];
  408. ipLeft[j] = ipRight[j];
  409. }
  410. var key = new Array(48);
  411. for(m = 0;m < 48;m ++){
  412. key[m] = keys[i][m];
  413. }
  414. var tempRight = xor(pPermute(sBoxPermute(xor(expandPermute(ipRight),key))), tempLeft);
  415. for(n = 0;n < 32;n ++){
  416. ipRight[n] = tempRight[n];
  417. }
  418. }
  419. var finalData =new Array(64);
  420. for(i = 0;i < 32;i ++){
  421. finalData[i] = ipRight[i];
  422. finalData[32+i] = ipLeft[i];
  423. }
  424. return finallyPermute(finalData);
  425. }
  426. function dec(dataByte,keyByte){
  427. var keys = generateKeys(keyByte);
  428. var ipByte = initPermute(dataByte);
  429. var ipLeft = new Array(32);
  430. var ipRight = new Array(32);
  431. var tempLeft = new Array(32);
  432. var i = 0,j = 0,k = 0,m = 0, n = 0;
  433. for(k = 0;k < 32;k ++){
  434. ipLeft[k] = ipByte[k];
  435. ipRight[k] = ipByte[32+k];
  436. }
  437. for(i = 15;i >= 0;i --){
  438. for(j = 0;j < 32;j ++){
  439. tempLeft[j] = ipLeft[j];
  440. ipLeft[j] = ipRight[j];
  441. }
  442. var key = new Array(48);
  443. for(m = 0;m < 48;m ++){
  444. key[m] = keys[i][m];
  445. }
  446. var tempRight = xor(pPermute(sBoxPermute(xor(expandPermute(ipRight),key))), tempLeft);
  447. for(n = 0;n < 32;n ++){
  448. ipRight[n] = tempRight[n];
  449. }
  450. }
  451. var finalData =new Array(64);
  452. for(i = 0;i < 32;i ++){
  453. finalData[i] = ipRight[i];
  454. finalData[32+i] = ipLeft[i];
  455. }
  456. return finallyPermute(finalData);
  457. }
  458. function initPermute(originalData){
  459. var ipByte = new Array(64);
  460. for (i = 0, m = 1, n = 0; i < 4; i++, m += 2, n += 2) {
  461. for (j = 7, k = 0; j >= 0; j--, k++) {
  462. ipByte[i * 8 + k] = originalData[j * 8 + m];
  463. ipByte[i * 8 + k + 32] = originalData[j * 8 + n];
  464. }
  465. }
  466. return ipByte;
  467. }
  468. function expandPermute(rightData){
  469. var epByte = new Array(48);
  470. for (i = 0; i < 8; i++) {
  471. if (i == 0) {
  472. epByte[i * 6 + 0] = rightData[31];
  473. } else {
  474. epByte[i * 6 + 0] = rightData[i * 4 - 1];
  475. }
  476. epByte[i * 6 + 1] = rightData[i * 4 + 0];
  477. epByte[i * 6 + 2] = rightData[i * 4 + 1];
  478. epByte[i * 6 + 3] = rightData[i * 4 + 2];
  479. epByte[i * 6 + 4] = rightData[i * 4 + 3];
  480. if (i == 7) {
  481. epByte[i * 6 + 5] = rightData[0];
  482. } else {
  483. epByte[i * 6 + 5] = rightData[i * 4 + 4];
  484. }
  485. }
  486. return epByte;
  487. }
  488. function xor(byteOne,byteTwo){
  489. var xorByte = new Array(byteOne.length);
  490. for(i = 0;i < byteOne.length; i ++){
  491. xorByte[i] = byteOne[i] ^ byteTwo[i];
  492. }
  493. return xorByte;
  494. }
  495. function sBoxPermute(expandByte){
  496. var sBoxByte = new Array(32);
  497. var binary = "";
  498. var s1 = [
  499. [14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7],
  500. [0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8],
  501. [4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0],
  502. [15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 ]];
  503. /* Table - s2 */
  504. var s2 = [
  505. [15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10],
  506. [3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5],
  507. [0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15],
  508. [13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9 ]];
  509. /* Table - s3 */
  510. var s3= [
  511. [10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8],
  512. [13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1],
  513. [13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7],
  514. [1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 ]];
  515. /* Table - s4 */
  516. var s4 = [
  517. [7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15],
  518. [13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9],
  519. [10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4],
  520. [3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 ]];
  521. /* Table - s5 */
  522. var s5 = [
  523. [2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9],
  524. [14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6],
  525. [4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14],
  526. [11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3 ]];
  527. /* Table - s6 */
  528. var s6 = [
  529. [12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11],
  530. [10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8],
  531. [9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6],
  532. [4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 ]];
  533. /* Table - s7 */
  534. var s7 = [
  535. [4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1],
  536. [13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6],
  537. [1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2],
  538. [6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12]];
  539. /* Table - s8 */
  540. var s8 = [
  541. [13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7],
  542. [1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2],
  543. [7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8],
  544. [2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11]];
  545. for(m=0;m<8;m++){
  546. var i=0,j=0;
  547. i = expandByte[m*6+0]*2+expandByte[m*6+5];
  548. j = expandByte[m * 6 + 1] * 2 * 2 * 2
  549. + expandByte[m * 6 + 2] * 2* 2
  550. + expandByte[m * 6 + 3] * 2
  551. + expandByte[m * 6 + 4];
  552. switch (m) {
  553. case 0 :
  554. binary = getBoxBinary(s1[i][j]);
  555. break;
  556. case 1 :
  557. binary = getBoxBinary(s2[i][j]);
  558. break;
  559. case 2 :
  560. binary = getBoxBinary(s3[i][j]);
  561. break;
  562. case 3 :
  563. binary = getBoxBinary(s4[i][j]);
  564. break;
  565. case 4 :
  566. binary = getBoxBinary(s5[i][j]);
  567. break;
  568. case 5 :
  569. binary = getBoxBinary(s6[i][j]);
  570. break;
  571. case 6 :
  572. binary = getBoxBinary(s7[i][j]);
  573. break;
  574. case 7 :
  575. binary = getBoxBinary(s8[i][j]);
  576. break;
  577. }
  578. sBoxByte[m*4+0] = parseInt(binary.substring(0,1));
  579. sBoxByte[m*4+1] = parseInt(binary.substring(1,2));
  580. sBoxByte[m*4+2] = parseInt(binary.substring(2,3));
  581. sBoxByte[m*4+3] = parseInt(binary.substring(3,4));
  582. }
  583. return sBoxByte;
  584. }
  585. function pPermute(sBoxByte){
  586. var pBoxPermute = new Array(32);
  587. pBoxPermute[ 0] = sBoxByte[15];
  588. pBoxPermute[ 1] = sBoxByte[ 6];
  589. pBoxPermute[ 2] = sBoxByte[19];
  590. pBoxPermute[ 3] = sBoxByte[20];
  591. pBoxPermute[ 4] = sBoxByte[28];
  592. pBoxPermute[ 5] = sBoxByte[11];
  593. pBoxPermute[ 6] = sBoxByte[27];
  594. pBoxPermute[ 7] = sBoxByte[16];
  595. pBoxPermute[ 8] = sBoxByte[ 0];
  596. pBoxPermute[ 9] = sBoxByte[14];
  597. pBoxPermute[10] = sBoxByte[22];
  598. pBoxPermute[11] = sBoxByte[25];
  599. pBoxPermute[12] = sBoxByte[ 4];
  600. pBoxPermute[13] = sBoxByte[17];
  601. pBoxPermute[14] = sBoxByte[30];
  602. pBoxPermute[15] = sBoxByte[ 9];
  603. pBoxPermute[16] = sBoxByte[ 1];
  604. pBoxPermute[17] = sBoxByte[ 7];
  605. pBoxPermute[18] = sBoxByte[23];
  606. pBoxPermute[19] = sBoxByte[13];
  607. pBoxPermute[20] = sBoxByte[31];
  608. pBoxPermute[21] = sBoxByte[26];
  609. pBoxPermute[22] = sBoxByte[ 2];
  610. pBoxPermute[23] = sBoxByte[ 8];
  611. pBoxPermute[24] = sBoxByte[18];
  612. pBoxPermute[25] = sBoxByte[12];
  613. pBoxPermute[26] = sBoxByte[29];
  614. pBoxPermute[27] = sBoxByte[ 5];
  615. pBoxPermute[28] = sBoxByte[21];
  616. pBoxPermute[29] = sBoxByte[10];
  617. pBoxPermute[30] = sBoxByte[ 3];
  618. pBoxPermute[31] = sBoxByte[24];
  619. return pBoxPermute;
  620. }
  621. function finallyPermute(endByte){
  622. var fpByte = new Array(64);
  623. fpByte[ 0] = endByte[39];
  624. fpByte[ 1] = endByte[ 7];
  625. fpByte[ 2] = endByte[47];
  626. fpByte[ 3] = endByte[15];
  627. fpByte[ 4] = endByte[55];
  628. fpByte[ 5] = endByte[23];
  629. fpByte[ 6] = endByte[63];
  630. fpByte[ 7] = endByte[31];
  631. fpByte[ 8] = endByte[38];
  632. fpByte[ 9] = endByte[ 6];
  633. fpByte[10] = endByte[46];
  634. fpByte[11] = endByte[14];
  635. fpByte[12] = endByte[54];
  636. fpByte[13] = endByte[22];
  637. fpByte[14] = endByte[62];
  638. fpByte[15] = endByte[30];
  639. fpByte[16] = endByte[37];
  640. fpByte[17] = endByte[ 5];
  641. fpByte[18] = endByte[45];
  642. fpByte[19] = endByte[13];
  643. fpByte[20] = endByte[53];
  644. fpByte[21] = endByte[21];
  645. fpByte[22] = endByte[61];
  646. fpByte[23] = endByte[29];
  647. fpByte[24] = endByte[36];
  648. fpByte[25] = endByte[ 4];
  649. fpByte[26] = endByte[44];
  650. fpByte[27] = endByte[12];
  651. fpByte[28] = endByte[52];
  652. fpByte[29] = endByte[20];
  653. fpByte[30] = endByte[60];
  654. fpByte[31] = endByte[28];
  655. fpByte[32] = endByte[35];
  656. fpByte[33] = endByte[ 3];
  657. fpByte[34] = endByte[43];
  658. fpByte[35] = endByte[11];
  659. fpByte[36] = endByte[51];
  660. fpByte[37] = endByte[19];
  661. fpByte[38] = endByte[59];
  662. fpByte[39] = endByte[27];
  663. fpByte[40] = endByte[34];
  664. fpByte[41] = endByte[ 2];
  665. fpByte[42] = endByte[42];
  666. fpByte[43] = endByte[10];
  667. fpByte[44] = endByte[50];
  668. fpByte[45] = endByte[18];
  669. fpByte[46] = endByte[58];
  670. fpByte[47] = endByte[26];
  671. fpByte[48] = endByte[33];
  672. fpByte[49] = endByte[ 1];
  673. fpByte[50] = endByte[41];
  674. fpByte[51] = endByte[ 9];
  675. fpByte[52] = endByte[49];
  676. fpByte[53] = endByte[17];
  677. fpByte[54] = endByte[57];
  678. fpByte[55] = endByte[25];
  679. fpByte[56] = endByte[32];
  680. fpByte[57] = endByte[ 0];
  681. fpByte[58] = endByte[40];
  682. fpByte[59] = endByte[ 8];
  683. fpByte[60] = endByte[48];
  684. fpByte[61] = endByte[16];
  685. fpByte[62] = endByte[56];
  686. fpByte[63] = endByte[24];
  687. return fpByte;
  688. }
  689. function getBoxBinary(i) {
  690. var binary = "";
  691. switch (i) {
  692. case 0 :binary = "0000";break;
  693. case 1 :binary = "0001";break;
  694. case 2 :binary = "0010";break;
  695. case 3 :binary = "0011";break;
  696. case 4 :binary = "0100";break;
  697. case 5 :binary = "0101";break;
  698. case 6 :binary = "0110";break;
  699. case 7 :binary = "0111";break;
  700. case 8 :binary = "1000";break;
  701. case 9 :binary = "1001";break;
  702. case 10 :binary = "1010";break;
  703. case 11 :binary = "1011";break;
  704. case 12 :binary = "1100";break;
  705. case 13 :binary = "1101";break;
  706. case 14 :binary = "1110";break;
  707. case 15 :binary = "1111";break;
  708. }
  709. return binary;
  710. }
  711. /*
  712. * generate 16 keys for xor
  713. *
  714. */
  715. function generateKeys(keyByte){
  716. var key = new Array(56);
  717. var keys = new Array();
  718. keys[ 0] = new Array();
  719. keys[ 1] = new Array();
  720. keys[ 2] = new Array();
  721. keys[ 3] = new Array();
  722. keys[ 4] = new Array();
  723. keys[ 5] = new Array();
  724. keys[ 6] = new Array();
  725. keys[ 7] = new Array();
  726. keys[ 8] = new Array();
  727. keys[ 9] = new Array();
  728. keys[10] = new Array();
  729. keys[11] = new Array();
  730. keys[12] = new Array();
  731. keys[13] = new Array();
  732. keys[14] = new Array();
  733. keys[15] = new Array();
  734. var loop = [1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1];
  735. for(i=0;i<7;i++){
  736. for(j=0,k=7;j<8;j++,k--){
  737. key[i*8+j]=keyByte[8*k+i];
  738. }
  739. }
  740. var i = 0;
  741. for(i = 0;i < 16;i ++){
  742. var tempLeft=0;
  743. var tempRight=0;
  744. for(j = 0; j < loop[i];j ++){
  745. tempLeft = key[0];
  746. tempRight = key[28];
  747. for(k = 0;k < 27 ;k ++){
  748. key[k] = key[k+1];
  749. key[28+k] = key[29+k];
  750. }
  751. key[27]=tempLeft;
  752. key[55]=tempRight;
  753. }
  754. var tempKey = new Array(48);
  755. tempKey[ 0] = key[13];
  756. tempKey[ 1] = key[16];
  757. tempKey[ 2] = key[10];
  758. tempKey[ 3] = key[23];
  759. tempKey[ 4] = key[ 0];
  760. tempKey[ 5] = key[ 4];
  761. tempKey[ 6] = key[ 2];
  762. tempKey[ 7] = key[27];
  763. tempKey[ 8] = key[14];
  764. tempKey[ 9] = key[ 5];
  765. tempKey[10] = key[20];
  766. tempKey[11] = key[ 9];
  767. tempKey[12] = key[22];
  768. tempKey[13] = key[18];
  769. tempKey[14] = key[11];
  770. tempKey[15] = key[ 3];
  771. tempKey[16] = key[25];
  772. tempKey[17] = key[ 7];
  773. tempKey[18] = key[15];
  774. tempKey[19] = key[ 6];
  775. tempKey[20] = key[26];
  776. tempKey[21] = key[19];
  777. tempKey[22] = key[12];
  778. tempKey[23] = key[ 1];
  779. tempKey[24] = key[40];
  780. tempKey[25] = key[51];
  781. tempKey[26] = key[30];
  782. tempKey[27] = key[36];
  783. tempKey[28] = key[46];
  784. tempKey[29] = key[54];
  785. tempKey[30] = key[29];
  786. tempKey[31] = key[39];
  787. tempKey[32] = key[50];
  788. tempKey[33] = key[44];
  789. tempKey[34] = key[32];
  790. tempKey[35] = key[47];
  791. tempKey[36] = key[43];
  792. tempKey[37] = key[48];
  793. tempKey[38] = key[38];
  794. tempKey[39] = key[55];
  795. tempKey[40] = key[33];
  796. tempKey[41] = key[52];
  797. tempKey[42] = key[45];
  798. tempKey[43] = key[41];
  799. tempKey[44] = key[49];
  800. tempKey[45] = key[35];
  801. tempKey[46] = key[28];
  802. tempKey[47] = key[31];
  803. switch(i){
  804. case 0: for(m=0;m < 48 ;m++){ keys[ 0][m] = tempKey[m]; } break;
  805. case 1: for(m=0;m < 48 ;m++){ keys[ 1][m] = tempKey[m]; } break;
  806. case 2: for(m=0;m < 48 ;m++){ keys[ 2][m] = tempKey[m]; } break;
  807. case 3: for(m=0;m < 48 ;m++){ keys[ 3][m] = tempKey[m]; } break;
  808. case 4: for(m=0;m < 48 ;m++){ keys[ 4][m] = tempKey[m]; } break;
  809. case 5: for(m=0;m < 48 ;m++){ keys[ 5][m] = tempKey[m]; } break;
  810. case 6: for(m=0;m < 48 ;m++){ keys[ 6][m] = tempKey[m]; } break;
  811. case 7: for(m=0;m < 48 ;m++){ keys[ 7][m] = tempKey[m]; } break;
  812. case 8: for(m=0;m < 48 ;m++){ keys[ 8][m] = tempKey[m]; } break;
  813. case 9: for(m=0;m < 48 ;m++){ keys[ 9][m] = tempKey[m]; } break;
  814. case 10: for(m=0;m < 48 ;m++){ keys[10][m] = tempKey[m]; } break;
  815. case 11: for(m=0;m < 48 ;m++){ keys[11][m] = tempKey[m]; } break;
  816. case 12: for(m=0;m < 48 ;m++){ keys[12][m] = tempKey[m]; } break;
  817. case 13: for(m=0;m < 48 ;m++){ keys[13][m] = tempKey[m]; } break;
  818. case 14: for(m=0;m < 48 ;m++){ keys[14][m] = tempKey[m]; } break;
  819. case 15: for(m=0;m < 48 ;m++){ keys[15][m] = tempKey[m]; } break;
  820. }
  821. }
  822. return keys;
  823. }