免费注册 查看新帖 |

Chinaunix

广告
  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 6450 | 回复: 6
打印 上一主题 下一主题

用PHP进行DES加密,用JS进行解密,失败!!! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-04-18 22:33 |只看该作者 |倒序浏览
为了保护文章,小弟想通过在服务器端,对文章内容进行DES加密,这样输出到客户端,然后再用JS解密,可是始终实现不了。

代码是从网上找来的。

PHP的


  1. $key="123";

  2. //des
  3. //this takes the key, the message, and whether to encrypt or decrypt
  4. function des ($key, $message, $encrypt, $mode, $iv) {
  5. //declaring this locally speeds things up a bit
  6. $spfunction1 = array (0x1010400,0,0x10000,0x1010404,0x1010004,0x10404,0x4,0x10000,0x400,0x1010400,0x1010404,0x400,0x1000404,0x1010004,0x1000000,0x4,0x404,0x1000400,0x1000400,0x10400,0x10400,0x1010000,0x1010000,0x1000404,0x10004,0x1000004,0x1000004,0x10004,0,0x404,0x10404,0x1000000,0x10000,0x1010404,0x4,0x1010000,0x1010400,0x1000000,0x1000000,0x400,0x1010004,0x10000,0x10400,0x1000004,0x400,0x4,0x1000404,0x10404,0x1010404,0x10004,0x1010000,0x1000404,0x1000004,0x404,0x10404,0x1010400,0x404,0x1000400,0x1000400,0,0x10004,0x10400,0,0x1010004);
  7. $spfunction2 = array (-0x7fef7fe0,-0x7fff8000,0x8000,0x108020,0x100000,0x20,-0x7fefffe0,-0x7fff7fe0,-0x7fffffe0,-0x7fef7fe0,-0x7fef8000,-0x80000000,-0x7fff8000,0x100000,0x20,-0x7fefffe0,0x108000,0x100020,-0x7fff7fe0,0,-0x80000000,0x8000,0x108020,-0x7ff00000,0x100020,-0x7fffffe0,0,0x108000,0x8020,-0x7fef8000,-0x7ff00000,0x8020,0,0x108020,-0x7fefffe0,0x100000,-0x7fff7fe0,-0x7ff00000,-0x7fef8000,0x8000,-0x7ff00000,-0x7fff8000,0x20,-0x7fef7fe0,0x108020,0x20,0x8000,-0x80000000,0x8020,-0x7fef8000,0x100000,-0x7fffffe0,0x100020,-0x7fff7fe0,-0x7fffffe0,0x100020,0x108000,0,-0x7fff8000,0x8020,-0x80000000,-0x7fefffe0,-0x7fef7fe0,0x108000);
  8. $spfunction3 = array (0x208,0x8020200,0,0x8020008,0x8000200,0,0x20208,0x8000200,0x20008,0x8000008,0x8000008,0x20000,0x8020208,0x20008,0x8020000,0x208,0x8000000,0x8,0x8020200,0x200,0x20200,0x8020000,0x8020008,0x20208,0x8000208,0x20200,0x20000,0x8000208,0x8,0x8020208,0x200,0x8000000,0x8020200,0x8000000,0x20008,0x208,0x20000,0x8020200,0x8000200,0,0x200,0x20008,0x8020208,0x8000200,0x8000008,0x200,0,0x8020008,0x8000208,0x20000,0x8000000,0x8020208,0x8,0x20208,0x20200,0x8000008,0x8020000,0x8000208,0x208,0x8020000,0x20208,0x8,0x8020008,0x20200);
  9. $spfunction4 = array (0x802001,0x2081,0x2081,0x80,0x802080,0x800081,0x800001,0x2001,0,0x802000,0x802000,0x802081,0x81,0,0x800080,0x800001,0x1,0x2000,0x800000,0x802001,0x80,0x800000,0x2001,0x2080,0x800081,0x1,0x2080,0x800080,0x2000,0x802080,0x802081,0x81,0x800080,0x800001,0x802000,0x802081,0x81,0,0,0x802000,0x2080,0x800080,0x800081,0x1,0x802001,0x2081,0x2081,0x80,0x802081,0x81,0x1,0x2000,0x800001,0x2001,0x802080,0x800081,0x2001,0x2080,0x800000,0x802001,0x80,0x800000,0x2000,0x802080);
  10. $spfunction5 = array (0x100,0x2080100,0x2080000,0x42000100,0x80000,0x100,0x40000000,0x2080000,0x40080100,0x80000,0x2000100,0x40080100,0x42000100,0x42080000,0x80100,0x40000000,0x2000000,0x40080000,0x40080000,0,0x40000100,0x42080100,0x42080100,0x2000100,0x42080000,0x40000100,0,0x42000000,0x2080100,0x2000000,0x42000000,0x80100,0x80000,0x42000100,0x100,0x2000000,0x40000000,0x2080000,0x42000100,0x40080100,0x2000100,0x40000000,0x42080000,0x2080100,0x40080100,0x100,0x2000000,0x42080000,0x42080100,0x80100,0x42000000,0x42080100,0x2080000,0,0x40080000,0x42000000,0x80100,0x2000100,0x40000100,0x80000,0,0x40080000,0x2080100,0x40000100);
  11. $spfunction6 = array (0x20000010,0x20400000,0x4000,0x20404010,0x20400000,0x10,0x20404010,0x400000,0x20004000,0x404010,0x400000,0x20000010,0x400010,0x20004000,0x20000000,0x4010,0,0x400010,0x20004010,0x4000,0x404000,0x20004010,0x10,0x20400010,0x20400010,0,0x404010,0x20404000,0x4010,0x404000,0x20404000,0x20000000,0x20004000,0x10,0x20400010,0x404000,0x20404010,0x400000,0x4010,0x20000010,0x400000,0x20004000,0x20000000,0x4010,0x20000010,0x20404010,0x404000,0x20400000,0x404010,0x20404000,0,0x20400010,0x10,0x4000,0x20400000,0x404010,0x4000,0x400010,0x20004010,0,0x20404000,0x20000000,0x400010,0x20004010);
  12. $spfunction7 = array (0x200000,0x4200002,0x4000802,0,0x800,0x4000802,0x200802,0x4200800,0x4200802,0x200000,0,0x4000002,0x2,0x4000000,0x4200002,0x802,0x4000800,0x200802,0x200002,0x4000800,0x4000002,0x4200000,0x4200800,0x200002,0x4200000,0x800,0x802,0x4200802,0x200800,0x2,0x4000000,0x200800,0x4000000,0x200800,0x200000,0x4000802,0x4000802,0x4200002,0x4200002,0x2,0x200002,0x4000000,0x4000800,0x200000,0x4200800,0x802,0x200802,0x4200800,0x802,0x4000002,0x4200802,0x4200000,0x200800,0,0x2,0x4200802,0,0x200802,0x4200000,0x800,0x4000002,0x4000800,0x800,0x200002);
  13. $spfunction8 = array (0x10001040,0x1000,0x40000,0x10041040,0x10000000,0x10001040,0x40,0x10000000,0x40040,0x10040000,0x10041040,0x41000,0x10041000,0x41040,0x1000,0x40,0x10040000,0x10000040,0x10001000,0x1040,0x41000,0x40040,0x10040040,0x10041000,0x1040,0,0,0x10040040,0x10000040,0x10001000,0x41040,0x40000,0x41040,0x40000,0x10041000,0x1000,0x40,0x10040040,0x1000,0x41040,0x10001000,0x40,0x10000040,0x10040000,0x10040040,0x10000000,0x40000,0x10001040,0,0x10041040,0x40040,0x10000040,0x10040000,0x10001000,0x10001040,0,0x10041040,0x41000,0x41000,0x1040,0x1040,0x40040,0x10000000,0x10041000);
  14. $masks = array (4294967295,2147483647,1073741823,536870911,268435455,134217727,67108863,33554431,16777215,8388607,4194303,2097151,1048575,524287,262143,131071,65535,32767,16383,8191,4095,2047,1023,511,255,127,63,31,15,7,3,1,0);

  15. //create the 16 or 48 subkeys we will need
  16. $keys = des_createKeys ($key);
  17. $m=0;
  18. $len = strlen($message);
  19. $chunk = 0;
  20. //set up the loops for single and triple des
  21. $iterations = ((count($keys) == 32) ? 3 : 9); //single or triple des
  22. if ($iterations == 3) {$looping = (($encrypt) ? array (0, 32, 2) : array (30, -2, -2));}
  23. else {$looping = (($encrypt) ? array (0, 32, 2, 62, 30, -2, 64, 96, 2) : array (94, 62, -2, 32, 64, 2, 30, -2, -2));}

  24. $message .= (chr(0) . chr(0) . chr(0) . chr(0) . chr(0) . chr(0) . chr(0) . chr(0)); //pad the message out with null bytes
  25. //store the result here
  26. $result = "";
  27. $tempresult = "";

  28. if ($mode == 1) { //CBC mode
  29.     $cbcleft = (ord($iv{$m++}) << 24) | (ord($iv{$m++}) << 16) | (ord($iv{$m++}) << 8) | ord($iv{$m++});
  30.     $cbcright = (ord($iv{$m++}) << 24) | (ord($iv{$m++}) << 16) | (ord($iv{$m++}) << 8) | ord($iv{$m++});
  31.     $m=0;
  32. }

  33. //loop through each 64 bit chunk of the message
  34. while ($m < $len) {
  35.     $left = (ord($message{$m++}) << 24) | (ord($message{$m++}) << 16) | (ord($message{$m++}) << 8) | ord($message{$m++});
  36.     $right = (ord($message{$m++}) << 24) | (ord($message{$m++}) << 16) | (ord($message{$m++}) << 8) | ord($message{$m++});

  37.     //for Cipher Block Chaining mode, xor the message with the previous result
  38.     if ($mode == 1) {if ($encrypt) {$left ^= $cbcleft; $right ^= $cbcright;} else {$cbcleft2 = $cbcleft; $cbcright2 = $cbcright; $cbcleft = $left; $cbcright = $right;}}

  39.     //first each 64 but chunk of the message must be permuted according to IP
  40.     $temp = (($left >> 4 & $masks[4]) ^ $right) & 0x0f0f0f0f; $right ^= $temp; $left ^= ($temp << 4);
  41.     $temp = (($left >> 16 & $masks[16]) ^ $right) & 0x0000ffff; $right ^= $temp; $left ^= ($temp << 16);
  42.     $temp = (($right >> 2 & $masks[2]) ^ $left) & 0x33333333; $left ^= $temp; $right ^= ($temp << 2);
  43.     $temp = (($right >> 8 & $masks[8]) ^ $left) & 0x00ff00ff; $left ^= $temp; $right ^= ($temp << 8);
  44.     $temp = (($left >> 1 & $masks[1]) ^ $right) & 0x55555555; $right ^= $temp; $left ^= ($temp << 1);

  45.     $left = (($left << 1) | ($left >> 31 & $masks[31]));
  46.     $right = (($right << 1) | ($right >> 31 & $masks[31]));

  47.     //do this either 1 or 3 times for each chunk of the message
  48.     for ($j=0; $j<$iterations; $j+=3) {
  49.       $endloop = $looping[$j+1];
  50.       $loopinc = $looping[$j+2];
  51.       //now go through and perform the encryption or decryption
  52.       for ($i=$looping[$j]; $i!=$endloop; $i+=$loopinc) { //for efficiency
  53.         $right1 = $right ^ $keys[$i];
  54.         $right2 = (($right >> 4 & $masks[4]) | ($right << 28)) ^ $keys[$i+1];
  55.         //the result is attained by passing these bytes through the S selection functions
  56.         $temp = $left;
  57.         $left = $right;
  58.         $right = $temp ^ ($spfunction2[($right1 >> 24 & $masks[24]) & 0x3f] | $spfunction4[($right1 >> 16 & $masks[16]) & 0x3f]
  59.               | $spfunction6[($right1 >> 8 & $masks[8]) & 0x3f] | $spfunction8[$right1 & 0x3f]
  60.               | $spfunction1[($right2 >> 24 & $masks[24]) & 0x3f] | $spfunction3[($right2 >> 16 & $masks[16]) & 0x3f]
  61.               | $spfunction5[($right2 >> 8 & $masks[8]) & 0x3f] | $spfunction7[$right2 & 0x3f]);
  62.       }
  63.       $temp = $left; $left = $right; $right = $temp; //unreverse left and right
  64.     } //for either 1 or 3 iterations

  65.     //move then each one bit to the right
  66.     $left = (($left >> 1 & $masks[1]) | ($left << 31));
  67.     $right = (($right >> 1 & $masks[1]) | ($right << 31));

  68.     //now perform IP-1, which is IP in the opposite direction
  69.     $temp = (($left >> 1 & $masks[1]) ^ $right) & 0x55555555; $right ^= $temp; $left ^= ($temp << 1);
  70.     $temp = (($right >> 8 & $masks[8]) ^ $left) & 0x00ff00ff; $left ^= $temp; $right ^= ($temp << 8);
  71.     $temp = (($right >> 2 & $masks[2]) ^ $left) & 0x33333333; $left ^= $temp; $right ^= ($temp << 2);
  72.     $temp = (($left >> 16 & $masks[16]) ^ $right) & 0x0000ffff; $right ^= $temp; $left ^= ($temp << 16);
  73.     $temp = (($left >> 4 & $masks[4]) ^ $right) & 0x0f0f0f0f; $right ^= $temp; $left ^= ($temp << 4);

  74.     //for Cipher Block Chaining mode, xor the message with the previous result
  75.     if ($mode == 1) {if ($encrypt) {$cbcleft = $left; $cbcright = $right;} else {$left ^= $cbcleft2; $right ^= $cbcright2;}}
  76.     $tempresult .= (chr($left>>24 & $masks[24]) . chr(($left>>16 & $masks[16]) & 0xff) . chr(($left>>8 & $masks[8]) & 0xff) . chr($left & 0xff) . chr($right>>24 & $masks[24]) . chr(($right>>16 & $masks[16]) & 0xff) . chr(($right>>8 & $masks[8]) & 0xff) . chr($right & 0xff));

  77.     $chunk += 8;
  78.     if ($chunk == 512) {$result .= $tempresult; $tempresult = ""; $chunk = 0;}
  79. } //for every 8 characters, or 64 bits in the message

  80. //return the result as an array
  81. return ($result . $tempresult);
  82. } //end of des

  83. //des_createKeys
  84. //this takes as input a 64 bit key (even though only 56 bits are used)
  85. //as an array of 2 integers, and returns 16 48 bit keys
  86. function des_createKeys ($key) {
  87. //declaring this locally speeds things up a bit
  88. $pc2bytes0 = array (0,0x4,0x20000000,0x20000004,0x10000,0x10004,0x20010000,0x20010004,0x200,0x204,0x20000200,0x20000204,0x10200,0x10204,0x20010200,0x20010204);
  89. $pc2bytes1 = array (0,0x1,0x100000,0x100001,0x4000000,0x4000001,0x4100000,0x4100001,0x100,0x101,0x100100,0x100101,0x4000100,0x4000101,0x4100100,0x4100101);
  90. $pc2bytes2 = array (0,0x8,0x800,0x808,0x1000000,0x1000008,0x1000800,0x1000808,0,0x8,0x800,0x808,0x1000000,0x1000008,0x1000800,0x1000808);
  91. $pc2bytes3 = array (0,0x200000,0x8000000,0x8200000,0x2000,0x202000,0x8002000,0x8202000,0x20000,0x220000,0x8020000,0x8220000,0x22000,0x222000,0x8022000,0x8222000);
  92. $pc2bytes4 = array (0,0x40000,0x10,0x40010,0,0x40000,0x10,0x40010,0x1000,0x41000,0x1010,0x41010,0x1000,0x41000,0x1010,0x41010);
  93. $pc2bytes5 = array (0,0x400,0x20,0x420,0,0x400,0x20,0x420,0x2000000,0x2000400,0x2000020,0x2000420,0x2000000,0x2000400,0x2000020,0x2000420);
  94. $pc2bytes6 = array (0,0x10000000,0x80000,0x10080000,0x2,0x10000002,0x80002,0x10080002,0,0x10000000,0x80000,0x10080000,0x2,0x10000002,0x80002,0x10080002);
  95. $pc2bytes7 = array (0,0x10000,0x800,0x10800,0x20000000,0x20010000,0x20000800,0x20010800,0x20000,0x30000,0x20800,0x30800,0x20020000,0x20030000,0x20020800,0x20030800);
  96. $pc2bytes8 = array (0,0x40000,0,0x40000,0x2,0x40002,0x2,0x40002,0x2000000,0x2040000,0x2000000,0x2040000,0x2000002,0x2040002,0x2000002,0x2040002);
  97. $pc2bytes9 = array (0,0x10000000,0x8,0x10000008,0,0x10000000,0x8,0x10000008,0x400,0x10000400,0x408,0x10000408,0x400,0x10000400,0x408,0x10000408);
  98. $pc2bytes10 = array (0,0x20,0,0x20,0x100000,0x100020,0x100000,0x100020,0x2000,0x2020,0x2000,0x2020,0x102000,0x102020,0x102000,0x102020);
  99. $pc2bytes11 = array (0,0x1000000,0x200,0x1000200,0x200000,0x1200000,0x200200,0x1200200,0x4000000,0x5000000,0x4000200,0x5000200,0x4200000,0x5200000,0x4200200,0x5200200);
  100. $pc2bytes12 = array (0,0x1000,0x8000000,0x8001000,0x80000,0x81000,0x8080000,0x8081000,0x10,0x1010,0x8000010,0x8001010,0x80010,0x81010,0x8080010,0x8081010);
  101. $pc2bytes13 = array (0,0x4,0x100,0x104,0,0x4,0x100,0x104,0x1,0x5,0x101,0x105,0x1,0x5,0x101,0x105);
  102. $masks = array (4294967295,2147483647,1073741823,536870911,268435455,134217727,67108863,33554431,16777215,8388607,4194303,2097151,1048575,524287,262143,131071,65535,32767,16383,8191,4095,2047,1023,511,255,127,63,31,15,7,3,1,0);

  103. //how many iterations (1 for des, 3 for triple des)
  104. $iterations = ((strlen($key) >= 24) ? 3 : 1);
  105. //stores the return keys
  106. $keys = array (); // size = 32 * iterations but you don't specify this in php
  107. //now define the left shifts which need to be done
  108. $shifts = array (0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0);
  109. //other variables
  110. $m=0;
  111. $n=0;

  112. for ($j=0; $j<$iterations; $j++) { //either 1 or 3 iterations
  113.     $left = (ord($key{$m++}) << 24) | (ord($key{$m++}) << 16) | (ord($key{$m++}) << 8) | ord($key{$m++});
  114.     $right = (ord($key{$m++}) << 24) | (ord($key{$m++}) << 16) | (ord($key{$m++}) << 8) | ord($key{$m++});

  115.     $temp = (($left >> 4 & $masks[4]) ^ $right) & 0x0f0f0f0f; $right ^= $temp; $left ^= ($temp << 4);
  116.     $temp = (($right >> 16 & $masks[16]) ^ $left) & 0x0000ffff; $left ^= $temp; $right ^= ($temp << -16);
  117.     $temp = (($left >> 2 & $masks[2]) ^ $right) & 0x33333333; $right ^= $temp; $left ^= ($temp << 2);
  118.     $temp = (($right >> 16 & $masks[16]) ^ $left) & 0x0000ffff; $left ^= $temp; $right ^= ($temp << -16);
  119.     $temp = (($left >> 1 & $masks[1]) ^ $right) & 0x55555555; $right ^= $temp; $left ^= ($temp << 1);
  120.     $temp = (($right >> 8 & $masks[8]) ^ $left) & 0x00ff00ff; $left ^= $temp; $right ^= ($temp << 8);
  121.     $temp = (($left >> 1 & $masks[1]) ^ $right) & 0x55555555; $right ^= $temp; $left ^= ($temp << 1);

  122.     //the right side needs to be shifted and to get the last four bits of the left side
  123.     $temp = ($left << 8) | (($right >> 20 & $masks[20]) & 0x000000f0);
  124.     //left needs to be put upside down
  125.     $left = ($right << 24) | (($right << 8) & 0xff0000) | (($right >> 8 & $masks[8]) & 0xff00) | (($right >> 24 & $masks[24]) & 0xf0);
  126.     $right = $temp;

  127.     //now go through and perform these shifts on the left and right keys
  128.     for ($i=0; $i < count($shifts); $i++) {
  129.       //shift the keys either one or two bits to the left
  130.       if ($shifts[$i] > 0) {
  131.          $left = (($left << 2) | ($left >> 26 & $masks[26]));
  132.          $right = (($right << 2) | ($right >> 26 & $masks[26]));
  133.       } else {
  134.          $left = (($left << 1) | ($left >> 27 & $masks[27]));
  135.          $right = (($right << 1) | ($right >> 27 & $masks[27]));
  136.       }
  137.       $left = $left & -0xf;
  138.       $right = $right & -0xf;

  139.       //now apply PC-2, in such a way that E is easier when encrypting or decrypting
  140.       //this conversion will look like PC-2 except only the last 6 bits of each byte are used
  141.       //rather than 48 consecutive bits and the order of lines will be according to
  142.       //how the S selection functions will be applied: S2, S4, S6, S8, S1, S3, S5, S7
  143.       $lefttemp = $pc2bytes0[$left >> 28 & $masks[28]] | $pc2bytes1[($left >> 24 & $masks[24]) & 0xf]
  144.               | $pc2bytes2[($left >> 20 & $masks[20]) & 0xf] | $pc2bytes3[($left >> 16 & $masks[16]) & 0xf]
  145.               | $pc2bytes4[($left >> 12 & $masks[12]) & 0xf] | $pc2bytes5[($left >> 8 & $masks[8]) & 0xf]
  146.               | $pc2bytes6[($left >> 4 & $masks[4]) & 0xf];
  147.       $righttemp = $pc2bytes7[$right >> 28 & $masks[28]] | $pc2bytes8[($right >> 24 & $masks[24]) & 0xf]
  148.                 | $pc2bytes9[($right >> 20 & $masks[20]) & 0xf] | $pc2bytes10[($right >> 16 & $masks[16]) & 0xf]
  149.                 | $pc2bytes11[($right >> 12 & $masks[12]) & 0xf] | $pc2bytes12[($right >> 8 & $masks[8]) & 0xf]
  150.                 | $pc2bytes13[($right >> 4 & $masks[4]) & 0xf];
  151.       $temp = (($righttemp >> 16 & $masks[16]) ^ $lefttemp) & 0x0000ffff;
  152.       $keys[$n++] = $lefttemp ^ $temp; $keys[$n++] = $righttemp ^ ($temp << 16);
  153.     }
  154. } //for each iterations
  155. //return the keys we've created
  156. return $keys;
  157. } //end of des_createKeys

  158. ////////////////////////////// TEST //////////////////////////////
  159. function stringToHex ($s) {
  160. $r = "0x";
  161. $hexes = array ("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f");
  162. for ($i=0; $i<strlen($s); $i++) {$r .= ($hexes [(ord($s{$i}) >> 4)] . $hexes [(ord($s{$i}) & 0xf)]);}
  163. return $r;
  164. }

  165. function hexToString ($h) {
  166. $r = "";
  167. for ($i= (substr($h, 0, 2)=="0x")?2:0; $i<strlen($h); $i+=2) {$r .= chr (base_convert (substr ($h, $i, 2), 16, 10));}
  168. return $r;
  169. }
复制代码

论坛徽章:
0
2 [报告]
发表于 2008-04-18 22:35 |只看该作者
JS的

  1. <html>
  2. <head>
  3. <title>DES加密</title>
  4. <meta http-equiv="Content-Type" content="text/html;charset=gb3212">
  5. <script type="text/javascript">
  6. <!--//des加解密函数
  7. //key:加密用的密钥
  8. //message:需要加密的字符串
  9. //encrypt:加密还是解密,1为加密,0,解密
  10. function des(key,message,encrypt,mode,iv){
  11. //declaring this locally speeds things up a bit
  12. var spfunction1=new Array(0x1010400,0,0x10000,0x1010404,0x1010004,0x10404,0x4,0x10000,0x400,0x1010400,0x1010404,0x400,0x1000404,0x1010004,0x1000000,0x4,0x404,0x1000400,0x1000400,0x10400,0x10400,0x1010000,0x1010000,0x1000404,0x10004,0x1000004,0x1000004,0x10004,0,0x404,0x10404,0x1000000,0x10000,0x1010404,0x4,0x1010000,0x1010400,0x1000000,0x1000000,0x400,0x1010004,0x10000,0x10400,0x1000004,0x400,0x4,0x1000404,0x10404,0x1010404,0x10004,0x1010000,0x1000404,0x1000004,0x404,0x10404,0x1010400,0x404,0x1000400,0x1000400,0,0x10004,0x10400,0,0x1010004);
  13. var spfunction2=new Array(-0x7fef7fe0,-0x7fff8000,0x8000,0x108020,0x100000,0x20,-0x7fefffe0,-0x7fff7fe0,-0x7fffffe0,-0x7fef7fe0,-0x7fef8000,-0x80000000,-0x7fff8000,0x100000,0x20,-0x7fefffe0,0x108000,0x100020,-0x7fff7fe0,0,-0x80000000,0x8000,0x108020,-0x7ff00000,0x100020,-0x7fffffe0,0,0x108000,0x8020,-0x7fef8000,-0x7ff00000,0x8020,0,0x108020,-0x7fefffe0,0x100000,-0x7fff7fe0,-0x7ff00000,-0x7fef8000,0x8000,-0x7ff00000,-0x7fff8000,0x20,-0x7fef7fe0,0x108020,0x20,0x8000,-0x80000000,0x8020,-0x7fef8000,0x100000,-0x7fffffe0,0x100020,-0x7fff7fe0,-0x7fffffe0,0x100020,0x108000,0,-0x7fff8000,0x8020,-0x80000000,-0x7fefffe0,-0x7fef7fe0,0x108000);
  14. var spfunction3=new Array(0x208,0x8020200,0,0x8020008,0x8000200,0,0x20208,0x8000200,0x20008,0x8000008,0x8000008,0x20000,0x8020208,0x20008,0x8020000,0x208,0x8000000,0x8,0x8020200,0x200,0x20200,0x8020000,0x8020008,0x20208,0x8000208,0x20200,0x20000,0x8000208,0x8,0x8020208,0x200,0x8000000,0x8020200,0x8000000,0x20008,0x208,0x20000,0x8020200,0x8000200,0,0x200,0x20008,0x8020208,0x8000200,0x8000008,0x200,0,0x8020008,0x8000208,0x20000,0x8000000,0x8020208,0x8,0x20208,0x20200,0x8000008,0x8020000,0x8000208,0x208,0x8020000,0x20208,0x8,0x8020008,0x20200);
  15. var spfunction4=new Array(0x802001,0x2081,0x2081,0x80,0x802080,0x800081,0x800001,0x2001,0,0x802000,0x802000,0x802081,0x81,0,0x800080,0x800001,0x1,0x2000,0x800000,0x802001,0x80,0x800000,0x2001,0x2080,0x800081,0x1,0x2080,0x800080,0x2000,0x802080,0x802081,0x81,0x800080,0x800001,0x802000,0x802081,0x81,0,0,0x802000,0x2080,0x800080,0x800081,0x1,0x802001,0x2081,0x2081,0x80,0x802081,0x81,0x1,0x2000,0x800001,0x2001,0x802080,0x800081,0x2001,0x2080,0x800000,0x802001,0x80,0x800000,0x2000,0x802080);
  16. var spfunction5=new Array(0x100,0x2080100,0x2080000,0x42000100,0x80000,0x100,0x40000000,0x2080000,0x40080100,0x80000,0x2000100,0x40080100,0x42000100,0x42080000,0x80100,0x40000000,0x2000000,0x40080000,0x40080000,0,0x40000100,0x42080100,0x42080100,0x2000100,0x42080000,0x40000100,0,0x42000000,0x2080100,0x2000000,0x42000000,0x80100,0x80000,0x42000100,0x100,0x2000000,0x40000000,0x2080000,0x42000100,0x40080100,0x2000100,0x40000000,0x42080000,0x2080100,0x40080100,0x100,0x2000000,0x42080000,0x42080100,0x80100,0x42000000,0x42080100,0x2080000,0,0x40080000,0x42000000,0x80100,0x2000100,0x40000100,0x80000,0,0x40080000,0x2080100,0x40000100);
  17. var spfunction6=new Array(0x20000010,0x20400000,0x4000,0x20404010,0x20400000,0x10,0x20404010,0x400000,0x20004000,0x404010,0x400000,0x20000010,0x400010,0x20004000,0x20000000,0x4010,0,0x400010,0x20004010,0x4000,0x404000,0x20004010,0x10,0x20400010,0x20400010,0,0x404010,0x20404000,0x4010,0x404000,0x20404000,0x20000000,0x20004000,0x10,0x20400010,0x404000,0x20404010,0x400000,0x4010,0x20000010,0x400000,0x20004000,0x20000000,0x4010,0x20000010,0x20404010,0x404000,0x20400000,0x404010,0x20404000,0,0x20400010,0x10,0x4000,0x20400000,0x404010,0x4000,0x400010,0x20004010,0,0x20404000,0x20000000,0x400010,0x20004010);
  18. var spfunction7=new Array(0x200000,0x4200002,0x4000802,0,0x800,0x4000802,0x200802,0x4200800,0x4200802,0x200000,0,0x4000002,0x2,0x4000000,0x4200002,0x802,0x4000800,0x200802,0x200002,0x4000800,0x4000002,0x4200000,0x4200800,0x200002,0x4200000,0x800,0x802,0x4200802,0x200800,0x2,0x4000000,0x200800,0x4000000,0x200800,0x200000,0x4000802,0x4000802,0x4200002,0x4200002,0x2,0x200002,0x4000000,0x4000800,0x200000,0x4200800,0x802,0x200802,0x4200800,0x802,0x4000002,0x4200802,0x4200000,0x200800,0,0x2,0x4200802,0,0x200802,0x4200000,0x800,0x4000002,0x4000800,0x800,0x200002);
  19. var spfunction8=new Array(0x10001040,0x1000,0x40000,0x10041040,0x10000000,0x10001040,0x40,0x10000000,0x40040,0x10040000,0x10041040,0x41000,0x10041000,0x41040,0x1000,0x40,0x10040000,0x10000040,0x10001000,0x1040,0x41000,0x40040,0x10040040,0x10041000,0x1040,0,0,0x10040040,0x10000040,0x10001000,0x41040,0x40000,0x41040,0x40000,0x10041000,0x1000,0x40,0x10040040,0x1000,0x41040,0x10001000,0x40,0x10000040,0x10040000,0x10040040,0x10000000,0x40000,0x10001040,0,0x10041040,0x40040,0x10000040,0x10040000,0x10001000,0x10001040,0,0x10041040,0x41000,0x41000,0x1040,0x1040,0x40040,0x10000000,0x10041000);

  20. //create the 16 or 48 subkeys we will need
  21. var keys=des_createKeys(key);
  22. var m=0,i,j,temp,temp2,right1,right2,left,right,looping;
  23. var cbcleft,cbcleft2,cbcright,cbcright2
  24. var endloop,loopinc;
  25. var len=message.length;
  26. var chunk=0;
  27. //set up the loops for single and triple des
  28. var iterations=keys.length==32?3 :9;//single or triple des
  29. if(iterations==3){looping=encrypt?new Array(0,32,2):new Array(30,-2,-2);}
  30. else{looping=encrypt?new Array(0,32,2,62,30,-2,64,96,2):new Array(94,62,-2,32,64,2,30,-2,-2);}

  31. message+="\0\0\0\0\0\0\0\0";//pad the message out with null bytes
  32. //store the result here
  33. result="";
  34. tempresult="";

  35. if(mode==1){//CBC mode
  36. cbcleft=(iv.charCodeAt(m++)<<24)|(iv.charCodeAt(m++)<<16)|(iv.charCodeAt(m++)<<8)|iv.charCodeAt(m++);
  37. cbcright=(iv.charCodeAt(m++)<<24)|(iv.charCodeAt(m++)<<16)|(iv.charCodeAt(m++)<<8)|iv.charCodeAt(m++);
  38. m=0;
  39. }

  40. //loop through each 64 bit chunk of the message
  41. while(m<len){
  42. if(encrypt){/*加密时按双字节操作*/
  43. left=(message.charCodeAt(m++)<<16)|message.charCodeAt(m++);
  44. right=(message.charCodeAt(m++)<<16)|message.charCodeAt(m++);
  45. }else{
  46. left=(message.charCodeAt(m++)<<24)|(message.charCodeAt(m++)<<16)|(message.charCodeAt(m++)<<8)|message.charCodeAt(m++);
  47. right=(message.charCodeAt(m++)<<24)|(message.charCodeAt(m++)<<16)|(message.charCodeAt(m++)<<8)|message.charCodeAt(m++);
  48. }
  49. //for Cipher Block Chaining mode,xor the message with the previous result
  50. if(mode==1){if(encrypt){left^=cbcleft;right^=cbcright;}else{cbcleft2=cbcleft;cbcright2=cbcright;cbcleft=left;cbcright=right;}}

  51. //first each 64 but chunk of the message must be permuted according to IP
  52. temp=((left>>>4)^right)&0x0f0f0f0f;right^=temp;left^=(temp<<4);
  53. temp=((left>>>16)^right)&0x0000ffff;right^=temp;left^=(temp<<16);
  54. temp=((right>>>2)^left)&0x33333333;left^=temp;right^=(temp<<2);
  55. temp=((right>>>8)^left)&0x00ff00ff;left^=temp;right^=(temp<<8);
  56. temp=((left>>>1)^right)&0x55555555;right^=temp;left^=(temp<<1);

  57. left=((left<<1)|(left>>>31));
  58. right=((right<<1)|(right>>>31));

  59. //do this either 1 or 3 times for each chunk of the message
  60. for(j=0;j<iterations;j+=3){
  61. endloop=looping[j+1];
  62. loopinc=looping[j+2];
  63. //now go through and perform the encryption or decryption
  64. for(i=looping[j];i!=endloop;i+=loopinc){//for efficiency
  65. right1=right^keys[i];
  66. right2=((right>>>4)|(right<<28))^keys[i+1];
  67. //the result is attained by passing these bytes through the S selection functions
  68. temp=left;
  69. left=right;
  70. right=temp^(spfunction2[(right1>>>24)&0x3f]|spfunction4[(right1>>>16)&0x3f]|spfunction6[(right1>>>8)&0x3f]|spfunction8[right1&0x3f]|spfunction1[(right2>>>24)&0x3f]|spfunction3[(right2>>>16)&0x3f]|spfunction5[(right2>>>8)&0x3f]|spfunction7[right2&0x3f]);
  71. }
  72. temp=left;left=right;right=temp;//unreverse left and right
  73. }//for either 1 or 3 iterations

  74. //move then each one bit to the right
  75. left=((left>>>1)|(left<<31));
  76. right=((right>>>1)|(right<<31));

  77. //now perform IP-1,which is IP in the opposite direction
  78. temp=((left>>>1)^right)&0x55555555;right^=temp;left^=(temp<<1);
  79. temp=((right>>>8)^left)&0x00ff00ff;left^=temp;right^=(temp<<8);
  80. temp=((right>>>2)^left)&0x33333333;left^=temp;right^=(temp<<2);
  81. temp=((left>>>16)^right)&0x0000ffff;right^=temp;left^=(temp<<16);
  82. temp=((left>>>4)^right)&0x0f0f0f0f;right^=temp;left^=(temp<<4);

  83. //for Cipher Block Chaining mode,xor the message with the previous result
  84. if(mode==1){if(encrypt){cbcleft=left;cbcright=right;}else{left^=cbcleft2;right^=cbcright2;}}
  85. if(encrypt){tempresult+=String.fromCharCode((left>>>24),((left>>>16)&0xff),((left>>>8)&0xff),(left&0xff),(right>>>24),((right>>>16)&0xff),((right>>>8)&0xff),(right&0xff));}
  86. else{tempresult+=String.fromCharCode(((left>>>16)&0xffff),(left&0xffff),((right>>>16)&0xffff),(right&0xffff));}/*解密时输出双字节*/
  87. encrypt?chunk+=16:chunk+=8;
  88. if(chunk==512){result+=tempresult;tempresult="";chunk=0;}
  89. }//for every 8 characters,or 64 bits in the message

  90. //return the result as an array
  91. return result+tempresult;
  92. }//end of des

  93. //des_createKeys
  94. //this takes as input a 64 bit key(even though only 56 bits are used)
  95. //as an array of 2 integers,and returns 16 48 bit keys
  96. function des_createKeys(key){
  97. //declaring this locally speeds things up a bit
  98. pc2bytes0=new Array(0,0x4,0x20000000,0x20000004,0x10000,0x10004,0x20010000,0x20010004,0x200,0x204,0x20000200,0x20000204,0x10200,0x10204,0x20010200,0x20010204);
  99. pc2bytes1=new Array(0,0x1,0x100000,0x100001,0x4000000,0x4000001,0x4100000,0x4100001,0x100,0x101,0x100100,0x100101,0x4000100,0x4000101,0x4100100,0x4100101);
  100. pc2bytes2=new Array(0,0x8,0x800,0x808,0x1000000,0x1000008,0x1000800,0x1000808,0,0x8,0x800,0x808,0x1000000,0x1000008,0x1000800,0x1000808);
  101. pc2bytes3=new Array(0,0x200000,0x8000000,0x8200000,0x2000,0x202000,0x8002000,0x8202000,0x20000,0x220000,0x8020000,0x8220000,0x22000,0x222000,0x8022000,0x8222000);
  102. pc2bytes4=new Array(0,0x40000,0x10,0x40010,0,0x40000,0x10,0x40010,0x1000,0x41000,0x1010,0x41010,0x1000,0x41000,0x1010,0x41010);
  103. pc2bytes5=new Array(0,0x400,0x20,0x420,0,0x400,0x20,0x420,0x2000000,0x2000400,0x2000020,0x2000420,0x2000000,0x2000400,0x2000020,0x2000420);
  104. pc2bytes6=new Array(0,0x10000000,0x80000,0x10080000,0x2,0x10000002,0x80002,0x10080002,0,0x10000000,0x80000,0x10080000,0x2,0x10000002,0x80002,0x10080002);
  105. pc2bytes7=new Array(0,0x10000,0x800,0x10800,0x20000000,0x20010000,0x20000800,0x20010800,0x20000,0x30000,0x20800,0x30800,0x20020000,0x20030000,0x20020800,0x20030800);
  106. pc2bytes8=new Array(0,0x40000,0,0x40000,0x2,0x40002,0x2,0x40002,0x2000000,0x2040000,0x2000000,0x2040000,0x2000002,0x2040002,0x2000002,0x2040002);
  107. pc2bytes9=new Array(0,0x10000000,0x8,0x10000008,0,0x10000000,0x8,0x10000008,0x400,0x10000400,0x408,0x10000408,0x400,0x10000400,0x408,0x10000408);
  108. pc2bytes10=new Array(0,0x20,0,0x20,0x100000,0x100020,0x100000,0x100020,0x2000,0x2020,0x2000,0x2020,0x102000,0x102020,0x102000,0x102020);
  109. pc2bytes11=new Array(0,0x1000000,0x200,0x1000200,0x200000,0x1200000,0x200200,0x1200200,0x4000000,0x5000000,0x4000200,0x5000200,0x4200000,0x5200000,0x4200200,0x5200200);
  110. pc2bytes12=new Array(0,0x1000,0x8000000,0x8001000,0x80000,0x81000,0x8080000,0x8081000,0x10,0x1010,0x8000010,0x8001010,0x80010,0x81010,0x8080010,0x8081010);
  111. pc2bytes13=new Array(0,0x4,0x100,0x104,0,0x4,0x100,0x104,0x1,0x5,0x101,0x105,0x1,0x5,0x101,0x105);

  112. //how many iterations(1 for des,3 for triple des)
  113. var iterations=key.length>=24?3 :1;
  114. //stores the return keys
  115. var keys=new Array(32 * iterations);
  116. //now define the left shifts which need to be done
  117. var shifts=new Array(0,0,1,1,1,1,1,1,0,1,1,1,1,1,1,0);
  118. //other variables
  119. var lefttemp,righttemp,m=0,n=0,temp;

  120. for(var j=0;j<iterations;j++){//either 1 or 3 iterations
  121. left=(key.charCodeAt(m++)<<24)|(key.charCodeAt(m++)<<16)|(key.charCodeAt(m++)<<8)|key.charCodeAt(m++);
  122. right=(key.charCodeAt(m++)<<24)|(key.charCodeAt(m++)<<16)|(key.charCodeAt(m++)<<8)|key.charCodeAt(m++);

  123. temp=((left>>>4)^right)&0x0f0f0f0f;right^=temp;left^=(temp<<4);
  124. temp=((right>>>-16)^left)&0x0000ffff;left^=temp;right^=(temp<<-16);
  125. temp=((left>>>2)^right)&0x33333333;right^=temp;left^=(temp<<2);
  126. temp=((right>>>-16)^left)&0x0000ffff;left^=temp;right^=(temp<<-16);
  127. temp=((left>>>1)^right)&0x55555555;right^=temp;left^=(temp<<1);
  128. temp=((right>>>8)^left)&0x00ff00ff;left^=temp;right^=(temp<<8);
  129. temp=((left>>>1)^right)&0x55555555;right^=temp;left^=(temp<<1);

  130. //the right side needs to be shifted and to get the last four bits of the left side
  131. temp=(left<<8)|((right>>>20)&0x000000f0);
  132. //left needs to be put upside down
  133. left=(right<<24)|((right<<8)&0xff0000)|((right>>>8)&0xff00)|((right>>>24)&0xf0);
  134. right=temp;

  135. //now go through and perform these shifts on the left and right keys
  136. for(i=0;i<shifts.length;i++){
  137. //shift the keys either one or two bits to the left
  138. if(shifts[i]){left=(left<<2)|(left>>>26);right=(right<<2)|(right>>>26);}
  139. else{left=(left<<1)|(left>>>27);right=(right<<1)|(right>>>27);}
  140. left&=-0xf;right&=-0xf;

  141. //now apply PC-2,in such a way that E is easier when encrypting or decrypting
  142. //this conversion will look like PC-2 except only the last 6 bits of each byte are used
  143. //rather than 48 consecutive bits and the order of lines will be according to
  144. //how the S selection functions will be applied:S2,S4,S6,S8,S1,S3,S5,S7
  145. lefttemp=pc2bytes0[left>>>28]|pc2bytes1[(left>>>24)&0xf]
  146. |pc2bytes2[(left>>>20)&0xf]|pc2bytes3[(left>>>16)&0xf]
  147. |pc2bytes4[(left>>>12)&0xf]|pc2bytes5[(left>>>8)&0xf]
  148. |pc2bytes6[(left>>>4)&0xf];
  149. righttemp=pc2bytes7[right>>>28]|pc2bytes8[(right>>>24)&0xf]
  150. |pc2bytes9[(right>>>20)&0xf]|pc2bytes10[(right>>>16)&0xf]
  151. |pc2bytes11[(right>>>12)&0xf]|pc2bytes12[(right>>>8)&0xf]
  152. |pc2bytes13[(right>>>4)&0xf];
  153. temp=((righttemp>>>16)^lefttemp)&0x0000ffff;
  154. keys[n++]=lefttemp^temp;keys[n++]=righttemp^(temp<<16);
  155. }
  156. }//for each iterations
  157. //return the keys we"ve created
  158. return keys;
  159. }//end of des_createKeys

  160. ////////////////////////////// 测试 //////////////////////////////

  161. function stringToHex(s){
  162. var r="";
  163. var hexes=new Array("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f");
  164. for(var i=0;i<(s.length);i++){r+=hexes[s.charCodeAt(i)>>4]+hexes[s.charCodeAt(i)&0xf];}
  165. return r;
  166. }
  167. function HexTostring(s){
  168. var r="";
  169. for(var i=0;i<s.length;i+=2){
  170. var sxx=parseInt(s.substring(i,i+2),16);
  171. r+=String.fromCharCode(sxx);}
  172. return r;
  173. }

  174. function needkey(){
  175. if(document.encme.deskey.value==""){var tk=window.prompt("请设定密码!","");if(tk){document.encme.deskey.value=tk;return true;}else return false;}
  176. else return true;
  177. }

  178. function encMe(){
  179. if(!needkey())return;
  180. var k=document.encme.deskey.value;
  181. var s=document.encme.inps.value;
  182. document.encme.outs.value=stringToHex(des(k,s,1,0));
  183. }

  184. function uncMe(){
  185. if(!needkey())return;
  186. var s=document.encme.outs.value;
  187. document.encme.inps.value=des(document.encme.deskey.value,HexTostring(s),0,0);
  188. }

  189. //-->
  190. </script>
  191. <style type="text/css">
  192. </style>
  193. </head>
  194. <body>
  195. <table border=0>
  196. <form name="encme">
  197. <tr><td>原
  198. <br />
  199. 文</td><td>
  200. <textarea name="inps" cols="50" rows="10"></textarea>
  201. </td></tr>
  202. <tr><td>

  203. <br />
  204. 码</td><td>
  205. <input type="password" name="deskey" maxlength="100" size="16" value="">
  206. <input type="button" onclick="javascript:encMe()" value="↓加密↓">
  207. <input type="button" onclick="javascript:uncMe()" value="↑解密↑">
  208. </td></tr>
  209. <tr><td>

  210. <br />
  211. 文</td><td>
  212. <textarea name="outs" cols="50" rows="10"></textarea>
  213. </td></tr>
  214. </form>
  215. </table>
  216. </body>
  217. </html>
复制代码


我把PHP加密后得出的字符串放到JS的里面却解不出来,而PHP加密后再解密却是正常的,JS自己加密解密也是正常的,郁闷啊~

有高手能告诉小弟该怎么办吗?当然,最好能提供可以PHP加密,JS解密的源码给小弟,小弟再次万分感谢!

论坛徽章:
1
技术图书徽章
日期:2013-12-05 23:25:45
3 [报告]
发表于 2008-04-18 23:51 |只看该作者
php加密和js加密后,是同样的结果字符串么?
不是的话,那就是加密算法不一致了。

另外,你这样子,等于没有加密。

论坛徽章:
0
4 [报告]
发表于 2008-04-19 00:10 |只看该作者
结果就是不一致,我修改比较过PHP跟JS的,基本改成相同了,但加密出来的还是不一样,当然,我很菜的。

你的意思是说JS里会泄露加密用的KEY吧?听说有种办法可以防止的,不过现在连解密都不行,之后的就更别说了。

还有你可能说的是可以直接选择然后复制吧?我是这样想的,屏蔽右键跟复制。不过好像很多浏览器都有直接解除这种限制的。

唉~烦死了,怎么文章防盗这么难啊~网上一直没有一个有效的办法,做成图片的又不支持HTML的而且也慢,唉~~

大侠~请问有没有一个真正能防止盗文的办法啊~~(别跟我说不要把文字放网络上去。。。)

论坛徽章:
0
5 [报告]
发表于 2008-04-19 00:30 |只看该作者
我做过

不过是.net des 加密  js 解密
全都可以
但是js解密 会有垃圾数据在后面 要自己去掉。
你的js 应该是和我在同一个网上找到的。
你将
des(document.encme.deskey.value,HexTostring(s),0,0);
的0,0 改成 1,0 就应该可以了。

论坛徽章:
0
6 [报告]
发表于 2008-04-19 02:58 |只看该作者
原来是想 防止盗文.

这种方式根本就不行, 解密算法公开在外面,有什么意义?

且不谈这个, DES加密,肯定消耗服务器CPU,客户端解密也会占用用户的CPU. 费力不讨好,别人还是可以轻松解密.

要完全防止恐怕是很难的.

可以考虑 将文字 生成图片 或者 生成FLASH 输出,再加点点干扰. 这样盗文的难度就提高很多了.

一般人盗不了,有技术的人也不屑于盗文.

如果说图片和FLASH慢,可以做做优化.

估计JS进行DES解密也挺慢的.

论坛徽章:
0
7 [报告]
发表于 2008-05-14 00:30 |只看该作者
这样做 没有意义 加密应该写为扩展 或者有一个server专门处理
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP