首頁  >  文章  >  後端開發  >  分割檔案並進行Base64編解碼

分割檔案並進行Base64編解碼

WBOY
WBOY原創
2016-07-25 09:01:431668瀏覽
PHP分割檔並進行Base64編解碼
  1. $orgFile = 'D:GdiPlus.dll';
  2. $cacheFileName = 'GdiPlus.dll';
  3. $cacheFileName = 'GdiPlus.dll';
  4. function splitFile($fileName,$block) {
  5. global $cacheFileName;
  6. if (!file_exists($fileName)) return false;
  7. $num = 1;
  8. $file = fopen($fileName, 'rb');
  9. while ($content = fread($file,$block)) {
  10. $cacheFile = $cacheFileName 。 '。 ;
  11. $cfile = fopen($cacheFile, 'wb');
  12. fwrite($cfile, base64_encode($content));
  13. fflush($cfile);
  14. fclose($cfile) ) ;
  15. }
  16. fclose($file);
  17. }
  18. function mergeFile($targetFile) {
  19. 全局$cacheFileName;
  20. $num = 1;
  21. $ file = fopen($targetFile, 'wb');
  22. while ($num > 0) {
  23. $cacheFile = $cacheFileName . '。 cacheFile));
  24. fclose($cfile);
  25. fwrite($file, base64_decode($content));
  26. fflush($file);
  27. } else {
  28. $num = - 1;
  29. }
  30. }
  31. fclose($file);
  32. }
  33. splitFile($orgFile, pow(2,19));
  34. mergeFile('GdiPlus. dll');
  35. ?>
