DESTools.java 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  1. package com.miniframe.bisiness.service;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. public class DESTools {
  5. public static void main(String[] args) {
  6. DESTools desObj = new DESTools();
  7. String key1 = "1";
  8. String key2 = "2";
  9. String key3 = "3";
  10. String data = "admin";
  11. String str = desObj.strEnc(data, key1, key2, key3);
  12. System.out.println(str);
  13. String dec = desObj.strDec(str, key1, key2, key3);
  14. System.out.println(dec);
  15. }
  16. /**
  17. * 根据客户端密码和令牌解密
  18. * @param password
  19. * @param transToken
  20. * @return
  21. */
  22. public static String decPassword(String password,String transToken){
  23. DESTools des = new DESTools();
  24. return des.strDec(password,transToken.substring(4, 12),transToken.substring(12, 20),transToken.substring(20, 28));
  25. }
  26. /**
  27. * DES加密/解密
  28. *
  29. * @Copyright Copyright (c) 2006
  30. * @author Guapo
  31. * @see DESCore
  32. */
  33. /*
  34. * encrypt the string to string made up of hex return the encrypted string
  35. */
  36. @SuppressWarnings("rawtypes")
  37. public String strEnc(String data, String firstKey, String secondKey,
  38. String thirdKey) {
  39. int leng = data.length();
  40. String encData = "";
  41. List firstKeyBt = null, secondKeyBt = null, thirdKeyBt = null;
  42. int firstLength = 0, secondLength = 0, thirdLength = 0;
  43. if (firstKey != null && firstKey != "") {
  44. firstKeyBt = getKeyBytes(firstKey);
  45. firstLength = firstKeyBt.size();
  46. }
  47. if (secondKey != null && secondKey != "") {
  48. secondKeyBt = getKeyBytes(secondKey);
  49. secondLength = secondKeyBt.size();
  50. }
  51. if (thirdKey != null && thirdKey != "") {
  52. thirdKeyBt = getKeyBytes(thirdKey);
  53. thirdLength = thirdKeyBt.size();
  54. }
  55. if (leng > 0) {
  56. if (leng < 4) {
  57. int[] bt = strToBt(data);
  58. int[] encByte = null;
  59. if (firstKey != null && firstKey != "" && secondKey != null
  60. && secondKey != "" && thirdKey != null
  61. && thirdKey != "") {
  62. int[] tempBt;
  63. int x, y, z;
  64. tempBt = bt;
  65. for (x = 0; x < firstLength; x++) {
  66. tempBt = enc(tempBt, (int[]) firstKeyBt.get(x));
  67. }
  68. for (y = 0; y < secondLength; y++) {
  69. tempBt = enc(tempBt, (int[]) secondKeyBt.get(y));
  70. }
  71. for (z = 0; z < thirdLength; z++) {
  72. tempBt = enc(tempBt, (int[]) thirdKeyBt.get(z));
  73. }
  74. encByte = tempBt;
  75. } else {
  76. if (firstKey != null && firstKey != "" && secondKey != null
  77. && secondKey != "") {
  78. int[] tempBt;
  79. int x, y;
  80. tempBt = bt;
  81. for (x = 0; x < firstLength; x++) {
  82. tempBt = enc(tempBt, (int[]) firstKeyBt.get(x));
  83. }
  84. for (y = 0; y < secondLength; y++) {
  85. tempBt = enc(tempBt, (int[]) secondKeyBt.get(y));
  86. }
  87. encByte = tempBt;
  88. } else {
  89. if (firstKey != null && firstKey != "") {
  90. int[] tempBt;
  91. int x = 0;
  92. tempBt = bt;
  93. for (x = 0; x < firstLength; x++) {
  94. tempBt = enc(tempBt, (int[]) firstKeyBt.get(x));
  95. }
  96. encByte = tempBt;
  97. }
  98. }
  99. }
  100. encData = bt64ToHex(encByte);
  101. } else {
  102. int iterator = (leng / 4);
  103. int remainder = leng % 4;
  104. int i = 0;
  105. for (i = 0; i < iterator; i++) {
  106. String tempData = data.substring(i * 4 + 0, i * 4 + 4);
  107. int[] tempByte = strToBt(tempData);
  108. int[] encByte = null;
  109. if (firstKey != null && firstKey != "" && secondKey != null
  110. && secondKey != "" && thirdKey != null
  111. && thirdKey != "") {
  112. int[] tempBt;
  113. int x, y, z;
  114. tempBt = tempByte;
  115. for (x = 0; x < firstLength; x++) {
  116. tempBt = enc(tempBt, (int[]) firstKeyBt.get(x));
  117. }
  118. for (y = 0; y < secondLength; y++) {
  119. tempBt = enc(tempBt, (int[]) secondKeyBt.get(y));
  120. }
  121. for (z = 0; z < thirdLength; z++) {
  122. tempBt = enc(tempBt, (int[]) thirdKeyBt.get(z));
  123. }
  124. encByte = tempBt;
  125. } else {
  126. if (firstKey != null && firstKey != ""
  127. && secondKey != null && secondKey != "") {
  128. int[] tempBt;
  129. int x, y;
  130. tempBt = tempByte;
  131. for (x = 0; x < firstLength; x++) {
  132. tempBt = enc(tempBt, (int[]) firstKeyBt.get(x));
  133. }
  134. for (y = 0; y < secondLength; y++) {
  135. tempBt = enc(tempBt, (int[]) secondKeyBt.get(y));
  136. }
  137. encByte = tempBt;
  138. } else {
  139. if (firstKey != null && firstKey != "") {
  140. int[] tempBt;
  141. int x;
  142. tempBt = tempByte;
  143. for (x = 0; x < firstLength; x++) {
  144. tempBt = enc(tempBt, (int[]) firstKeyBt
  145. .get(x));
  146. }
  147. encByte = tempBt;
  148. }
  149. }
  150. }
  151. encData += bt64ToHex(encByte);
  152. }
  153. if (remainder > 0) {
  154. String remainderData = data.substring(iterator * 4 + 0,
  155. leng);
  156. int[] tempByte = strToBt(remainderData);
  157. int[] encByte = null;
  158. if (firstKey != null && firstKey != "" && secondKey != null
  159. && secondKey != "" && thirdKey != null
  160. && thirdKey != "") {
  161. int[] tempBt;
  162. int x, y, z;
  163. tempBt = tempByte;
  164. for (x = 0; x < firstLength; x++) {
  165. tempBt = enc(tempBt, (int[]) firstKeyBt.get(x));
  166. }
  167. for (y = 0; y < secondLength; y++) {
  168. tempBt = enc(tempBt, (int[]) secondKeyBt.get(y));
  169. }
  170. for (z = 0; z < thirdLength; z++) {
  171. tempBt = enc(tempBt, (int[]) thirdKeyBt.get(z));
  172. }
  173. encByte = tempBt;
  174. } else {
  175. if (firstKey != null && firstKey != ""
  176. && secondKey != null && secondKey != "") {
  177. int[] tempBt;
  178. int x, y;
  179. tempBt = tempByte;
  180. for (x = 0; x < firstLength; x++) {
  181. tempBt = enc(tempBt, (int[]) firstKeyBt.get(x));
  182. }
  183. for (y = 0; y < secondLength; y++) {
  184. tempBt = enc(tempBt, (int[]) secondKeyBt.get(y));
  185. }
  186. encByte = tempBt;
  187. } else {
  188. if (firstKey != null && firstKey != "") {
  189. int[] tempBt;
  190. int x;
  191. tempBt = tempByte;
  192. for (x = 0; x < firstLength; x++) {
  193. tempBt = enc(tempBt, (int[]) firstKeyBt
  194. .get(x));
  195. }
  196. encByte = tempBt;
  197. }
  198. }
  199. }
  200. encData += bt64ToHex(encByte);
  201. }
  202. }
  203. }
  204. return encData;
  205. }
  206. /*
  207. * decrypt the encrypted string to the original string
  208. *
  209. * return the original string
  210. */
  211. @SuppressWarnings({ "rawtypes", "unused" })
  212. public String strDec(String data, String firstKey, String secondKey,
  213. String thirdKey) {
  214. int leng = data.length();
  215. String decStr = "";
  216. List firstKeyBt = null, secondKeyBt = null, thirdKeyBt = null;
  217. int firstLength = 0, secondLength = 0, thirdLength = 0;
  218. if (firstKey != null && firstKey != "") {
  219. firstKeyBt = getKeyBytes(firstKey);
  220. firstLength = firstKeyBt.size();
  221. }
  222. if (secondKey != null && secondKey != "") {
  223. secondKeyBt = getKeyBytes(secondKey);
  224. secondLength = secondKeyBt.size();
  225. }
  226. if (thirdKey != null && thirdKey != "") {
  227. thirdKeyBt = getKeyBytes(thirdKey);
  228. thirdLength = thirdKeyBt.size();
  229. }
  230. int iterator = leng / 16;
  231. int i = 0;
  232. for (i = 0; i < iterator; i++) {
  233. String tempData = data.substring(i * 16 + 0, i * 16 + 16);
  234. String strByte = hexToBt64(tempData);
  235. int[] intByte = new int[64];
  236. int j = 0;
  237. for (j = 0; j < 64; j++) {
  238. intByte[j] = Integer.parseInt(strByte.substring(j, j + 1));
  239. }
  240. int[] decByte = null;
  241. if (firstKey != null && firstKey != "" && secondKey != null
  242. && secondKey != "" && thirdKey != null && thirdKey != "") {
  243. int[] tempBt;
  244. int x, y, z;
  245. tempBt = intByte;
  246. for (x = thirdLength - 1; x >= 0; x--) {
  247. tempBt = dec(tempBt, (int[]) thirdKeyBt.get(x));
  248. }
  249. for (y = secondLength - 1; y >= 0; y--) {
  250. tempBt = dec(tempBt, (int[]) secondKeyBt.get(y));
  251. }
  252. for (z = firstLength - 1; z >= 0; z--) {
  253. tempBt = dec(tempBt, (int[]) firstKeyBt.get(z));
  254. }
  255. decByte = tempBt;
  256. } else {
  257. if (firstKey != null && firstKey != "" && secondKey != null
  258. && secondKey != "") {
  259. int[] tempBt;
  260. int x, y, z;
  261. tempBt = intByte;
  262. for (x = secondLength - 1; x >= 0; x--) {
  263. tempBt = dec(tempBt, (int[]) secondKeyBt.get(x));
  264. }
  265. for (y = firstLength - 1; y >= 0; y--) {
  266. tempBt = dec(tempBt, (int[]) firstKeyBt.get(y));
  267. }
  268. decByte = tempBt;
  269. } else {
  270. if (firstKey != null && firstKey != "") {
  271. int[] tempBt;
  272. int x, y, z;
  273. tempBt = intByte;
  274. for (x = firstLength - 1; x >= 0; x--) {
  275. tempBt = dec(tempBt, (int[]) firstKeyBt.get(x));
  276. }
  277. decByte = tempBt;
  278. }
  279. }
  280. }
  281. decStr += byteToString(decByte);
  282. }
  283. return decStr;
  284. }
  285. /*
  286. * chang the string into the bit array
  287. *
  288. * return bit array(it's length % 64 = 0)
  289. */
  290. @SuppressWarnings({ "unchecked", "rawtypes" })
  291. public List getKeyBytes(String key) {
  292. List keyBytes = new ArrayList();
  293. int leng = key.length();
  294. int iterator = (leng / 4);
  295. int remainder = leng % 4;
  296. int i = 0;
  297. for (i = 0; i < iterator; i++) {
  298. keyBytes.add(i, strToBt(key.substring(i * 4 + 0, i * 4 + 4)));
  299. }
  300. if (remainder > 0) {
  301. // keyBytes[i] = strToBt(key.substring(i*4+0,leng));
  302. keyBytes.add(i, strToBt(key.substring(i * 4 + 0, leng)));
  303. }
  304. return keyBytes;
  305. }
  306. /*
  307. * chang the string(it's length <= 4) into the bit array
  308. *
  309. * return bit array(it's length = 64)
  310. */
  311. public int[] strToBt(String str) {
  312. int leng = str.length();
  313. int[] bt = new int[64];
  314. if (leng < 4) {
  315. int i = 0, j = 0, p = 0, q = 0;
  316. for (i = 0; i < leng; i++) {
  317. int k = str.charAt(i);
  318. for (j = 0; j < 16; j++) {
  319. int pow = 1, m = 0;
  320. for (m = 15; m > j; m--) {
  321. pow *= 2;
  322. }
  323. // bt.set(16*i+j,""+(k/pow)%2));
  324. bt[16 * i + j] = (k / pow) % 2;
  325. }
  326. }
  327. for (p = leng; p < 4; p++) {
  328. int k = 0;
  329. for (q = 0; q < 16; q++) {
  330. int pow = 1, m = 0;
  331. for (m = 15; m > q; m--) {
  332. pow *= 2;
  333. }
  334. // bt[16*p+q]=parseInt(k/pow)%2;
  335. // bt.add(16*p+q,""+((k/pow)%2));
  336. bt[16 * p + q] = (k / pow) % 2;
  337. }
  338. }
  339. } else {
  340. for (int i = 0; i < 4; i++) {
  341. int k = str.charAt(i);
  342. for (int j = 0; j < 16; j++) {
  343. int pow = 1;
  344. for (int m = 15; m > j; m--) {
  345. pow *= 2;
  346. }
  347. // bt[16*i+j]=parseInt(k/pow)%2;
  348. // bt.add(16*i+j,""+((k/pow)%2));
  349. bt[16 * i + j] = (k / pow) % 2;
  350. }
  351. }
  352. }
  353. return bt;
  354. }
  355. /*
  356. * chang the bit(it's length = 4) into the hex
  357. *
  358. * return hex
  359. */
  360. public String bt4ToHex(String binary) {
  361. String hex = "";
  362. if (binary.equalsIgnoreCase("0000")) {
  363. hex = "0";
  364. } else if (binary.equalsIgnoreCase("0001")) {
  365. hex = "1";
  366. } else if (binary.equalsIgnoreCase("0010")) {
  367. hex = "2";
  368. } else if (binary.equalsIgnoreCase("0011")) {
  369. hex = "3";
  370. } else if (binary.equalsIgnoreCase("0100")) {
  371. hex = "4";
  372. } else if (binary.equalsIgnoreCase("0101")) {
  373. hex = "5";
  374. } else if (binary.equalsIgnoreCase("0110")) {
  375. hex = "6";
  376. } else if (binary.equalsIgnoreCase("0111")) {
  377. hex = "7";
  378. } else if (binary.equalsIgnoreCase("1000")) {
  379. hex = "8";
  380. } else if (binary.equalsIgnoreCase("1001")) {
  381. hex = "9";
  382. } else if (binary.equalsIgnoreCase("1010")) {
  383. hex = "A";
  384. } else if (binary.equalsIgnoreCase("1011")) {
  385. hex = "B";
  386. } else if (binary.equalsIgnoreCase("1100")) {
  387. hex = "C";
  388. } else if (binary.equalsIgnoreCase("1101")) {
  389. hex = "D";
  390. } else if (binary.equalsIgnoreCase("1110")) {
  391. hex = "E";
  392. } else if (binary.equalsIgnoreCase("1111")) {
  393. hex = "F";
  394. }
  395. return hex;
  396. }
  397. /*
  398. * chang the hex into the bit(it's length = 4)
  399. *
  400. * return the bit(it's length = 4)
  401. */
  402. public String hexToBt4(String hex) {
  403. String binary = "";
  404. if (hex.equalsIgnoreCase("0")) {
  405. binary = "0000";
  406. } else if (hex.equalsIgnoreCase("1")) {
  407. binary = "0001";
  408. }
  409. if (hex.equalsIgnoreCase("2")) {
  410. binary = "0010";
  411. }
  412. if (hex.equalsIgnoreCase("3")) {
  413. binary = "0011";
  414. }
  415. if (hex.equalsIgnoreCase("4")) {
  416. binary = "0100";
  417. }
  418. if (hex.equalsIgnoreCase("5")) {
  419. binary = "0101";
  420. }
  421. if (hex.equalsIgnoreCase("6")) {
  422. binary = "0110";
  423. }
  424. if (hex.equalsIgnoreCase("7")) {
  425. binary = "0111";
  426. }
  427. if (hex.equalsIgnoreCase("8")) {
  428. binary = "1000";
  429. }
  430. if (hex.equalsIgnoreCase("9")) {
  431. binary = "1001";
  432. }
  433. if (hex.equalsIgnoreCase("A")) {
  434. binary = "1010";
  435. }
  436. if (hex.equalsIgnoreCase("B")) {
  437. binary = "1011";
  438. }
  439. if (hex.equalsIgnoreCase("C")) {
  440. binary = "1100";
  441. }
  442. if (hex.equalsIgnoreCase("D")) {
  443. binary = "1101";
  444. }
  445. if (hex.equalsIgnoreCase("E")) {
  446. binary = "1110";
  447. }
  448. if (hex.equalsIgnoreCase("F")) {
  449. binary = "1111";
  450. }
  451. return binary;
  452. }
  453. /*
  454. * chang the bit(it's length = 64) into the string
  455. *
  456. * return string
  457. */
  458. public String byteToString(int[] byteData) {
  459. String str = "";
  460. for (int i = 0; i < 4; i++) {
  461. int count = 0;
  462. for (int j = 0; j < 16; j++) {
  463. int pow = 1;
  464. for (int m = 15; m > j; m--) {
  465. pow *= 2;
  466. }
  467. count += byteData[16 * i + j] * pow;
  468. }
  469. if (count != 0) {
  470. str += "" + (char) (count);
  471. }
  472. }
  473. return str;
  474. }
  475. public String bt64ToHex(int[] byteData) {
  476. String hex = "";
  477. for (int i = 0; i < 16; i++) {
  478. String bt = "";
  479. for (int j = 0; j < 4; j++) {
  480. bt += byteData[i * 4 + j];
  481. }
  482. hex += bt4ToHex(bt);
  483. }
  484. return hex;
  485. }
  486. public String hexToBt64(String hex) {
  487. String binary = "";
  488. for (int i = 0; i < 16; i++) {
  489. binary += hexToBt4(hex.substring(i, i + 1));
  490. }
  491. return binary;
  492. }
  493. /*
  494. * the 64 bit des core arithmetic
  495. */
  496. public int[] enc(int[] dataByte, int[] keyByte) {
  497. int[][] keys = generateKeys(keyByte);
  498. int[] ipByte = initPermute(dataByte);
  499. int[] ipLeft = new int[32];
  500. int[] ipRight = new int[32];
  501. int[] tempLeft = new int[32];
  502. int i = 0, j = 0, k = 0, m = 0, n = 0;
  503. for (k = 0; k < 32; k++) {
  504. ipLeft[k] = ipByte[k];
  505. ipRight[k] = ipByte[32 + k];
  506. }
  507. for (i = 0; i < 16; i++) {
  508. for (j = 0; j < 32; j++) {
  509. tempLeft[j] = ipLeft[j];
  510. ipLeft[j] = ipRight[j];
  511. }
  512. int[] key = new int[48];
  513. for (m = 0; m < 48; m++) {
  514. key[m] = keys[i][m];
  515. }
  516. int[] tempRight = xor(pPermute(sBoxPermute(xor(
  517. expandPermute(ipRight), key))), tempLeft);
  518. for (n = 0; n < 32; n++) {
  519. ipRight[n] = tempRight[n];
  520. }
  521. }
  522. int[] finalData = new int[64];
  523. for (i = 0; i < 32; i++) {
  524. finalData[i] = ipRight[i];
  525. finalData[32 + i] = ipLeft[i];
  526. }
  527. return finallyPermute(finalData);
  528. }
  529. public int[] dec(int[] dataByte, int[] keyByte) {
  530. int[][] keys = generateKeys(keyByte);
  531. int[] ipByte = initPermute(dataByte);
  532. int[] ipLeft = new int[32];
  533. int[] ipRight = new int[32];
  534. int[] tempLeft = new int[32];
  535. int i = 0, j = 0, k = 0, m = 0, n = 0;
  536. for (k = 0; k < 32; k++) {
  537. ipLeft[k] = ipByte[k];
  538. ipRight[k] = ipByte[32 + k];
  539. }
  540. for (i = 15; i >= 0; i--) {
  541. for (j = 0; j < 32; j++) {
  542. tempLeft[j] = ipLeft[j];
  543. ipLeft[j] = ipRight[j];
  544. }
  545. int[] key = new int[48];
  546. for (m = 0; m < 48; m++) {
  547. key[m] = keys[i][m];
  548. }
  549. int[] tempRight = xor(pPermute(sBoxPermute(xor(
  550. expandPermute(ipRight), key))), tempLeft);
  551. for (n = 0; n < 32; n++) {
  552. ipRight[n] = tempRight[n];
  553. }
  554. }
  555. int[] finalData = new int[64];
  556. for (i = 0; i < 32; i++) {
  557. finalData[i] = ipRight[i];
  558. finalData[32 + i] = ipLeft[i];
  559. }
  560. return finallyPermute(finalData);
  561. }
  562. public int[] initPermute(int[] originalData) {
  563. int[] ipByte = new int[64];
  564. int i = 0, m = 1, n = 0, j, k;
  565. for (i = 0, m = 1, n = 0; i < 4; i++, m += 2, n += 2) {
  566. for (j = 7, k = 0; j >= 0; j--, k++) {
  567. ipByte[i * 8 + k] = originalData[j * 8 + m];
  568. ipByte[i * 8 + k + 32] = originalData[j * 8 + n];
  569. }
  570. }
  571. return ipByte;
  572. }
  573. @SuppressWarnings("unused")
  574. public int[] expandPermute(int[] rightData) {
  575. int[] epByte = new int[48];
  576. int i, j;
  577. for (i = 0; i < 8; i++) {
  578. if (i == 0) {
  579. epByte[i * 6 + 0] = rightData[31];
  580. } else {
  581. epByte[i * 6 + 0] = rightData[i * 4 - 1];
  582. }
  583. epByte[i * 6 + 1] = rightData[i * 4 + 0];
  584. epByte[i * 6 + 2] = rightData[i * 4 + 1];
  585. epByte[i * 6 + 3] = rightData[i * 4 + 2];
  586. epByte[i * 6 + 4] = rightData[i * 4 + 3];
  587. if (i == 7) {
  588. epByte[i * 6 + 5] = rightData[0];
  589. } else {
  590. epByte[i * 6 + 5] = rightData[i * 4 + 4];
  591. }
  592. }
  593. return epByte;
  594. }
  595. public int[] xor(int[] byteOne, int[] byteTwo) {
  596. // var xorByte = new Array(byteOne.length);
  597. // for(int i = 0;i < byteOne.length; i ++){
  598. // xorByte[i] = byteOne[i] ^ byteTwo[i];
  599. // }
  600. // return xorByte;
  601. int[] xorByte = new int[byteOne.length];
  602. for (int i = 0; i < byteOne.length; i++) {
  603. xorByte[i] = byteOne[i] ^ byteTwo[i];
  604. }
  605. return xorByte;
  606. }
  607. public int[] sBoxPermute(int[] expandByte) {
  608. // var sBoxByte = new Array(32);
  609. int[] sBoxByte = new int[32];
  610. String binary = "";
  611. int[][] s1 = {
  612. { 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7 },
  613. { 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8 },
  614. { 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0 },
  615. { 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 } };
  616. /* Table - s2 */
  617. int[][] s2 = {
  618. { 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10 },
  619. { 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5 },
  620. { 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15 },
  621. { 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9 } };
  622. /* Table - s3 */
  623. int[][] s3 = {
  624. { 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8 },
  625. { 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1 },
  626. { 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7 },
  627. { 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 } };
  628. /* Table - s4 */
  629. int[][] s4 = {
  630. { 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15 },
  631. { 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9 },
  632. { 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4 },
  633. { 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 } };
  634. /* Table - s5 */
  635. int[][] s5 = {
  636. { 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9 },
  637. { 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6 },
  638. { 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14 },
  639. { 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3 } };
  640. /* Table - s6 */
  641. int[][] s6 = {
  642. { 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11 },
  643. { 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8 },
  644. { 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6 },
  645. { 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 } };
  646. /* Table - s7 */
  647. int[][] s7 = {
  648. { 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1 },
  649. { 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6 },
  650. { 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2 },
  651. { 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12 } };
  652. /* Table - s8 */
  653. int[][] s8 = {
  654. { 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7 },
  655. { 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2 },
  656. { 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8 },
  657. { 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11 } };
  658. for (int m = 0; m < 8; m++) {
  659. int i = 0, j = 0;
  660. i = expandByte[m * 6 + 0] * 2 + expandByte[m * 6 + 5];
  661. j = expandByte[m * 6 + 1] * 2 * 2 * 2 + expandByte[m * 6 + 2] * 2
  662. * 2 + expandByte[m * 6 + 3] * 2 + expandByte[m * 6 + 4];
  663. switch (m) {
  664. case 0:
  665. binary = getBoxBinary(s1[i][j]);
  666. break;
  667. case 1:
  668. binary = getBoxBinary(s2[i][j]);
  669. break;
  670. case 2:
  671. binary = getBoxBinary(s3[i][j]);
  672. break;
  673. case 3:
  674. binary = getBoxBinary(s4[i][j]);
  675. break;
  676. case 4:
  677. binary = getBoxBinary(s5[i][j]);
  678. break;
  679. case 5:
  680. binary = getBoxBinary(s6[i][j]);
  681. break;
  682. case 6:
  683. binary = getBoxBinary(s7[i][j]);
  684. break;
  685. case 7:
  686. binary = getBoxBinary(s8[i][j]);
  687. break;
  688. }
  689. sBoxByte[m * 4 + 0] = Integer.parseInt(binary.substring(0, 1));
  690. sBoxByte[m * 4 + 1] = Integer.parseInt(binary.substring(1, 2));
  691. sBoxByte[m * 4 + 2] = Integer.parseInt(binary.substring(2, 3));
  692. sBoxByte[m * 4 + 3] = Integer.parseInt(binary.substring(3, 4));
  693. }
  694. return sBoxByte;
  695. }
  696. public int[] pPermute(int[] sBoxByte) {
  697. int[] pBoxPermute = new int[32];
  698. pBoxPermute[0] = sBoxByte[15];
  699. pBoxPermute[1] = sBoxByte[6];
  700. pBoxPermute[2] = sBoxByte[19];
  701. pBoxPermute[3] = sBoxByte[20];
  702. pBoxPermute[4] = sBoxByte[28];
  703. pBoxPermute[5] = sBoxByte[11];
  704. pBoxPermute[6] = sBoxByte[27];
  705. pBoxPermute[7] = sBoxByte[16];
  706. pBoxPermute[8] = sBoxByte[0];
  707. pBoxPermute[9] = sBoxByte[14];
  708. pBoxPermute[10] = sBoxByte[22];
  709. pBoxPermute[11] = sBoxByte[25];
  710. pBoxPermute[12] = sBoxByte[4];
  711. pBoxPermute[13] = sBoxByte[17];
  712. pBoxPermute[14] = sBoxByte[30];
  713. pBoxPermute[15] = sBoxByte[9];
  714. pBoxPermute[16] = sBoxByte[1];
  715. pBoxPermute[17] = sBoxByte[7];
  716. pBoxPermute[18] = sBoxByte[23];
  717. pBoxPermute[19] = sBoxByte[13];
  718. pBoxPermute[20] = sBoxByte[31];
  719. pBoxPermute[21] = sBoxByte[26];
  720. pBoxPermute[22] = sBoxByte[2];
  721. pBoxPermute[23] = sBoxByte[8];
  722. pBoxPermute[24] = sBoxByte[18];
  723. pBoxPermute[25] = sBoxByte[12];
  724. pBoxPermute[26] = sBoxByte[29];
  725. pBoxPermute[27] = sBoxByte[5];
  726. pBoxPermute[28] = sBoxByte[21];
  727. pBoxPermute[29] = sBoxByte[10];
  728. pBoxPermute[30] = sBoxByte[3];
  729. pBoxPermute[31] = sBoxByte[24];
  730. return pBoxPermute;
  731. }
  732. public int[] finallyPermute(int[] endByte) {
  733. int[] fpByte = new int[64];
  734. fpByte[0] = endByte[39];
  735. fpByte[1] = endByte[7];
  736. fpByte[2] = endByte[47];
  737. fpByte[3] = endByte[15];
  738. fpByte[4] = endByte[55];
  739. fpByte[5] = endByte[23];
  740. fpByte[6] = endByte[63];
  741. fpByte[7] = endByte[31];
  742. fpByte[8] = endByte[38];
  743. fpByte[9] = endByte[6];
  744. fpByte[10] = endByte[46];
  745. fpByte[11] = endByte[14];
  746. fpByte[12] = endByte[54];
  747. fpByte[13] = endByte[22];
  748. fpByte[14] = endByte[62];
  749. fpByte[15] = endByte[30];
  750. fpByte[16] = endByte[37];
  751. fpByte[17] = endByte[5];
  752. fpByte[18] = endByte[45];
  753. fpByte[19] = endByte[13];
  754. fpByte[20] = endByte[53];
  755. fpByte[21] = endByte[21];
  756. fpByte[22] = endByte[61];
  757. fpByte[23] = endByte[29];
  758. fpByte[24] = endByte[36];
  759. fpByte[25] = endByte[4];
  760. fpByte[26] = endByte[44];
  761. fpByte[27] = endByte[12];
  762. fpByte[28] = endByte[52];
  763. fpByte[29] = endByte[20];
  764. fpByte[30] = endByte[60];
  765. fpByte[31] = endByte[28];
  766. fpByte[32] = endByte[35];
  767. fpByte[33] = endByte[3];
  768. fpByte[34] = endByte[43];
  769. fpByte[35] = endByte[11];
  770. fpByte[36] = endByte[51];
  771. fpByte[37] = endByte[19];
  772. fpByte[38] = endByte[59];
  773. fpByte[39] = endByte[27];
  774. fpByte[40] = endByte[34];
  775. fpByte[41] = endByte[2];
  776. fpByte[42] = endByte[42];
  777. fpByte[43] = endByte[10];
  778. fpByte[44] = endByte[50];
  779. fpByte[45] = endByte[18];
  780. fpByte[46] = endByte[58];
  781. fpByte[47] = endByte[26];
  782. fpByte[48] = endByte[33];
  783. fpByte[49] = endByte[1];
  784. fpByte[50] = endByte[41];
  785. fpByte[51] = endByte[9];
  786. fpByte[52] = endByte[49];
  787. fpByte[53] = endByte[17];
  788. fpByte[54] = endByte[57];
  789. fpByte[55] = endByte[25];
  790. fpByte[56] = endByte[32];
  791. fpByte[57] = endByte[0];
  792. fpByte[58] = endByte[40];
  793. fpByte[59] = endByte[8];
  794. fpByte[60] = endByte[48];
  795. fpByte[61] = endByte[16];
  796. fpByte[62] = endByte[56];
  797. fpByte[63] = endByte[24];
  798. return fpByte;
  799. }
  800. public String getBoxBinary(int i) {
  801. String binary = "";
  802. switch (i) {
  803. case 0:
  804. binary = "0000";
  805. break;
  806. case 1:
  807. binary = "0001";
  808. break;
  809. case 2:
  810. binary = "0010";
  811. break;
  812. case 3:
  813. binary = "0011";
  814. break;
  815. case 4:
  816. binary = "0100";
  817. break;
  818. case 5:
  819. binary = "0101";
  820. break;
  821. case 6:
  822. binary = "0110";
  823. break;
  824. case 7:
  825. binary = "0111";
  826. break;
  827. case 8:
  828. binary = "1000";
  829. break;
  830. case 9:
  831. binary = "1001";
  832. break;
  833. case 10:
  834. binary = "1010";
  835. break;
  836. case 11:
  837. binary = "1011";
  838. break;
  839. case 12:
  840. binary = "1100";
  841. break;
  842. case 13:
  843. binary = "1101";
  844. break;
  845. case 14:
  846. binary = "1110";
  847. break;
  848. case 15:
  849. binary = "1111";
  850. break;
  851. }
  852. return binary;
  853. }
  854. /*
  855. * generate 16 keys for xor
  856. *
  857. */
  858. public int[][] generateKeys(int[] keyByte) {
  859. int[] key = new int[56];
  860. int[][] keys = new int[16][48];
  861. // keys[ 0] = new Array();
  862. // keys[ 1] = new Array();
  863. // keys[ 2] = new Array();
  864. // keys[ 3] = new Array();
  865. // keys[ 4] = new Array();
  866. // keys[ 5] = new Array();
  867. // keys[ 6] = new Array();
  868. // keys[ 7] = new Array();
  869. // keys[ 8] = new Array();
  870. // keys[ 9] = new Array();
  871. // keys[10] = new Array();
  872. // keys[11] = new Array();
  873. // keys[12] = new Array();
  874. // keys[13] = new Array();
  875. // keys[14] = new Array();
  876. // keys[15] = new Array();
  877. int[] loop = new int[] { 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 };
  878. for (int i = 0; i < 7; i++) {
  879. for (int j = 0, k = 7; j < 8; j++, k--) {
  880. key[i * 8 + j] = keyByte[8 * k + i];
  881. }
  882. }
  883. int i = 0;
  884. for (i = 0; i < 16; i++) {
  885. int tempLeft = 0;
  886. int tempRight = 0;
  887. for (int j = 0; j < loop[i]; j++) {
  888. tempLeft = key[0];
  889. tempRight = key[28];
  890. for (int k = 0; k < 27; k++) {
  891. key[k] = key[k + 1];
  892. key[28 + k] = key[29 + k];
  893. }
  894. key[27] = tempLeft;
  895. key[55] = tempRight;
  896. }
  897. // var tempKey = new Array(48);
  898. int[] tempKey = new int[48];
  899. tempKey[0] = key[13];
  900. tempKey[1] = key[16];
  901. tempKey[2] = key[10];
  902. tempKey[3] = key[23];
  903. tempKey[4] = key[0];
  904. tempKey[5] = key[4];
  905. tempKey[6] = key[2];
  906. tempKey[7] = key[27];
  907. tempKey[8] = key[14];
  908. tempKey[9] = key[5];
  909. tempKey[10] = key[20];
  910. tempKey[11] = key[9];
  911. tempKey[12] = key[22];
  912. tempKey[13] = key[18];
  913. tempKey[14] = key[11];
  914. tempKey[15] = key[3];
  915. tempKey[16] = key[25];
  916. tempKey[17] = key[7];
  917. tempKey[18] = key[15];
  918. tempKey[19] = key[6];
  919. tempKey[20] = key[26];
  920. tempKey[21] = key[19];
  921. tempKey[22] = key[12];
  922. tempKey[23] = key[1];
  923. tempKey[24] = key[40];
  924. tempKey[25] = key[51];
  925. tempKey[26] = key[30];
  926. tempKey[27] = key[36];
  927. tempKey[28] = key[46];
  928. tempKey[29] = key[54];
  929. tempKey[30] = key[29];
  930. tempKey[31] = key[39];
  931. tempKey[32] = key[50];
  932. tempKey[33] = key[44];
  933. tempKey[34] = key[32];
  934. tempKey[35] = key[47];
  935. tempKey[36] = key[43];
  936. tempKey[37] = key[48];
  937. tempKey[38] = key[38];
  938. tempKey[39] = key[55];
  939. tempKey[40] = key[33];
  940. tempKey[41] = key[52];
  941. tempKey[42] = key[45];
  942. tempKey[43] = key[41];
  943. tempKey[44] = key[49];
  944. tempKey[45] = key[35];
  945. tempKey[46] = key[28];
  946. tempKey[47] = key[31];
  947. int m;
  948. switch (i) {
  949. case 0:
  950. for (m = 0; m < 48; m++) {
  951. keys[0][m] = tempKey[m];
  952. }
  953. break;
  954. case 1:
  955. for (m = 0; m < 48; m++) {
  956. keys[1][m] = tempKey[m];
  957. }
  958. break;
  959. case 2:
  960. for (m = 0; m < 48; m++) {
  961. keys[2][m] = tempKey[m];
  962. }
  963. break;
  964. case 3:
  965. for (m = 0; m < 48; m++) {
  966. keys[3][m] = tempKey[m];
  967. }
  968. break;
  969. case 4:
  970. for (m = 0; m < 48; m++) {
  971. keys[4][m] = tempKey[m];
  972. }
  973. break;
  974. case 5:
  975. for (m = 0; m < 48; m++) {
  976. keys[5][m] = tempKey[m];
  977. }
  978. break;
  979. case 6:
  980. for (m = 0; m < 48; m++) {
  981. keys[6][m] = tempKey[m];
  982. }
  983. break;
  984. case 7:
  985. for (m = 0; m < 48; m++) {
  986. keys[7][m] = tempKey[m];
  987. }
  988. break;
  989. case 8:
  990. for (m = 0; m < 48; m++) {
  991. keys[8][m] = tempKey[m];
  992. }
  993. break;
  994. case 9:
  995. for (m = 0; m < 48; m++) {
  996. keys[9][m] = tempKey[m];
  997. }
  998. break;
  999. case 10:
  1000. for (m = 0; m < 48; m++) {
  1001. keys[10][m] = tempKey[m];
  1002. }
  1003. break;
  1004. case 11:
  1005. for (m = 0; m < 48; m++) {
  1006. keys[11][m] = tempKey[m];
  1007. }
  1008. break;
  1009. case 12:
  1010. for (m = 0; m < 48; m++) {
  1011. keys[12][m] = tempKey[m];
  1012. }
  1013. break;
  1014. case 13:
  1015. for (m = 0; m < 48; m++) {
  1016. keys[13][m] = tempKey[m];
  1017. }
  1018. break;
  1019. case 14:
  1020. for (m = 0; m < 48; m++) {
  1021. keys[14][m] = tempKey[m];
  1022. }
  1023. break;
  1024. case 15:
  1025. for (m = 0; m < 48; m++) {
  1026. keys[15][m] = tempKey[m];
  1027. }
  1028. break;
  1029. }
  1030. }
  1031. return keys;
  1032. }
  1033. }