Heim  >  Artikel  >  Backend-Entwicklung  >  php对称加密算法(DES/AES)类代码

php对称加密算法(DES/AES)类代码

WBOY
WBOYOriginal
2016-07-25 08:43:171327Durchsuche
  1. /**
  2. * 常用对称加密算法类
  3. * 支持密钥:64/128/256 bit(字节长度8/16/32)
  4. * 支持算法:DES/AES(根据密钥长度自动匹配使用:DES:64bit AES:128/256bit)
  5. * 支持模式:CBC/ECB/OFB/CFB
  6. * 密文编码:base64字符串/十六进制字符串/二进制字符串流
  7. * 填充方式: PKCS5Padding(DES)
  8. *
  9. * @author: linvo
  10. * @version: 1.0.0
  11. * @date: 2013/1/10
  12. */
  13. class Xcrypt{
  14. private $mcrypt;
  15. private $key;
  16. private $mode;
  17. private $iv;
  18. private $blocksize;
  19. /**
  20. * 构造函数
  21. *
  22. * @param string 密钥
  23. * @param string 模式
  24. * @param string 向量("off":不使用 / "auto":自动 / 其他:指定值,长度同密钥)
  25. */
  26. public function __construct($key, $mode = 'cbc', $iv = "off"){
  27. switch (strlen($key)){
  28. case 8:
  29. $this->mcrypt = MCRYPT_DES;
  30. break;
  31. case 16:
  32. $this->mcrypt = MCRYPT_RIJNDAEL_128;
  33. break;
  34. case 32:
  35. $this->mcrypt = MCRYPT_RIJNDAEL_256;
  36. break;
  37. default:
  38. die("Key size must be 8/16/32");
  39. }
  40. $this->key = $key;
  41. switch (strtolower($mode)){
  42. case 'ofb':
  43. $this->mode = MCRYPT_MODE_OFB;
  44. if ($iv == 'off') die('OFB must give a IV'); //OFB必须有向量
  45. break;
  46. case 'cfb':
  47. $this->mode = MCRYPT_MODE_CFB;
  48. if ($iv == 'off') die('CFB must give a IV'); //CFB必须有向量
  49. break;
  50. case 'ecb':
  51. $this->mode = MCRYPT_MODE_ECB;
  52. $iv = 'off'; //ECB不需要向量
  53. break;
  54. case 'cbc':
  55. default:
  56. $this->mode = MCRYPT_MODE_CBC;
  57. }
  58. switch (strtolower($iv)){
  59. case "off":
  60. $this->iv = null;
  61. break;
  62. case "auto":
  63. $source = PHP_OS=='WINNT' ? MCRYPT_RAND : MCRYPT_DEV_RANDOM;
  64. $this->iv = mcrypt_create_iv(mcrypt_get_block_size($this->mcrypt, $this->mode), $source);
  65. break;
  66. default:
  67. $this->iv = $iv;
  68. }
  69. }
  70. /**
  71. * 获取向量值
  72. * @param string 向量值编码(base64/hex/bin)
  73. * @return string 向量值
  74. */
  75. public function getIV($code = 'base64'){
  76. switch ($code){
  77. case 'base64':
  78. $ret = base64_encode($this->iv);
  79. break;
  80. case 'hex':
  81. $ret = bin2hex($this->iv);
  82. break;
  83. case 'bin':
  84. default:
  85. $ret = $this->iv;
  86. }
  87. return $ret;
  88. }
  89. /**
  90. * 加密
  91. * @param string 明文
  92. * @param string 密文编码(base64/hex/bin)
  93. * @return string 密文
  94. */
  95. public function encrypt($str, $code = 'base64'){
  96. if ($this->mcrypt == MCRYPT_DES) $str = $this->_pkcs5Pad($str);
  97. if (isset($this->iv)) {
  98. $result = mcrypt_encrypt($this->mcrypt, $this->key, $str, $this->mode, $this->iv);
  99. } else {
  100. @$result = mcrypt_encrypt($this->mcrypt, $this->key, $str, $this->mode);
  101. }
  102. switch ($code){
  103. case 'base64':
  104. $ret = base64_encode($result);
  105. break;
  106. case 'hex':
  107. $ret = bin2hex($result);
  108. break;
  109. case 'bin':
  110. default:
  111. $ret = $result;
  112. }
  113. return $ret;
  114. }
  115. /**
  116. * 解密
  117. * @param string 密文
  118. * @param string 密文编码(base64/hex/bin)
  119. * @return string 明文
  120. */
  121. public function decrypt($str, $code = "base64"){
  122. $ret = false;
  123. switch ($code){
  124. case 'base64':
  125. $str = base64_decode($str);
  126. break;
  127. case 'hex':
  128. $str = $this->_hex2bin($str);
  129. break;
  130. case 'bin':
  131. default:
  132. }
  133. if ($str !== false){
  134. if (isset($this->iv)) {
  135. $ret = mcrypt_decrypt($this->mcrypt, $this->key, $str, $this->mode, $this->iv);
  136. } else {
  137. @$ret = mcrypt_decrypt($this->mcrypt, $this->key, $str, $this->mode);
  138. }
  139. if ($this->mcrypt == MCRYPT_DES) $ret = $this->_pkcs5Unpad($ret);
  140. }
  141. return $ret;
  142. }
  143. private function _pkcs5Pad($text){
  144. $this->blocksize = mcrypt_get_block_size($this->mcrypt, $this->mode);
  145. $pad = $this->blocksize - (strlen($text) % $this->blocksize);
  146. return $text . str_repeat(chr($pad), $pad);
  147. }
  148. private function _pkcs5Unpad($text){
  149. $pad = ord($text{strlen($text) - 1});
  150. if ($pad > strlen($text)) return false;
  151. if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) return false;
  152. $ret = substr($text, 0, -1 * $pad);
  153. return $ret;
  154. }
  155. private function _hex2bin($hex = false){
  156. $ret = $hex !== false && preg_match('/^[0-9a-fA-F]+$/i', $hex) ? pack("H*", $hex) : false;
  157. return $ret;
  158. }
  159. }
复制代码
使用示例:
  1. header('Content-Type:text/html;Charset=utf-8;');
  2. include "xcrypt.php";
  3. echo '
    '; 
  4. //////////////////////////////////////
  5. $a = isset($_GET['a']) ? $_GET['a'] : '测试123';
  6. //密钥
  7. $key = '12345678123456781234567812345678'; //256 bit
  8. $key = '1234567812345678'; //128 bit
  9. $key = '12345678'; //64 bit
  10. //设置模式和IV
  11. $m = new Xcrypt($key, 'cbc', 'auto');
  12. //获取向量值
  13. echo '向量:';
  14. var_dump($m->getIV());
  15. //加密
  16. $b = $m->encrypt($a, 'base64');
  17. //解密
  18. $c = $m->decrypt($b, 'base64');
  19. echo '加密后:';
  20. var_dump($b);
  21. echo '解密后:';
  22. var_dump($c);
  23. /////////////////////////////////////////
  24. echo '';
复制代码


php, DES, AES


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn