JavaScript代码    DES.js

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

des.html

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript" src="../js/DES.js"></script>
<script>
function getResult(){
//待加密字符串
var str = document.getElementById("str").innerText;
//第一个参数必须;第二个、第三个参数可选
var key1 = document.getElementById("key1").innerText;
var key2 = document.getElementById("key2").innerText;
var key3 = document.getElementById("key3").innerText;
//加密方法
var enResult = strEnc(str,key1,key2,key3);
//解密方法
var deResult = strDec(enResult,key1,key2,key3);
//展示结果
document.getElementById("enStr").innerText = enResult;
document.getElementById("dnStr").innerText = 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>

java代码

package des;
import java.util.ArrayList;
import java.util.List; public class Des {
public Des() {
}
public static void main(String[] args) {
Des desObj = new Des();
String key1 = "1";
String key2 = "2";
String key3 = "3";
String data = "admin";
String str = desObj.strEnc(data, key1, key2, key3);
System.out.println(str);
String dec = desObj.strDec(str, key1, key2, key3);
System.out.println(dec);
} /**
* DES加密/解密
*
* @Copyright Copyright (c) 2006
* @author Guapo
* @see DESCore
*/ /*
* encrypt the string to string made up of hex return the encrypted string
*/
public String strEnc(String data, String firstKey, String secondKey,
String thirdKey) { int leng = data.length();
String encData = "";
List firstKeyBt = null, secondKeyBt = null, thirdKeyBt = null;
int firstLength = 0, secondLength = 0, thirdLength = 0;
if (firstKey != null && firstKey != "") {
firstKeyBt = getKeyBytes(firstKey);
firstLength = firstKeyBt.size();
}
if (secondKey != null && secondKey != "") {
secondKeyBt = getKeyBytes(secondKey);
secondLength = secondKeyBt.size();
}
if (thirdKey != null && thirdKey != "") {
thirdKeyBt = getKeyBytes(thirdKey);
thirdLength = thirdKeyBt.size();
} if (leng > 0) {
if (leng < 4) {
int[] bt = strToBt(data);
int[] encByte = null;
if (firstKey != null && firstKey != "" && secondKey != null
&& secondKey != "" && thirdKey != null
&& thirdKey != "") {
int[] tempBt;
int x, y, z;
tempBt = bt;
for (x = 0; x < firstLength; x++) {
tempBt = enc(tempBt, (int[]) firstKeyBt.get(x));
}
for (y = 0; y < secondLength; y++) {
tempBt = enc(tempBt, (int[]) secondKeyBt.get(y));
}
for (z = 0; z < thirdLength; z++) {
tempBt = enc(tempBt, (int[]) thirdKeyBt.get(z));
}
encByte = tempBt;
} else {
if (firstKey != null && firstKey != "" && secondKey != null
&& secondKey != "") {
int[] tempBt;
int x, y;
tempBt = bt;
for (x = 0; x < firstLength; x++) {
tempBt = enc(tempBt, (int[]) firstKeyBt.get(x));
}
for (y = 0; y < secondLength; y++) {
tempBt = enc(tempBt, (int[]) secondKeyBt.get(y));
}
encByte = tempBt;
} else {
if (firstKey != null && firstKey != "") {
int[] tempBt;
int x = 0;
tempBt = bt;
for (x = 0; x < firstLength; x++) {
tempBt = enc(tempBt, (int[]) firstKeyBt.get(x));
}
encByte = tempBt;
}
}
}
encData = bt64ToHex(encByte);
} else {
int iterator = (leng / 4);
int remainder = leng % 4;
int i = 0;
for (i = 0; i < iterator; i++) {
String tempData = data.substring(i * 4 + 0, i * 4 + 4);
int[] tempByte = strToBt(tempData);
int[] encByte = null;
if (firstKey != null && firstKey != "" && secondKey != null
&& secondKey != "" && thirdKey != null
&& thirdKey != "") {
int[] tempBt;
int x, y, z;
tempBt = tempByte;
for (x = 0; x < firstLength; x++) {
tempBt = enc(tempBt, (int[]) firstKeyBt.get(x));
}
for (y = 0; y < secondLength; y++) {
tempBt = enc(tempBt, (int[]) secondKeyBt.get(y));
}
for (z = 0; z < thirdLength; z++) {
tempBt = enc(tempBt, (int[]) thirdKeyBt.get(z));
}
encByte = tempBt;
} else {
if (firstKey != null && firstKey != ""
&& secondKey != null && secondKey != "") {
int[] tempBt;
int x, y;
tempBt = tempByte;
for (x = 0; x < firstLength; x++) {
tempBt = enc(tempBt, (int[]) firstKeyBt.get(x));
}
for (y = 0; y < secondLength; y++) {
tempBt = enc(tempBt, (int[]) secondKeyBt.get(y));
}
encByte = tempBt;
} else {
if (firstKey != null && firstKey != "") {
int[] tempBt;
int x;
tempBt = tempByte;
for (x = 0; x < firstLength; x++) {
tempBt = enc(tempBt, (int[]) firstKeyBt
.get(x));
}
encByte = tempBt;
}
}
}
encData += bt64ToHex(encByte);
}
if (remainder > 0) {
String remainderData = data.substring(iterator * 4 + 0,
leng);
int[] tempByte = strToBt(remainderData);
int[] encByte = null;
if (firstKey != null && firstKey != "" && secondKey != null
&& secondKey != "" && thirdKey != null
&& thirdKey != "") {
int[] tempBt;
int x, y, z;
tempBt = tempByte;
for (x = 0; x < firstLength; x++) {
tempBt = enc(tempBt, (int[]) firstKeyBt.get(x));
}
for (y = 0; y < secondLength; y++) {
tempBt = enc(tempBt, (int[]) secondKeyBt.get(y));
}
for (z = 0; z < thirdLength; z++) {
tempBt = enc(tempBt, (int[]) thirdKeyBt.get(z));
}
encByte = tempBt;
} else {
if (firstKey != null && firstKey != ""
&& secondKey != null && secondKey != "") {
int[] tempBt;
int x, y;
tempBt = tempByte;
for (x = 0; x < firstLength; x++) {
tempBt = enc(tempBt, (int[]) firstKeyBt.get(x));
}
for (y = 0; y < secondLength; y++) {
tempBt = enc(tempBt, (int[]) secondKeyBt.get(y));
}
encByte = tempBt;
} else {
if (firstKey != null && firstKey != "") {
int[] tempBt;
int x;
tempBt = tempByte;
for (x = 0; x < firstLength; x++) {
tempBt = enc(tempBt, (int[]) firstKeyBt
.get(x));
}
encByte = tempBt;
}
}
}
encData += bt64ToHex(encByte);
}
}
}
return encData;
} /*
* decrypt the encrypted string to the original string
*
* return the original string
*/
public String strDec(String data, String firstKey, String secondKey,
String thirdKey) {
int leng = data.length();
String decStr = "";
List firstKeyBt = null, secondKeyBt = null, thirdKeyBt = null;
int firstLength = 0, secondLength = 0, thirdLength = 0;
if (firstKey != null && firstKey != "") {
firstKeyBt = getKeyBytes(firstKey);
firstLength = firstKeyBt.size();
}
if (secondKey != null && secondKey != "") {
secondKeyBt = getKeyBytes(secondKey);
secondLength = secondKeyBt.size();
}
if (thirdKey != null && thirdKey != "") {
thirdKeyBt = getKeyBytes(thirdKey);
thirdLength = thirdKeyBt.size();
} int iterator = leng / 16;
int i = 0;
for (i = 0; i < iterator; i++) {
String tempData = data.substring(i * 16 + 0, i * 16 + 16);
String strByte = hexToBt64(tempData);
int[] intByte = new int[64];
int j = 0;
for (j = 0; j < 64; j++) {
intByte[j] = Integer.parseInt(strByte.substring(j, j + 1));
}
int[] decByte = null;
if (firstKey != null && firstKey != "" && secondKey != null
&& secondKey != "" && thirdKey != null && thirdKey != "") {
int[] tempBt;
int x, y, z;
tempBt = intByte;
for (x = thirdLength - 1; x >= 0; x--) {
tempBt = dec(tempBt, (int[]) thirdKeyBt.get(x));
}
for (y = secondLength - 1; y >= 0; y--) {
tempBt = dec(tempBt, (int[]) secondKeyBt.get(y));
}
for (z = firstLength - 1; z >= 0; z--) {
tempBt = dec(tempBt, (int[]) firstKeyBt.get(z));
}
decByte = tempBt;
} else {
if (firstKey != null && firstKey != "" && secondKey != null
&& secondKey != "") {
int[] tempBt;
int x, y, z;
tempBt = intByte;
for (x = secondLength - 1; x >= 0; x--) {
tempBt = dec(tempBt, (int[]) secondKeyBt.get(x));
}
for (y = firstLength - 1; y >= 0; y--) {
tempBt = dec(tempBt, (int[]) firstKeyBt.get(y));
}
decByte = tempBt;
} else {
if (firstKey != null && firstKey != "") {
int[] tempBt;
int x, y, z;
tempBt = intByte;
for (x = firstLength - 1; x >= 0; x--) {
tempBt = dec(tempBt, (int[]) firstKeyBt.get(x));
}
decByte = tempBt;
}
}
}
decStr += byteToString(decByte);
}
return decStr;
} /*
* chang the string into the bit array
*
* return bit array(it's length % 64 = 0)
*/
public List getKeyBytes(String key) {
List keyBytes = new ArrayList();
int leng = key.length();
int iterator = (leng / 4);
int remainder = leng % 4;
int i = 0;
for (i = 0; i < iterator; i++) {
keyBytes.add(i, strToBt(key.substring(i * 4 + 0, i * 4 + 4)));
}
if (remainder > 0) {
// keyBytes[i] = strToBt(key.substring(i*4+0,leng));
keyBytes.add(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)
*/
public int[] strToBt(String str) {
int leng = str.length();
int[] bt = new int[64];
if (leng < 4) {
int i = 0, j = 0, p = 0, q = 0;
for (i = 0; i < leng; i++) {
int k = str.charAt(i);
for (j = 0; j < 16; j++) {
int pow = 1, m = 0;
for (m = 15; m > j; m--) {
pow *= 2;
}
// bt.set(16*i+j,""+(k/pow)%2));
bt[16 * i + j] = (k / pow) % 2;
}
}
for (p = leng; p < 4; p++) {
int k = 0;
for (q = 0; q < 16; q++) {
int pow = 1, m = 0;
for (m = 15; m > q; m--) {
pow *= 2;
}
// bt[16*p+q]=parseInt(k/pow)%2;
// bt.add(16*p+q,""+((k/pow)%2));
bt[16 * p + q] = (k / pow) % 2;
}
}
} else {
for (int i = 0; i < 4; i++) {
int k = str.charAt(i);
for (int j = 0; j < 16; j++) {
int pow = 1;
for (int m = 15; m > j; m--) {
pow *= 2;
}
// bt[16*i+j]=parseInt(k/pow)%2;
// bt.add(16*i+j,""+((k/pow)%2));
bt[16 * i + j] = (k / pow) % 2;
}
}
}
return bt;
} /*
* chang the bit(it's length = 4) into the hex
*
* return hex
*/
public String bt4ToHex(String binary) {
String hex = "";
if (binary.equalsIgnoreCase("0000")) {
hex = "0";
} else if (binary.equalsIgnoreCase("0001")) {
hex = "1";
} else if (binary.equalsIgnoreCase("0010")) {
hex = "2";
} else if (binary.equalsIgnoreCase("0011")) {
hex = "3";
} else if (binary.equalsIgnoreCase("0100")) {
hex = "4";
} else if (binary.equalsIgnoreCase("0101")) {
hex = "5";
} else if (binary.equalsIgnoreCase("0110")) {
hex = "6";
} else if (binary.equalsIgnoreCase("0111")) {
hex = "7";
} else if (binary.equalsIgnoreCase("1000")) {
hex = "8";
} else if (binary.equalsIgnoreCase("1001")) {
hex = "9";
} else if (binary.equalsIgnoreCase("1010")) {
hex = "A";
} else if (binary.equalsIgnoreCase("1011")) {
hex = "B";
} else if (binary.equalsIgnoreCase("1100")) {
hex = "C";
} else if (binary.equalsIgnoreCase("1101")) {
hex = "D";
} else if (binary.equalsIgnoreCase("1110")) {
hex = "E";
} else if (binary.equalsIgnoreCase("1111")) {
hex = "F";
} return hex;
} /*
* chang the hex into the bit(it's length = 4)
*
* return the bit(it's length = 4)
*/
public String hexToBt4(String hex) {
String binary = "";
if (hex.equalsIgnoreCase("0")) {
binary = "0000";
} else if (hex.equalsIgnoreCase("1")) {
binary = "0001";
}
if (hex.equalsIgnoreCase("2")) {
binary = "0010";
}
if (hex.equalsIgnoreCase("3")) {
binary = "0011";
}
if (hex.equalsIgnoreCase("4")) {
binary = "0100";
}
if (hex.equalsIgnoreCase("5")) {
binary = "0101";
}
if (hex.equalsIgnoreCase("6")) {
binary = "0110";
}
if (hex.equalsIgnoreCase("7")) {
binary = "0111";
}
if (hex.equalsIgnoreCase("8")) {
binary = "1000";
}
if (hex.equalsIgnoreCase("9")) {
binary = "1001";
}
if (hex.equalsIgnoreCase("A")) {
binary = "1010";
}
if (hex.equalsIgnoreCase("B")) {
binary = "1011";
}
if (hex.equalsIgnoreCase("C")) {
binary = "1100";
}
if (hex.equalsIgnoreCase("D")) {
binary = "1101";
}
if (hex.equalsIgnoreCase("E")) {
binary = "1110";
}
if (hex.equalsIgnoreCase("F")) {
binary = "1111";
}
return binary;
} /*
* chang the bit(it's length = 64) into the string
*
* return string
*/
public String byteToString(int[] byteData) {
String str = "";
for (int i = 0; i < 4; i++) {
int count = 0;
for (int j = 0; j < 16; j++) {
int pow = 1;
for (int m = 15; m > j; m--) {
pow *= 2;
}
count += byteData[16 * i + j] * pow;
}
if (count != 0) {
str += "" + (char) (count);
}
}
return str;
} public String bt64ToHex(int[] byteData) {
String hex = "";
for (int i = 0; i < 16; i++) {
String bt = "";
for (int j = 0; j < 4; j++) {
bt += byteData[i * 4 + j];
}
hex += bt4ToHex(bt);
}
return hex;
} public String hexToBt64(String hex) {
String binary = "";
for (int i = 0; i < 16; i++) {
binary += hexToBt4(hex.substring(i, i + 1));
}
return binary;
} /*
* the 64 bit des core arithmetic
*/ public int[] enc(int[] dataByte, int[] keyByte) {
int[][] keys = generateKeys(keyByte);
int[] ipByte = initPermute(dataByte);
int[] ipLeft = new int[32];
int[] ipRight = new int[32];
int[] tempLeft = new int[32];
int 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];
}
int[] key = new int[48];
for (m = 0; m < 48; m++) {
key[m] = keys[i][m];
}
int[] tempRight = xor(pPermute(sBoxPermute(xor(
expandPermute(ipRight), key))), tempLeft);
for (n = 0; n < 32; n++) {
ipRight[n] = tempRight[n];
} } int[] finalData = new int[64];
for (i = 0; i < 32; i++) {
finalData[i] = ipRight[i];
finalData[32 + i] = ipLeft[i];
}
return finallyPermute(finalData);
} public int[] dec(int[] dataByte, int[] keyByte) {
int[][] keys = generateKeys(keyByte);
int[] ipByte = initPermute(dataByte);
int[] ipLeft = new int[32];
int[] ipRight = new int[32];
int[] tempLeft = new int[32];
int 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];
}
int[] key = new int[48];
for (m = 0; m < 48; m++) {
key[m] = keys[i][m];
} int[] tempRight = xor(pPermute(sBoxPermute(xor(
expandPermute(ipRight), key))), tempLeft);
for (n = 0; n < 32; n++) {
ipRight[n] = tempRight[n];
}
} int[] finalData = new int[64];
for (i = 0; i < 32; i++) {
finalData[i] = ipRight[i];
finalData[32 + i] = ipLeft[i];
}
return finallyPermute(finalData);
} public int[] initPermute(int[] originalData) {
int[] ipByte = new int[64];
int i = 0, m = 1, n = 0, j, k;
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;
} public int[] expandPermute(int[] rightData) {
int[] epByte = new int[48];
int i, j;
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;
} public int[] xor(int[] byteOne, int[] byteTwo) {
// var xorByte = new Array(byteOne.length);
// for(int i = 0;i < byteOne.length; i ++){
// xorByte[i] = byteOne[i] ^ byteTwo[i];
// }
// return xorByte;
int[] xorByte = new int[byteOne.length];
for (int i = 0; i < byteOne.length; i++) {
xorByte[i] = byteOne[i] ^ byteTwo[i];
}
return xorByte;
} public int[] sBoxPermute(int[] expandByte) { // var sBoxByte = new Array(32);
int[] sBoxByte = new int[32];
String binary = "";
int[][] 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 */
int[][] 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 */
int[][] 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 */
int[][] 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 */
int[][] 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 */
int[][] 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 */
int[][] 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 */
int[][] 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 (int m = 0; m < 8; m++) {
int 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] = Integer.parseInt(binary.substring(0, 1));
sBoxByte[m * 4 + 1] = Integer.parseInt(binary.substring(1, 2));
sBoxByte[m * 4 + 2] = Integer.parseInt(binary.substring(2, 3));
sBoxByte[m * 4 + 3] = Integer.parseInt(binary.substring(3, 4));
}
return sBoxByte;
} public int[] pPermute(int[] sBoxByte) {
int[] pBoxPermute = new int[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;
} public int[] finallyPermute(int[] endByte) {
int[] fpByte = new int[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;
} public String getBoxBinary(int i) {
String 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
*
*/
public int[][] generateKeys(int[] keyByte) {
int[] key = new int[56];
int[][] keys = new int[16][48]; // 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();
int[] loop = new int[] { 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 }; for (int i = 0; i < 7; i++) {
for (int j = 0, k = 7; j < 8; j++, k--) {
key[i * 8 + j] = keyByte[8 * k + i];
}
} int i = 0;
for (i = 0; i < 16; i++) {
int tempLeft = 0;
int tempRight = 0;
for (int j = 0; j < loop[i]; j++) {
tempLeft = key[0];
tempRight = key[28];
for (int 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);
int[] tempKey = new int[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];
int m;
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;
}
}

DES加解密、JavaScript、Java的更多相关文章

  1. Java拓展教程:文件DES加解密

    Java拓展教程:文件加解密 Java中的加密解密技术 加密技术根据一般可以分为对称加密技术和非对称加密技术.对称加密技术属于传统的加密技术,它的加密和解密的密钥是相同的,它的优点是:运算速度快,加密 ...

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

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

  3. 一个java的DES加解密类转换成C#

    原文:一个java的DES加解密类转换成C# 一个java的des加密解密代码如下: //package com.visionsky.util; import java.security.*; //i ...

  4. Java Des加解密方法(c#加密Java解密)

    最近我们用Java把一个用.net编写的老系统重新做了翻版,但是登录还是用.net的登录.这样就会遇到一个比较棘手的问题,我们登录用的cookie信息都是.net用des加密的,但我们不得不用Java ...

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

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

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

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

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

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

  8. DES加解密 cbc模式 的简单讲解 && C++用openssl库来实现的注意事项

    DES cbc是基于数据块加密的.数据块的长度为8字节64bit.以数据块为单位循环加密,再拼接.每个数据块加密的秘钥一样,IV向量不同.第一个数据快所需的IV向量,需要我们提供,从第二个数据块开始, ...

  9. Node.js的DES加解密和MD5加密

    最基本的就是经常用的md5加密算法 代码如下 var  MD5=function (data) {        var _encrymd5 = require('crypto').createHas ...

  10. DES加解密算法Qt实现

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

随机推荐

  1. 显示mysql线程和kill线程的命令

    show processlist;//显示哪些线程正在运行. kill id //kill线程   通常在表被锁的时候用.   show processlist;显示哪些线程正在运行.您也可以使用my ...

  2. FreeRTOS--堆内存管理

    因为项目需要,最近开始学习FreeRTOS,一开始有些紧张,因为两个星期之前对于FreeRTOS的熟悉度几乎为零,经过对FreeRTOS官网的例子程序的摸索,和项目中问题的解决,遇到了很多熟悉的身影, ...

  3. MySql基础入门-mysql体系结构

    mysql体系结构:     由:连接池组件.管理服务和工具组件.sql接口组件.查询分析器组件.优化器组件.                缓冲组件.插件式存储引擎.物理文件组成.     mysq ...

  4. HDU5804--Price List

    Price List Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/131072 K (Java/Others) Tot ...

  5. lua 限流

    前言 每逢大促必压测,每逢大促必限流,这估计是电商人的常态.每次大促期间,业务流量是平时的几倍十几倍,大促期间大部分业务都会集中在购物车结算,必须限流,才能保证系统不宕机. 限流算法 限流算法一般有三 ...

  6. ibv_get_device_guid()函数

    uint64_t ibv_get_device_guid(struct ibv_device *device); 描述 函数返回RDMA 设备的 GUID(The Global Unique IDen ...

  7. 天天乐宝APP开发

    "互联网+"时代是一个"信息过剩"的时代,也是一个"注意力稀缺"的时代,怎样在"无限的信息中"获取"有限的注意 ...

  8. springmvc关于前台日期作为实体类对象参数类型转换错误

    页面报错: 后台错误: Field error in object 'user' on field 'birthday': rejected value [2013-06-24]; codes [ty ...

  9. c++对象在lua层的生命周期与内容扩展

    前言 上一篇博客记录了 tolua++ 将 c++类型,变量,函数,以及对象导出到 lua 的过程,这篇博客就接着记录一下 c++对象的内存回收以及c++对象数据和方法在lua中的扩展. 首先 tol ...

  10. QWT与QT Designer

    QWT是一套非常不错的开发库,它能结合QT开发,做出非常好的曲线,刻度,表盘等效果来.  qwt的下载以及动态链接库的编译等这里就不做介绍了.在源码目录下可以找到designer目录,其中有插件的源码 ...