Des.java 38 KB

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