├── Adapter ├── AesEncrypt.php ├── Des3Encrypt.php └── DesEncrypt.php ├── Des.php └── README.md /Adapter/AesEncrypt.php: -------------------------------------------------------------------------------- 1 | _secret_key = trim( $key ); 27 | } 28 | 29 | /** 30 | * @brief 加密字符串 31 | * @param $str 32 | * @return string 33 | */ 34 | public function encrypt( $str ){ 35 | self::$_TYPE = self::ENCODE; 36 | return self::_get( $str ); 37 | } 38 | 39 | /** 40 | * @brief 解密字符串 41 | * @param $str 42 | * @return string 43 | */ 44 | public function decrypt( $str ){ 45 | self::$_TYPE = self::DECODE; 46 | return self::_get( $str ); 47 | } 48 | 49 | private function _get( $str ){ 50 | $str = (string) $str; 51 | 52 | if ( strlen( $this->_secret_key ) < 1 ){ 53 | return ''; 54 | } 55 | 56 | $key = md5( $this->_secret_key ); 57 | $key_length = strlen( $key ); 58 | 59 | $str = SELF::$_TYPE === SELF::DECODE 60 | ? base64_decode( $str ) 61 | : substr( md5( $str . $key ), 0 , 8 ) . $str; 62 | 63 | $str_length = strlen($str); 64 | 65 | $rndkey = $box = array (); 66 | 67 | for ($i = 0; $i <= 255; $i ++) { 68 | $rndkey[$i] = ord( $key[$i % $key_length] ); 69 | $box[$i] = $i; 70 | } 71 | 72 | for ($j = $i = 0; $i < 256; $i ++) { 73 | $j = ($j + $box[$i] + $rndkey[$i]) % 256; 74 | $tmp = $box[$i]; 75 | $box[$i] = $box[$j]; 76 | $box[$j] = $tmp; 77 | } 78 | 79 | $result = ''; 80 | 81 | for ($a = $j = $i = 0; $i < $str_length; $i ++) { 82 | $a = ($a + 1) % 256; 83 | $j = ($j + $box[$a]) % 256; 84 | $tmp = $box[$a]; 85 | $box[$a] = $box[$j]; 86 | $box[$j] = $tmp; 87 | $result .= chr( ord( $str[$i] ) ^ ($box[($box[$a] + $box[$j]) % 256]) ); 88 | } 89 | 90 | if (self::$_TYPE == self::DECODE) { 91 | if (substr( $result, 0, 8 ) == substr( md5( substr( $result, 8 ) . $key ), 0, 8 )) { 92 | return substr( $result, 8 ); 93 | } else { 94 | return ''; 95 | } 96 | } else { 97 | return str_replace( '=', '', base64_encode( $result ) ); 98 | } 99 | } 100 | } -------------------------------------------------------------------------------- /Adapter/Des3Encrypt.php: -------------------------------------------------------------------------------- 1 | _iv = $iv; 25 | } 26 | 27 | $this->_secureKey = $this->_padding(trim($key)); 28 | } 29 | 30 | public function encrypt( $str ){ 31 | 32 | //使用MCRYPT_3DES算法,cbc模式 33 | $td = mcrypt_module_open( MCRYPT_3DES, '', MCRYPT_MODE_CBC, ''); 34 | 35 | mcrypt_generic_init($td, $this->_secureKey, $this->_iv); 36 | 37 | //初始处理 38 | $data = mcrypt_generic($td, $str); 39 | 40 | //清理加密模块 41 | mcrypt_generic_deinit($td); 42 | 43 | //结束 44 | mcrypt_module_close($td); 45 | 46 | return $this->_removeBr(base64_encode($data)); 47 | } 48 | 49 | public function decrypt( $str ){ 50 | 51 | $str = base64_decode($str); 52 | 53 | //使用MCRYPT_3DES算法,cbc模式 54 | $td = mcrypt_module_open( MCRYPT_3DES,'',MCRYPT_MODE_CBC,''); 55 | 56 | mcrypt_generic_init($td, $this->_secureKey, $this->_iv); 57 | 58 | //初始处理 59 | $str = mdecrypt_generic($td, $str); 60 | 61 | //清理加密模块 62 | mcrypt_generic_deinit($td); 63 | 64 | //结束 65 | mcrypt_module_close($td); 66 | 67 | return $this->_removePadding($str); 68 | } 69 | 70 | //填充密码,填充至8的倍数 71 | private function _padding( $str ) { 72 | $len = 8 - strlen( $str ) % 8; 73 | 74 | for ( $i = 0; $i < $len; $i++ ) { 75 | $str .= chr( 0 ); 76 | } 77 | 78 | return $str; 79 | } 80 | 81 | //删除回车和换行 82 | private function _removeBr($str) { 83 | $len = strlen( $str ); 84 | $newStr = ""; 85 | $str = str_split($str); 86 | 87 | for ($i = 0; $i < $len; $i++ ) { 88 | if ($str[$i] != '\n' and $str[$i] != '\r') { 89 | $newStr .= $str[$i]; 90 | } 91 | } 92 | return $newStr; 93 | } 94 | 95 | //删除填充符 96 | private function _removePadding( $str ) { 97 | $len = strlen( $str ); 98 | $newStr = ""; 99 | $str = str_split($str); 100 | 101 | for ($i = 0; $i < $len; $i++ ) { 102 | if ($str[$i] != chr( 0 )) { 103 | $newStr .= $str[$i]; 104 | } 105 | } 106 | 107 | return $newStr; 108 | } 109 | } -------------------------------------------------------------------------------- /Adapter/DesEncrypt.php: -------------------------------------------------------------------------------- 1 | _secureKey = trim($key); 22 | } 23 | 24 | /** 25 | * @desc 加密 26 | * @param $str 27 | * @return mixed 28 | */ 29 | public function encrypt( $str ){ 30 | 31 | $ivArray=array(0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF); 32 | $iv = ''; 33 | 34 | foreach ($ivArray as $element) { 35 | $iv .= chr($element); 36 | } 37 | 38 | $size = mcrypt_get_block_size(MCRYPT_DES, MCRYPT_MODE_CBC ); 39 | 40 | $str = $this->_pkcs5Pad($str, $size); 41 | 42 | $data = mcrypt_encrypt(MCRYPT_DES, $this->_secureKey, $str, MCRYPT_MODE_CBC, $iv); 43 | 44 | return base64_encode($data); 45 | } 46 | 47 | /** 48 | * @desc 解密 49 | * @param $str 50 | * @return bool 51 | */ 52 | public function decrypt( $str ){ 53 | 54 | $ivArray=array(0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF); 55 | $iv = ''; 56 | 57 | foreach ($ivArray as $element) { 58 | $iv .= chr($element); 59 | } 60 | 61 | $str = base64_decode($str); 62 | 63 | $result = mcrypt_decrypt(MCRYPT_DES, $this->_secureKey, $str, MCRYPT_MODE_CBC, $iv); 64 | 65 | $result = $this->_pkcs5Unpad( $result ); 66 | 67 | return $result; 68 | } 69 | 70 | private function _pkcs5Pad($text, $blockSize) { 71 | $pad = $blockSize - (strlen( $text ) % $blockSize); 72 | return $text . str_repeat( chr( $pad ), $pad ); 73 | } 74 | 75 | private function _pkcs5Unpad($text) { 76 | $pad = ord( $text {strlen( $text ) - 1} ); 77 | 78 | if ($pad > strlen( $text )) 79 | return false; 80 | 81 | if ( strspn( $text, chr( $pad ), strlen( $text ) - $pad ) != $pad) 82 | return false; 83 | 84 | return substr ( $text, 0, - 1 * $pad ); 85 | } 86 | 87 | } -------------------------------------------------------------------------------- /Des.php: -------------------------------------------------------------------------------- 1 | $mode, 58 | 'secretKey' => $secretKey 59 | ]); 60 | 61 | if ( !isset( self::$_HANDLE_ARRAY[$handle_key] )){ 62 | 63 | switch ( $mode ){ 64 | case self::MODE_DES: 65 | $obj = new Adapter\DesEncrypt( $secretKey ); 66 | break; 67 | case self::MODE_3DES: 68 | $obj = new Adapter\Des3Encrypt( $secretKey, $iv); 69 | break; 70 | case self::MODE_AES: 71 | default: 72 | $obj = new Adapter\AesEncrypt( $secretKey ); 73 | break; 74 | } 75 | 76 | self::$_HANDLE_ARRAY[$handle_key] = $obj; 77 | } 78 | 79 | return self::$_HANDLE_ARRAY[$handle_key]; 80 | } 81 | } 82 | 83 | 84 | spl_autoload_register(function( $className ){ 85 | 86 | $pos = strpos($className, '\\'); 87 | 88 | $_relativePath = substr( $className, $pos + 1); 89 | 90 | $_filePath = __DIR__ . DIRECTORY_SEPARATOR . str_replace( '\\', DIRECTORY_SEPARATOR, $_relativePath ) . '.php'; 91 | 92 | if (is_file( $_filePath )){ 93 | require $_filePath; 94 | } 95 | }); 96 | 97 | 98 | $aes = Des::getInstance( DES::MODE_AES, 'des_key' ); 99 | 100 | var_dump($aes->encrypt('abcdefg')); 101 | 102 | var_dump( $aes->decrypt('GlU3LwJlug19WbslKWcG') ); 103 | 104 | echo "