Java代码

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

Js代码(des.js):

/**
* DES加密/解密
* @Copyright Copyright (c) 2015
* @author liuyazhuang
* @see DESCore
*/ /*
* encrypt the string to string made up of hex
* return the encrypted string
*/
function strEnc(data,firstKey,secondKey,thirdKey){
var leng = data.length;
var encData = "";
var firstKeyBt,secondKeyBt,thirdKeyBt,firstLength,secondLength,thirdLength;
if(firstKey != null && firstKey != ""){
firstKeyBt = getKeyBytes(firstKey);
firstLength = firstKeyBt.length;
}
if(secondKey != null && secondKey != ""){
secondKeyBt = getKeyBytes(secondKey);
secondLength = secondKeyBt.length;
}
if(thirdKey != null && thirdKey != ""){
thirdKeyBt = getKeyBytes(thirdKey);
thirdLength = thirdKeyBt.length;
} if(leng > 0){
if(leng < 4){
var bt = strToBt(data);
var encByte ;
if(firstKey != null && firstKey !="" && secondKey != null && secondKey != "" && thirdKey != null && thirdKey != ""){
var tempBt;
var x,y,z;
tempBt = bt;
for(x = 0;x < firstLength ;x ++){
tempBt = enc(tempBt,firstKeyBt[x]);
}
for(y = 0;y < secondLength ;y ++){
tempBt = enc(tempBt,secondKeyBt[y]);
}
for(z = 0;z < thirdLength ;z ++){
tempBt = enc(tempBt,thirdKeyBt[z]);
}
encByte = tempBt;
}else{
if(firstKey != null && firstKey !="" && secondKey != null && secondKey != ""){
var tempBt;
var x,y;
tempBt = bt;
for(x = 0;x < firstLength ;x ++){
tempBt = enc(tempBt,firstKeyBt[x]);
}
for(y = 0;y < secondLength ;y ++){
tempBt = enc(tempBt,secondKeyBt[y]);
}
encByte = tempBt;
}else{
if(firstKey != null && firstKey !=""){
var tempBt;
var x = 0;
tempBt = bt;
for(x = 0;x < firstLength ;x ++){
tempBt = enc(tempBt,firstKeyBt[x]);
}
encByte = tempBt;
}
}
}
encData = bt64ToHex(encByte);
}else{
var iterator = parseInt(leng/4);
var remainder = leng%4;
var i=0;
for(i = 0;i < iterator;i++){
var tempData = data.substring(i*4+0,i*4+4);
var tempByte = strToBt(tempData);
var encByte ;
if(firstKey != null && firstKey !="" && secondKey != null && secondKey != "" && thirdKey != null && thirdKey != ""){
var tempBt;
var x,y,z;
tempBt = tempByte;
for(x = 0;x < firstLength ;x ++){
tempBt = enc(tempBt,firstKeyBt[x]);
}
for(y = 0;y < secondLength ;y ++){
tempBt = enc(tempBt,secondKeyBt[y]);
}
for(z = 0;z < thirdLength ;z ++){
tempBt = enc(tempBt,thirdKeyBt[z]);
}
encByte = tempBt;
}else{
if(firstKey != null && firstKey !="" && secondKey != null && secondKey != ""){
var tempBt;
var x,y;
tempBt = tempByte;
for(x = 0;x < firstLength ;x ++){
tempBt = enc(tempBt,firstKeyBt[x]);
}
for(y = 0;y < secondLength ;y ++){
tempBt = enc(tempBt,secondKeyBt[y]);
}
encByte = tempBt;
}else{
if(firstKey != null && firstKey !=""){
var tempBt;
var x;
tempBt = tempByte;
for(x = 0;x < firstLength ;x ++){
tempBt = enc(tempBt,firstKeyBt[x]);
}
encByte = tempBt;
}
}
}
encData += bt64ToHex(encByte);
}
if(remainder > 0){
var remainderData = data.substring(iterator*4+0,leng);
var tempByte = strToBt(remainderData);
var encByte ;
if(firstKey != null && firstKey !="" && secondKey != null && secondKey != "" && thirdKey != null && thirdKey != ""){
var tempBt;
var x,y,z;
tempBt = tempByte;
for(x = 0;x < firstLength ;x ++){
tempBt = enc(tempBt,firstKeyBt[x]);
}
for(y = 0;y < secondLength ;y ++){
tempBt = enc(tempBt,secondKeyBt[y]);
}
for(z = 0;z < thirdLength ;z ++){
tempBt = enc(tempBt,thirdKeyBt[z]);
}
encByte = tempBt;
}else{
if(firstKey != null && firstKey !="" && secondKey != null && secondKey != ""){
var tempBt;
var x,y;
tempBt = tempByte;
for(x = 0;x < firstLength ;x ++){
tempBt = enc(tempBt,firstKeyBt[x]);
}
for(y = 0;y < secondLength ;y ++){
tempBt = enc(tempBt,secondKeyBt[y]);
}
encByte = tempBt;
}else{
if(firstKey != null && firstKey !=""){
var tempBt;
var x;
tempBt = tempByte;
for(x = 0;x < firstLength ;x ++){
tempBt = enc(tempBt,firstKeyBt[x]);
}
encByte = tempBt;
}
}
}
encData += bt64ToHex(encByte);
}
}
}
return encData;
} /*
* decrypt the encrypted string to the original string
*
* return the original string
*/
function strDec(data,firstKey,secondKey,thirdKey){
var leng = data.length;
var decStr = "";
var firstKeyBt,secondKeyBt,thirdKeyBt,firstLength,secondLength,thirdLength;
if(firstKey != null && firstKey != ""){
firstKeyBt = getKeyBytes(firstKey);
firstLength = firstKeyBt.length;
}
if(secondKey != null && secondKey != ""){
secondKeyBt = getKeyBytes(secondKey);
secondLength = secondKeyBt.length;
}
if(thirdKey != null && thirdKey != ""){
thirdKeyBt = getKeyBytes(thirdKey);
thirdLength = thirdKeyBt.length;
} var iterator = parseInt(leng/16);
var i=0;
for(i = 0;i < iterator;i++){
var tempData = data.substring(i*16+0,i*16+16);
var strByte = hexToBt64(tempData);
var intByte = new Array(64);
var j = 0;
for(j = 0;j < 64; j++){
intByte[j] = parseInt(strByte.substring(j,j+1));
}
var decByte;
if(firstKey != null && firstKey !="" && secondKey != null && secondKey != "" && thirdKey != null && thirdKey != ""){
var tempBt;
var x,y,z;
tempBt = intByte;
for(x = thirdLength - 1;x >= 0;x --){
tempBt = dec(tempBt,thirdKeyBt[x]);
}
for(y = secondLength - 1;y >= 0;y --){
tempBt = dec(tempBt,secondKeyBt[y]);
}
for(z = firstLength - 1;z >= 0 ;z --){
tempBt = dec(tempBt,firstKeyBt[z]);
}
decByte = tempBt;
}else{
if(firstKey != null && firstKey !="" && secondKey != null && secondKey != ""){
var tempBt;
var x,y,z;
tempBt = intByte;
for(x = secondLength - 1;x >= 0 ;x --){
tempBt = dec(tempBt,secondKeyBt[x]);
}
for(y = firstLength - 1;y >= 0 ;y --){
tempBt = dec(tempBt,firstKeyBt[y]);
}
decByte = tempBt;
}else{
if(firstKey != null && firstKey !=""){
var tempBt;
var x,y,z;
tempBt = intByte;
for(x = firstLength - 1;x >= 0 ;x --){
tempBt = dec(tempBt,firstKeyBt[x]);
}
decByte = tempBt;
}
}
}
decStr += byteToString(decByte);
}
return decStr;
}
/*
* chang the string into the bit array
*
* return bit array(it's length % 64 = 0)
*/
function getKeyBytes(key){
var keyBytes = new Array();
var leng = key.length;
var iterator = parseInt(leng/4);
var remainder = leng%4;
var i = 0;
for(i = 0;i < iterator; i ++){
keyBytes[i] = strToBt(key.substring(i*4+0,i*4+4));
}
if(remainder > 0){
keyBytes[i] = strToBt(key.substring(i*4+0,leng));
}
return keyBytes;
} /*
* chang the string(it's length <= 4) into the bit array
*
* return bit array(it's length = 64)
*/
function strToBt(str){
var leng = str.length;
var bt = new Array(64);
if(leng < 4){
var i=0,j=0,p=0,q=0;
for(i = 0;i<leng;i++){
var k = str.charCodeAt(i);
for(j=0;j<16;j++){
var pow=1,m=0;
for(m=15;m>j;m--){
pow *= 2;
}
bt[16*i+j]=parseInt(k/pow)%2;
}
}
for(p = leng;p<4;p++){
var k = 0;
for(q=0;q<16;q++){
var pow=1,m=0;
for(m=15;m>q;m--){
pow *= 2;
}
bt[16*p+q]=parseInt(k/pow)%2;
}
}
}else{
for(i = 0;i<4;i++){
var k = str.charCodeAt(i);
for(j=0;j<16;j++){
var pow=1;
for(m=15;m>j;m--){
pow *= 2;
}
bt[16*i+j]=parseInt(k/pow)%2;
}
}
}
return bt;
} /*
* chang the bit(it's length = 4) into the hex
*
* return hex
*/
function bt4ToHex(binary) {
var hex;
switch (binary) {
case "0000" : hex = "0"; break;
case "0001" : hex = "1"; break;
case "0010" : hex = "2"; break;
case "0011" : hex = "3"; break;
case "0100" : hex = "4"; break;
case "0101" : hex = "5"; break;
case "0110" : hex = "6"; break;
case "0111" : hex = "7"; break;
case "1000" : hex = "8"; break;
case "1001" : hex = "9"; break;
case "1010" : hex = "A"; break;
case "1011" : hex = "B"; break;
case "1100" : hex = "C"; break;
case "1101" : hex = "D"; break;
case "1110" : hex = "E"; break;
case "1111" : hex = "F"; break;
}
return hex;
} /*
* chang the hex into the bit(it's length = 4)
*
* return the bit(it's length = 4)
*/
function hexToBt4(hex) {
var binary;
switch (hex) {
case "0" : binary = "0000"; break;
case "1" : binary = "0001"; break;
case "2" : binary = "0010"; break;
case "3" : binary = "0011"; break;
case "4" : binary = "0100"; break;
case "5" : binary = "0101"; break;
case "6" : binary = "0110"; break;
case "7" : binary = "0111"; break;
case "8" : binary = "1000"; break;
case "9" : binary = "1001"; break;
case "A" : binary = "1010"; break;
case "B" : binary = "1011"; break;
case "C" : binary = "1100"; break;
case "D" : binary = "1101"; break;
case "E" : binary = "1110"; break;
case "F" : binary = "1111"; break;
}
return binary;
} /*
* chang the bit(it's length = 64) into the string
*
* return string
*/
function byteToString(byteData){
var str="";
for(i = 0;i<4;i++){
var count=0;
for(j=0;j<16;j++){
var pow=1;
for(m=15;m>j;m--){
pow*=2;
}
count+=byteData[16*i+j]*pow;
}
if(count != 0){
str+=String.fromCharCode(count);
}
}
return str;
} function bt64ToHex(byteData){
var hex = "";
for(i = 0;i<16;i++){
var bt = "";
for(j=0;j<4;j++){
bt += byteData[i*4+j];
}
hex+=bt4ToHex(bt);
}
return hex;
} function hexToBt64(hex){
var binary = "";
for(i = 0;i<16;i++){
binary+=hexToBt4(hex.substring(i,i+1));
}
return binary;
} /*
* the 64 bit des core arithmetic
*/ function enc(dataByte,keyByte){
var keys = generateKeys(keyByte);
var ipByte = initPermute(dataByte);
var ipLeft = new Array(32);
var ipRight = new Array(32);
var tempLeft = new Array(32);
var i = 0,j = 0,k = 0,m = 0, n = 0;
for(k = 0;k < 32;k ++){
ipLeft[k] = ipByte[k];
ipRight[k] = ipByte[32+k];
}
for(i = 0;i < 16;i ++){
for(j = 0;j < 32;j ++){
tempLeft[j] = ipLeft[j];
ipLeft[j] = ipRight[j];
}
var key = new Array(48);
for(m = 0;m < 48;m ++){
key[m] = keys[i][m];
}
var tempRight = xor(pPermute(sBoxPermute(xor(expandPermute(ipRight),key))), tempLeft);
for(n = 0;n < 32;n ++){
ipRight[n] = tempRight[n];
} } var finalData =new Array(64);
for(i = 0;i < 32;i ++){
finalData[i] = ipRight[i];
finalData[32+i] = ipLeft[i];
}
return finallyPermute(finalData);
} function dec(dataByte,keyByte){
var keys = generateKeys(keyByte);
var ipByte = initPermute(dataByte);
var ipLeft = new Array(32);
var ipRight = new Array(32);
var tempLeft = new Array(32);
var i = 0,j = 0,k = 0,m = 0, n = 0;
for(k = 0;k < 32;k ++){
ipLeft[k] = ipByte[k];
ipRight[k] = ipByte[32+k];
}
for(i = 15;i >= 0;i --){
for(j = 0;j < 32;j ++){
tempLeft[j] = ipLeft[j];
ipLeft[j] = ipRight[j];
}
var key = new Array(48);
for(m = 0;m < 48;m ++){
key[m] = keys[i][m];
} var tempRight = xor(pPermute(sBoxPermute(xor(expandPermute(ipRight),key))), tempLeft);
for(n = 0;n < 32;n ++){
ipRight[n] = tempRight[n];
}
} var finalData =new Array(64);
for(i = 0;i < 32;i ++){
finalData[i] = ipRight[i];
finalData[32+i] = ipLeft[i];
}
return finallyPermute(finalData);
} function initPermute(originalData){
var ipByte = new Array(64);
for (i = 0, m = 1, n = 0; i < 4; i++, m += 2, n += 2) {
for (j = 7, k = 0; j >= 0; j--, k++) {
ipByte[i * 8 + k] = originalData[j * 8 + m];
ipByte[i * 8 + k + 32] = originalData[j * 8 + n];
}
}
return ipByte;
} function expandPermute(rightData){
var epByte = new Array(48);
for (i = 0; i < 8; i++) {
if (i == 0) {
epByte[i * 6 + 0] = rightData[31];
} else {
epByte[i * 6 + 0] = rightData[i * 4 - 1];
}
epByte[i * 6 + 1] = rightData[i * 4 + 0];
epByte[i * 6 + 2] = rightData[i * 4 + 1];
epByte[i * 6 + 3] = rightData[i * 4 + 2];
epByte[i * 6 + 4] = rightData[i * 4 + 3];
if (i == 7) {
epByte[i * 6 + 5] = rightData[0];
} else {
epByte[i * 6 + 5] = rightData[i * 4 + 4];
}
}
return epByte;
} function xor(byteOne,byteTwo){
var xorByte = new Array(byteOne.length);
for(i = 0;i < byteOne.length; i ++){
xorByte[i] = byteOne[i] ^ byteTwo[i];
}
return xorByte;
} function sBoxPermute(expandByte){ var sBoxByte = new Array(32);
var binary = "";
var s1 = [
[14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7],
[0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8],
[4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0],
[15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 ]]; /* Table - s2 */
var s2 = [
[15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10],
[3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5],
[0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15],
[13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9 ]]; /* Table - s3 */
var s3= [
[10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8],
[13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1],
[13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7],
[1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 ]];
/* Table - s4 */
var s4 = [
[7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15],
[13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9],
[10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4],
[3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 ]]; /* Table - s5 */
var s5 = [
[2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9],
[14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6],
[4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14],
[11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3 ]]; /* Table - s6 */
var s6 = [
[12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11],
[10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8],
[9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6],
[4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 ]]; /* Table - s7 */
var s7 = [
[4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1],
[13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6],
[1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2],
[6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12]]; /* Table - s8 */
var s8 = [
[13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7],
[1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2],
[7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8],
[2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11]]; for(m=0;m<8;m++){
var i=0,j=0;
i = expandByte[m*6+0]*2+expandByte[m*6+5];
j = expandByte[m * 6 + 1] * 2 * 2 * 2
+ expandByte[m * 6 + 2] * 2* 2
+ expandByte[m * 6 + 3] * 2
+ expandByte[m * 6 + 4];
switch (m) {
case 0 :
binary = getBoxBinary(s1[i][j]);
break;
case 1 :
binary = getBoxBinary(s2[i][j]);
break;
case 2 :
binary = getBoxBinary(s3[i][j]);
break;
case 3 :
binary = getBoxBinary(s4[i][j]);
break;
case 4 :
binary = getBoxBinary(s5[i][j]);
break;
case 5 :
binary = getBoxBinary(s6[i][j]);
break;
case 6 :
binary = getBoxBinary(s7[i][j]);
break;
case 7 :
binary = getBoxBinary(s8[i][j]);
break;
}
sBoxByte[m*4+0] = parseInt(binary.substring(0,1));
sBoxByte[m*4+1] = parseInt(binary.substring(1,2));
sBoxByte[m*4+2] = parseInt(binary.substring(2,3));
sBoxByte[m*4+3] = parseInt(binary.substring(3,4));
}
return sBoxByte;
} function pPermute(sBoxByte){
var pBoxPermute = new Array(32);
pBoxPermute[ 0] = sBoxByte[15];
pBoxPermute[ 1] = sBoxByte[ 6];
pBoxPermute[ 2] = sBoxByte[19];
pBoxPermute[ 3] = sBoxByte[20];
pBoxPermute[ 4] = sBoxByte[28];
pBoxPermute[ 5] = sBoxByte[11];
pBoxPermute[ 6] = sBoxByte[27];
pBoxPermute[ 7] = sBoxByte[16];
pBoxPermute[ 8] = sBoxByte[ 0];
pBoxPermute[ 9] = sBoxByte[14];
pBoxPermute[10] = sBoxByte[22];
pBoxPermute[11] = sBoxByte[25];
pBoxPermute[12] = sBoxByte[ 4];
pBoxPermute[13] = sBoxByte[17];
pBoxPermute[14] = sBoxByte[30];
pBoxPermute[15] = sBoxByte[ 9];
pBoxPermute[16] = sBoxByte[ 1];
pBoxPermute[17] = sBoxByte[ 7];
pBoxPermute[18] = sBoxByte[23];
pBoxPermute[19] = sBoxByte[13];
pBoxPermute[20] = sBoxByte[31];
pBoxPermute[21] = sBoxByte[26];
pBoxPermute[22] = sBoxByte[ 2];
pBoxPermute[23] = sBoxByte[ 8];
pBoxPermute[24] = sBoxByte[18];
pBoxPermute[25] = sBoxByte[12];
pBoxPermute[26] = sBoxByte[29];
pBoxPermute[27] = sBoxByte[ 5];
pBoxPermute[28] = sBoxByte[21];
pBoxPermute[29] = sBoxByte[10];
pBoxPermute[30] = sBoxByte[ 3];
pBoxPermute[31] = sBoxByte[24];
return pBoxPermute;
} function finallyPermute(endByte){
var fpByte = new Array(64);
fpByte[ 0] = endByte[39];
fpByte[ 1] = endByte[ 7];
fpByte[ 2] = endByte[47];
fpByte[ 3] = endByte[15];
fpByte[ 4] = endByte[55];
fpByte[ 5] = endByte[23];
fpByte[ 6] = endByte[63];
fpByte[ 7] = endByte[31];
fpByte[ 8] = endByte[38];
fpByte[ 9] = endByte[ 6];
fpByte[10] = endByte[46];
fpByte[11] = endByte[14];
fpByte[12] = endByte[54];
fpByte[13] = endByte[22];
fpByte[14] = endByte[62];
fpByte[15] = endByte[30];
fpByte[16] = endByte[37];
fpByte[17] = endByte[ 5];
fpByte[18] = endByte[45];
fpByte[19] = endByte[13];
fpByte[20] = endByte[53];
fpByte[21] = endByte[21];
fpByte[22] = endByte[61];
fpByte[23] = endByte[29];
fpByte[24] = endByte[36];
fpByte[25] = endByte[ 4];
fpByte[26] = endByte[44];
fpByte[27] = endByte[12];
fpByte[28] = endByte[52];
fpByte[29] = endByte[20];
fpByte[30] = endByte[60];
fpByte[31] = endByte[28];
fpByte[32] = endByte[35];
fpByte[33] = endByte[ 3];
fpByte[34] = endByte[43];
fpByte[35] = endByte[11];
fpByte[36] = endByte[51];
fpByte[37] = endByte[19];
fpByte[38] = endByte[59];
fpByte[39] = endByte[27];
fpByte[40] = endByte[34];
fpByte[41] = endByte[ 2];
fpByte[42] = endByte[42];
fpByte[43] = endByte[10];
fpByte[44] = endByte[50];
fpByte[45] = endByte[18];
fpByte[46] = endByte[58];
fpByte[47] = endByte[26];
fpByte[48] = endByte[33];
fpByte[49] = endByte[ 1];
fpByte[50] = endByte[41];
fpByte[51] = endByte[ 9];
fpByte[52] = endByte[49];
fpByte[53] = endByte[17];
fpByte[54] = endByte[57];
fpByte[55] = endByte[25];
fpByte[56] = endByte[32];
fpByte[57] = endByte[ 0];
fpByte[58] = endByte[40];
fpByte[59] = endByte[ 8];
fpByte[60] = endByte[48];
fpByte[61] = endByte[16];
fpByte[62] = endByte[56];
fpByte[63] = endByte[24];
return fpByte;
} function getBoxBinary(i) {
var binary = "";
switch (i) {
case 0 :binary = "0000";break;
case 1 :binary = "0001";break;
case 2 :binary = "0010";break;
case 3 :binary = "0011";break;
case 4 :binary = "0100";break;
case 5 :binary = "0101";break;
case 6 :binary = "0110";break;
case 7 :binary = "0111";break;
case 8 :binary = "1000";break;
case 9 :binary = "1001";break;
case 10 :binary = "1010";break;
case 11 :binary = "1011";break;
case 12 :binary = "1100";break;
case 13 :binary = "1101";break;
case 14 :binary = "1110";break;
case 15 :binary = "1111";break;
}
return binary;
}
/*
* generate 16 keys for xor
*
*/
function generateKeys(keyByte){
var key = new Array(56);
var keys = new Array(); keys[ 0] = new Array();
keys[ 1] = new Array();
keys[ 2] = new Array();
keys[ 3] = new Array();
keys[ 4] = new Array();
keys[ 5] = new Array();
keys[ 6] = new Array();
keys[ 7] = new Array();
keys[ 8] = new Array();
keys[ 9] = new Array();
keys[10] = new Array();
keys[11] = new Array();
keys[12] = new Array();
keys[13] = new Array();
keys[14] = new Array();
keys[15] = new Array();
var loop = [1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1]; for(i=0;i<7;i++){
for(j=0,k=7;j<8;j++,k--){
key[i*8+j]=keyByte[8*k+i];
}
} var i = 0;
for(i = 0;i < 16;i ++){
var tempLeft=0;
var tempRight=0;
for(j = 0; j < loop[i];j ++){
tempLeft = key[0];
tempRight = key[28];
for(k = 0;k < 27 ;k ++){
key[k] = key[k+1];
key[28+k] = key[29+k];
}
key[27]=tempLeft;
key[55]=tempRight;
}
var tempKey = new Array(48);
tempKey[ 0] = key[13];
tempKey[ 1] = key[16];
tempKey[ 2] = key[10];
tempKey[ 3] = key[23];
tempKey[ 4] = key[ 0];
tempKey[ 5] = key[ 4];
tempKey[ 6] = key[ 2];
tempKey[ 7] = key[27];
tempKey[ 8] = key[14];
tempKey[ 9] = key[ 5];
tempKey[10] = key[20];
tempKey[11] = key[ 9];
tempKey[12] = key[22];
tempKey[13] = key[18];
tempKey[14] = key[11];
tempKey[15] = key[ 3];
tempKey[16] = key[25];
tempKey[17] = key[ 7];
tempKey[18] = key[15];
tempKey[19] = key[ 6];
tempKey[20] = key[26];
tempKey[21] = key[19];
tempKey[22] = key[12];
tempKey[23] = key[ 1];
tempKey[24] = key[40];
tempKey[25] = key[51];
tempKey[26] = key[30];
tempKey[27] = key[36];
tempKey[28] = key[46];
tempKey[29] = key[54];
tempKey[30] = key[29];
tempKey[31] = key[39];
tempKey[32] = key[50];
tempKey[33] = key[44];
tempKey[34] = key[32];
tempKey[35] = key[47];
tempKey[36] = key[43];
tempKey[37] = key[48];
tempKey[38] = key[38];
tempKey[39] = key[55];
tempKey[40] = key[33];
tempKey[41] = key[52];
tempKey[42] = key[45];
tempKey[43] = key[41];
tempKey[44] = key[49];
tempKey[45] = key[35];
tempKey[46] = key[28];
tempKey[47] = key[31];
switch(i){
case 0: for(m=0;m < 48 ;m++){ keys[ 0][m] = tempKey[m]; } break;
case 1: for(m=0;m < 48 ;m++){ keys[ 1][m] = tempKey[m]; } break;
case 2: for(m=0;m < 48 ;m++){ keys[ 2][m] = tempKey[m]; } break;
case 3: for(m=0;m < 48 ;m++){ keys[ 3][m] = tempKey[m]; } break;
case 4: for(m=0;m < 48 ;m++){ keys[ 4][m] = tempKey[m]; } break;
case 5: for(m=0;m < 48 ;m++){ keys[ 5][m] = tempKey[m]; } break;
case 6: for(m=0;m < 48 ;m++){ keys[ 6][m] = tempKey[m]; } break;
case 7: for(m=0;m < 48 ;m++){ keys[ 7][m] = tempKey[m]; } break;
case 8: for(m=0;m < 48 ;m++){ keys[ 8][m] = tempKey[m]; } break;
case 9: for(m=0;m < 48 ;m++){ keys[ 9][m] = tempKey[m]; } break;
case 10: for(m=0;m < 48 ;m++){ keys[10][m] = tempKey[m]; } break;
case 11: for(m=0;m < 48 ;m++){ keys[11][m] = tempKey[m]; } break;
case 12: for(m=0;m < 48 ;m++){ keys[12][m] = tempKey[m]; } break;
case 13: for(m=0;m < 48 ;m++){ keys[13][m] = tempKey[m]; } break;
case 14: for(m=0;m < 48 ;m++){ keys[14][m] = tempKey[m]; } break;
case 15: for(m=0;m < 48 ;m++){ keys[15][m] = tempKey[m]; } break;
}
}
return keys;
}

html测试代码:

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript" src="des.js"></script>
<script>
function getResult(){
//待加密字符串
var str = document.getElementById("str").innerHTML;
//第一个参数必须;第二个、第三个参数可选
var key1 = document.getElementById("key1").innerHTML;
var key2 = document.getElementById("key2").innerHTML;
var key3 = document.getElementById("key3").innerHTML;
//加密方法
var enResult = strEnc(str,key1,key2,key3);
//解密方法
var deResult = strDec(enResult,key1,key2,key3);
//展示结果
document.getElementById("enStr").innerHTML = enResult;
document.getElementById("dnStr").innerHTML = deResult;
}
</script>
</head>
<body>
<input type="button" value="获取加密结果与解密结果" onclick="getResult()" />
<table>
<tr>
<td align="left">字符串:</td>
<td><span id="str">admin</span></td>
</tr>
<tr>
<td>加密key:</td>
<td>key1=<span id="key1">1</span>;key2=<span id="key2">2</span>;key3=<span id="key3">3</span></td>
</tr>
<tr>
<td align="left">加密结果:</td>
<td align="left"><label id = "enStr"></label></td>
</tr>
<tr>
<td align="left">解密结果: </td>
<td align="left"><label id = "dnStr"></label></td>
</tr>
<table>
</body>
</html>

转自:http://blog.csdn.net/l1028386804/article/details/50196061

实现与JS相同的Des加解密算法【转】的更多相关文章

  1. JavaScript与C#互通的DES加解密算法

    原文地址:传送门 本文提供了一个能使JavaScript与C#互通的DES加解密算法的实现,在前台页面中用JavaScript版本的DES算法将数据加密之后,传到服务器端,在服务器端可用C#版本的DE ...

  2. DES加解密算法Qt实现

      算法解密qt加密table64bit [声明] (1) 本文源码 大部分源码来自:DES算法代码.在此基础上,利用Qt编程进行了改写,实现了DES加解密算法,并添加了文件加解密功能.在此对署名为b ...

  3. 实验一:C语言实现DES加解密算法

    计算程序执行10万次需要的时间: 总共需要175秒 加解密一次的时间小于:0.00175秒 纯计算加解密的时间会更短 去除IO操作后的时间 也就是说加解密一次的时间为0.07毫秒 /*-------- ...

  4. DES加解密算法(C语言实现)

    DES加密和解密算法的实现(C语言) 主要是做个记录,害怕以后代码丢了,先放到这里了. DES再不进行介绍了,可以看上一篇的 DES 的python实现 转载请注明出处:https://www.cnb ...

  5. Des加解密算法

    class DesHelper    {        /// <summary>        /// DES加密方法        /// </summary>       ...

  6. JavaScript与C#互通的DES加解密算法的实现(转)

    本文提供了一个能使JavaScript与C#互通的DES加解密算法的实现,在前台页面中用JavaScript版本的DES算法将数据加密之后,传到服务器端,在服务器端可用C#版本的DES解密算法将其解密 ...

  7. C#加解密算法

    先附上源码 加密解密算法目前已经应用到我们生活中的各个方面 加密用于达到以下目的: 保密性:帮助保护用户的标识或数据不被读取. 数据完整性:帮助保护数据不被更改. 身份验证:确保数据发自特定的一方. ...

  8. Des加解密(Java端和Js端配套)解析

    一.什么是DES加密        des对称加密,对称加密,是一种比较传统的加密方式,其加密运算.解密运算使用的是同样的密钥,信息的发送者和信息的接收者在进行信息的传输与处理时,必须共同持有该密码( ...

  9. javascript JS CryptoJS DES加解密CBC模式与C#DES加解密相同互通

    我们只知道不同的语言解密要相互通用,就需要遵循相同的加密方式,然而在具体做技术预研的时候,就发现会遇到很多问题,网上找的资料也是比较片面,所以我踩了坑,并且把解决方案和相关资料源码提供出来,给需要的朋 ...

随机推荐

  1. tcp.validnode_checking踩过的坑

    对Oracle 检查ip合法性,就必须在服务器端的sqlnet.ora文件中设置如下参数 TCP.INVITED_NODES=(10.0.0.36,10.0.0.1,10.0.0.35) TCP.EX ...

  2. vue 裁剪图片,插件Cropper的使用

    全局安装    npm install cropperjs 如果想本项目安装,方便移植:   import Cropper from 'cropperjs'   --save    这样的话,本地 p ...

  3. Javaweb学习笔记——(三)——————JavaScript基础&DOM基础

    day031.js的String对象 **创建String对象 ***var str = "abc"; **方法和属性(文档) ***属性 lenth:字符串的长度 ***方法 ( ...

  4. 基于802.11Fuzz技术的研究

    转自安全客 关于无线的Fuzz最开始接触了解时,国内基本毛线都搜不到.经过几个月的资料搜集和学习,将大约全网的fuzz资料整理翻译分析并读懂写下,就为填补国内空白,也希望无线爱好者能多多交流. 在各个 ...

  5. JavaScript之复杂对象的深拷贝(完全深拷贝)

    由于网上很多的深拷贝大都有如下问题: 1.灵活性.不能应对复杂对象(复杂对象是指:单一数组对象.多数组对象.多非数组对象.单一对象组合形成的复杂对象)的灵活拷贝 2.不变性.即 拷贝的对象与原对象的结 ...

  6. UE4联机编译光照

    UE4联机编译光照需要SwarmCoordinator以及SwarmAgent,在Engine\Binaries\DotNET目录下. SwarmAgent 我们主要关注Distribution Se ...

  7. Check Box、Radio Button、Combo Box控件使用

    Check Box.Radio Button.Combo Box控件使用 使用控件的方法 1.拖动控件到对话框 2. 定义控件对应的变量(值变量或者控件变量) 3.响应控件各种消息 Check Box ...

  8. WiFi基本知识【转】

    转自:http://blog.csdn.net/myarrow/article/details/7930131 1. IE802.11简介 标准号 IEEE 802.11b IEEE 802.11a ...

  9. Ajax+json+jquery实现无限瀑布流布局

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  10. mysql系列四、mySQL四舍五入函数用法总结

    一.MySQL四舍五入函数ROUND(x) ROUND(x)函数返回最接近于参数x的整数,对x值进行四舍五入. 实例: 使用ROUND(x)函数对操作数进行四舍五入操作.SQL语句如下: mysql& ...