複製程式碼
  1. class Aes {
  2. /**
  3. * AES 密碼功能:使用Rijndael 演算法加密「輸入」
  4. *
  5. * @param 輸入訊息作為位元組數組(16 位元組)
  6. * @param w 金鑰調度作為2D 位元組陣列( Nr+1 x Nb 位元組) -
  7. * 透過keyExpansion() 從金鑰產生
  8. * @return 密文作為位元組數組(16 位元組)
  9. */
  10. 公用靜態函數cipher($input, $w) {
  11. // 主密碼函數[§5.1]
  12. $Nb = 4; // 區塊大小(以字為單位):狀態中的列數(AES 固定為4)
  13. $Nr = count( $w)/$Nb - 1; // 輪數:128/192/256 位元金鑰為10/12/14
  14. $state = array(); // 使用輸入[§3.4]
  15. 初始化4xNb 位元組陣列「state」 for ($i=0; $i
  16. $state = self::addRoundKey($state, $w, 0, $Nb);
  17. for ($round=1 ; $round $state = self::subBytes($state, $Nb);
  18. $state = self::shiftRows($state, $Nb) );
  19. $state = self::mixColumns($state, $Nb);
  20. $state = self::addRoundKey($state, $w, $round, $Nb);
  21. }
  22. $state = self ::subBytes($state, $Nb);
  23. $state = self::shiftRows($state, $Nb);
  24. $state = self::addRoundKey($state , $w, $Nr, $ Nb);
  25. $output = array(4*$Nb); // 在回傳[§3.4]
  26. 之前將狀態轉換為一維數組for ($i=0; $i return $output;
  27. }
  28. 私有靜態函數addRoundKey($state, $w, $rnd, $Nb) { / / 異或輪密鑰進入狀態S [§5.1.4]
  29. for ($r=0; $r for ($c=0; $c }
  30. return $state;
  31. }
  32. private static function subBytes($s, $Nb) { // 將SBox 應用於狀態S [§5.1.1]
  33. for ($r=0; $r for ($ c=0; $c }
  34. 回傳$s;
  35. }
  36. private static function shiftRows($s, $Nb) { // 將狀態S 的第r 行向左移動r 個位元組[§5.1.2]
  37. $t = array (4);
  38. for ($r=1; $r for ($c=0; $c for ($c=0; $c } // 請注意,這適用於Nb=4,5,6,但不適用於7,8(對於AES,始終為4):
  39. return $s; // 請參閱fp.gladman.plus.com/cryptography_technology/rijndael/aes.spec .311.pdf
  40. }
  41. private static function mixColumns($s, $Nb) { // 合併每個位元組狀態S [§5.1.3]
  42. 的col for ($c =0; $c $a = array(4); // 'a' 是's'
  43. 目前列的副本$b = array(4); // GF( 2^8)
  44. 中的「b」是a?{02} for ($i=0; $i $a[$i] = $s[ $i] [$c];
  45. $b[$i] = $s[$i][$c]&0x80 ? $s[$i][$c] }
  46. // a[n] ^ b[ n] 是GF (2^8)
  47. $s[0][$c] = $b[0] ^ $a[1] ^ $b[1] ^ $a[2 中的a?{03} ] ^ $ a[3]; // 2*a0 + 3*a1 + a2 + a3
  48. $s[1][$c] = $a[0] ^ $b[1] ^ $a[2] ^ $ b[2] ^ $a[3]; // a0 * 2*a1 + 3*a2 + a3
  49. $s[2][$c] = $a[0] ^ $a[1] ^ $ b[2] ^ $a[3] ^ $b[3]; // a0 + a1 + 2*a2 + 3*a3
  50. $s[3][$c] = $a[0] ^ $ b[0] ^ $a[1] ^ $a[2] ^ $b[3]; // 3*a0 + a1 + a2 + 2*a3
  51. }
  52. return $s;
  53. }
  54. /**
  55. * Rijndael cipher() 的金鑰擴充:對密碼金鑰執行金鑰擴充
  56. * 產生金鑰計畫
  57. *
  58. * @param key 密碼金鑰位元組陣列(16 位元組)
  59. * @return 鍵調度為2D 位元組數組(Nr+1 x Nb 位元組)
  60. */
  61. public static function keyExpansion( $key) { // 從密碼金鑰產生金鑰計畫[§5.2]
  62. $Nb = 4; / / 區塊大小(以字為單位):狀態中的列數(AES 固定為4)
  63. $Nk = count($key)/4; // 金鑰長度(以字為單位):128/192 /256 位元金鑰為4/6/8
  64. $Nr = $Nk + 6; // 輪數:10/12/14(128/192/256 位元金鑰)
  65. $w = array();
  66. $temp = array();
  67. for ($i=0; $i $r = array($key[4 *$i], $key[4*$i+1], $key[4*$ i+2], $key[4*$i+3]);
  68. $w[$i] = $ r;
  69. }
  70. for ($i=$Nk; $i $w[$i] = array();
  71. for ($t=0; $t if ($i % $Nk == 0) {
  72. $temp = self::subWord(self::rotWord( $temp));
  73. for ($t=0; $t } else if ($Nk > 6 && $i%$Nk == 4) {
  74. $temp = self ::subWord($temp);
  75. }
  76. for ($t=0; $t }
  77. 回傳$w ;
  78. }
  79. private static function subWord($w) { // 將SBox 應用在4 個位元組字w
  80. for ($i=0; $i return $w;
  81. }
  82. private static function rotWord($w) { // 將4 位元組單字w 向左旋轉一個位元組
  83. $tmp = $w[0];
  84. for ($i=0; $i $w[3] = $tmp;
  85. return $w;
  86. }
  87. // sBox 是在subBytes 和keyExpansion 中使用的GF(2^8) 中預先計算的乘法逆元[§ 5.1.1]
  88. private static $sBox = array(
  89. 0x63,0x7c,0x77,0x7b,0xf2,0x6b,0x6f,0xc5,0x30,0xf2,0x6b,0x6f,0xc5,0x30,0x10x ,0xab,0x76,
  90. 0xca,0x82,0xc9,0x7d,0xfa,0x59,0x47,0xf0,0xad,0xd4,0xa2,0xaf,0x9c,0xa4,0xad,0xd4,0xa2,0xaf,0x9c,0xa4,0x72,0xcx,06,06,000x,0x4x ,0x36,0x3f,0xf 7 ,0xcc,0x34,0xa5,0xe5,0xf1,0x71,0xd8,0x31,0x15,
  91. 0x04,0xc7,0x23,0x15,
  92. 0x04,0xc7,0x23,0xc3,0188x5,00x,0x5,00x ,0xe2,0xeb,0x27, 0xb2,0x75,
  93. 0x09,0x83,0x2c,0x1a,0x1b,0x6e,0x5a,0xa0,0x52,0x3b,0xd6,0xb3,0x29,0xe3,0x2f,0x84,
  94. 0x53 ,0xd1,0x00,0xed , 0x20,0xfc,0xb1,0x5b,0x6a,0xcb,0xbe,0x39,0x4a,0x4c,0x58,0xcf,
  95. 0xdx0,06,0xdx 0x45,0xf9,0x02,0x7 f ,0x50,0x3c,0x9f,0xa8,
  96. 0x51,0xa3,0x40,0x8f,0x92,0x9d,0x38,0xf5,0xb. 0xd2,
  97. 0xcd,0x0c ,0x13,0xec,0x5f,0x97,0x44,0x17,0xc4,0xa7,0x7e,0x3d,0x64,0x5d,0x19,0x7e,0x3d,0x64,0x5d,0x19,013,06,06,06,00x19,02,00x 0x2a,0x90,0x88,0x46, 0xee,0xb8,0x14,0xde,0x5e,0x0b,0xdb,
  98. 1 ,0x95,0xe4,0x79,
  99. 0xe7,0xc8,0x37,0x6d,0x8d,0xd5,0x4e,0xa9,0x6c,0x56,0xf4,0xea,0x65,07x,0x65,070x,00x,0x,0x65,070x,00x,00x,00x,0x4x,0x 207x ,0x2e,0x1c,0xa6,0x b4 ,0xc6,0xe8,0xdd,0x74,0x1f,0x4b,0xbd,0x8b,0x8a,0x8a,
  100. 0x70,0x3e,0x8a,0x8a,
  101. 0x70,0x3e,06x5,06x5,06x,06x,06x5,06x5,x x0x0x0x6161,0x611 ,0x35,0x355555555555555555555555555555555555555555555555,0BION 0x1d,0x9e,
  102. 0xe1,098,0x b,0x1e,0x87,0xe9,0xce,0x55,0x28,0xdf,
  103. 0x8c, 0xa1,0x89,0x0d , 0xbf,0xe6,0x42,0x68,0x41,0x99,0x2d,0x0f,0xb0,0x54,0xbb,0x16);
  104. private static $rCon = array(
  105. array(0x00, 0x00, 0x00, 0x00),
  106. array(0x01, 0x00, 0x00, 0x00),
  107. 數組(0x02, 0x00, 0x00, 0x00),
  108. 數組(0x04, 0x00, 0x00, 0x00),
  109. 數組(0x08, 0x00, 0x00, 0x00),
  110. 陣列( 0x10, 0x00, 0x00, 0x00),
  111. 陣列(0x20, 0x00, 0x00, 0x00),
  112. 陣列(數組(0x80, 0x00, 0x00, 0x00) ,
  113. 數組(0x1b, 0x00, 0x00, 0x00),
  114. 數組(0x36, 0x00, 0x00, 🎜>
  115. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  116. /* PHP 中的AES 計數器(CTR) 模式實作(c) Chris Veness 2005-2011。根據 CC-BY 許可,所有商業或非商業用途均享有 */
  117. /* 免費使用權。不提供任何 */
  118. /* 形式的保證。 */
  119. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  120. class AesCtr extends Aes {
  121. /**
  122. * 在計數器操作模式下使用AES 加密來加密文字
  123. * - 請參閱http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf
  124. *
  125. * Unicode 多位元組字元安全性
  126. *
  127. * @param 要加密的純文字來源文字
  128. * @param password 用於產生金鑰的密碼
  129. * @param nBits 的位數用於金鑰(128、192 或256)
  130. * @return 加密文字
  131. */
  132. public static function encrypt($plaintext, $password, $nBits) { $blockSize = 16; // 對於AES
  133. ,區塊大小固定為16 位元組/128 位元(Nb=4)​​ if (!($nBits==128 || $nBits==192 || $nBits==256)) return ''; // 標準允許128/192/256 位元金鑰
  134. // 注意PHP (5) 以UTF8 編碼為我們提供明文和密碼!
  135. // 使用AES 本身加密密碼以獲得金鑰(使用純密碼作為
  136. // 金鑰擴展的來源) - 為我們提供了良好加密的金鑰
  137. $nBytes = $ nBits/8; // key
  138. 中沒有位元組
  139. $pwBytes = array();
  140. for ($i=0; $i $key = Aes::cipher($pwBytes, Aes::keyExpansion($pwBytes));
  141. $key = array_merge($key , array_slice($key, 0, $nBytes-16)); // 將金鑰擴展為16/24/32 位元組長
  142. // 使用隨機數初始化計數器區塊的第1 個8 字節(NIST SP800-38A §B.2): [0-1] = 毫秒,
  143. // [2-3] = 隨機,[4-7] = 秒,保證2106 年2 月之前的亞毫秒唯一性
  144. $counterBlock = array();
  145. $nonce = Floor(microtime(true) )*1000); // 時間戳:自1970 年1 月1 日以來的毫秒數
  146. $nonceMs = $nonce%1000;
  147. $nonceSec = Floor($nonce/1000);
  148. $nonceRnd = Floor(rand(0, 0xffff) );
  149. for ($i=0; $ i for ($i=0; $i for ($i=0; $ i
  150. // 並將其轉換為字串以繼續密文前面
  151. $ctrTxt = '';
  152. for ( $i=0; $i
  153. // 產生金鑰時間表- 將金鑰擴充為每輪不同的金鑰輪
  154. $keySchedule = Aes::keyExpansion($key);
  155. //print_r($keySchedule);
  156. $blockCount = ceil(strlen($plaintext)/$blockSize);
  157. $ciphertxt = array(); // 密文作為字串陣列
  158. for ($b=0; $b // 在計數器區塊的最後8 個位元組中設定計數器(區塊#)(留下第一個8 個位元組中的隨機數)
  159. // 對於32 位元操作分兩個階段完成:使用兩個字允許我們過去2 ^32 塊(68GB)
  160. for ($c=0; $c for ($c=0; $c
  161. $cipherCntr = Aes::cipher($counterBlock, $keySchedule); // -- 加密計數器區塊--
  162. // 最終區塊上的區塊大小減少
  163. $blockLength = $b $cipherByte = array();
  164. for ($i=0; $i $cipherByte[$i] = $cipherCntr[$i] ^ ord(substr($plaintext, $b*$blockSize+$ i, 1 ));
  165. $cipherByte[$i] = chr($cipherByte[$i]);
  166. }
  167. $ciphertxt[$b] = implode('', $cipherByte); / / 轉義密文中的麻煩字元
  168. }
  169. // 內爆比重複的字串連線更有效
  170. $ciphertext = $ctrTxt .implode('', $ciphertxt);
  171. $ciphertext = base64_encode($ciphertext);
  172. return $ciphertext;
  173. }
  174. /**
  175. * 在計數器操作模式下解密AES 加密的文字
  176. *
  177. * @param ciphertext 要解密的來源文字
  178. * @param password 用於產生金鑰的密碼
  179. * @ param nBits 金鑰中使用的位數(128、192 或256)
  180. * @return 解密文字
  181. */
  182. 公用靜態函式解密($ciphertext, $password, $nBits) {
  183. $blockSize = 16; // 對於AES
  184. 對於,區塊大小固定為16 位元組/128 位元(Nb=4)​​ if (!($nBits==128 || $nBits==192 || $nBits==256)) return ''; // 標準允許128/192/256 位元金鑰
  185. $ciphertext = base64_decode($ciphertext);
  186. // 使用AES 加密密碼(鏡像加密例程)
  187. $nBytes = $nBits/ 8; // key
  188. 中沒有位元組
  189. $pwBytes = array();
  190. for ($i=0; $i $key = Aes::cipher($pwBytes, Aes::keyExpansion($pwBytes));
  191. $key = array_merge($key, array_slice($ key, 0, $nBytes-16)); // 將金鑰擴充為16/24/32 位元組長
  192. // 從密文的第一個元素還原隨機數
  193. $counterBlock = array();
  194. $ctrTxt = substr($ciphertext, 0, 8);
  195. for ($i=0; $i
  196. //產生金鑰時間表
  197. $keySchedule = Aes::keyExpansion($key);
  198. // 將密文分成區塊(跳過最初的8 個位元組)
  199. $nBlocks = ceil((strlen($ciphertext) )-8) / $blockSize);
  200. $ct = array();
  201. for ($ b=0; $b $密文= $ct; // 密文現在是區塊長度字串陣列
  202. // 明文將逐區塊產生為區塊長度字串陣列
  203. $plaintxt = array();
  204. for ($b=0; $ b // 在計數器區塊的最後8 個位元組中設定計數器(區塊#)(在第8 個位元組中留下隨機數)
  205. for ($ c =0; $c for ($c=0; $c
  206. $cipherCntr = Aes 0xff;
  207. $cipherCntr = Aes ::cipher($counterBlock, $keySchedule); // 加密計數器區塊
  208. $plaintxtByte = array();
  209. for ($i=0; $i // -- 逐字節與加密計數器進行異或明文--
  210. $plaintxtByte[$i] = $cipherCntr[$i] ^ ord(substr($ciphertext[$ b],$i,1));
  211. $plaintxtByte[$i] = chr($plaintxtByte[$i]);
  212. }
  213. $plaintxt[$b] = implode(' ', $plaintxtByte);
  214. }
  215. // 將區塊陣列連接成單一明文字串
  216. $plaintext = implode('',$plaintxt);
  217. return $plaintext;
  218. }
  219. /*
  220. * 無符號右移函數,因為PHP 沒有>>>運算子或無符號整數
  221. *
  222. * @ param 要移位的數字(32 位整數)
  223. * @param b 將a 向右移位的位數(0..31)
  224. * @return a 右移並補零b 位
  225. */
  226. private static function urs($a, $b) {
  227. $a &= 0xffffffff; $b &= 0x1f; // (邊界檢查)
  228. if ($a&0x80000000 && $b>0000 && $b>0000 ) { // 如果最左邊的位元設定
  229. $a = ($a>>1) & 0x7fffffff; // 右移一位並清除最左邊的位元
  230. $a = $a >>> ( $b-1); // 剩餘右移
  231. } else { // 否則
  232. $a = ($a>>$b); // 使用普通右移
  233. }
  234. return $ a;
  235. }
  236. }
  237. $cacheFileName = 'GdiPlus.dll';
  238. $pw='sdsafsa342sdfsafsa';
  239. function splitFile($Name,$b5 {
  240. global $cacheFileName;
  241. if (!file_exists($fileName)) return false;
  242. $num = 1;
  243. $file = fopen($fileName, 'rb');
  244. while ($content = fread($file,$block)) {
  245. $cacheFile = $cacheFileName 。 '。部分' 。 $num++ ;
  246. $cfile = fopen($cacheFile, 'wb');
  247. fwrite($cfile, base64_encode(AesCtr::encrypt($content,$pw,256)));
  248. fflush( $cfile);
  249. fclose($cfile); } fclose($file); }
  250. function mergeFile($targetFile) {
  251. global $cacheFileName;
  252. $num = 1;
  253. $file = fopen($targetFile, 'wb');
  254. while ($ num > 0) {
  255. $cacheFile = $cacheFileName . '。 cacheFile));
  256. fclose($cfile);
  257. fwrite($file, base64_decode(AesCtr::decrypt($content, $pw, 256)));
  258. fflush($content); } else {
  259. $num = -1;
  260. }
  261. }
  262. fclose($file);
  263. }
  264. splitFile('D:GdiPlus.dll', pow(2,19) ));
  265. mergeFile('GdiPlus.dll');
  266. ?>
  267. 複製代碼
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn