908a483a7bca359b8303d337e4cf6a444a3ab192.svn-base 36 KB

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