使用mycript.class.php进行PHP加密/解密

时间:2015-02-20 10:59:17

标签: php encryption mcrypt

我的代码出了什么问题?我无法解密文本! 这是我从解密文本中得到的:„Ïiµ£lcäâä0íà¯DO‰yºbôYð©;Î 我该怎么办?

36b35b3a0ed2c91b126183a40252ff6d2c28690d005538cdf33aeb99f15e2eb8 „Ïiµ£lcäâä0íà¯DO‰yºbôYð©;Î

<?php 
include('mcrypt.class.php');
$key = "Red!Hat#34505";
$cc_number = '4123412341234123';
print $decode ;

$oEncrypt = new CEncoder($cc_number, $key);
$iv = $oEncrypt->GetIV();
$enc = $oEncrypt->Encode();
$card_number_encrypted = $enc;
$iv3 = $iv;
print $card_number_encrypted;
echo "<br>";
print $iv;
$oEncrypt = new CEncoder($card_number_encrypted, $key);
$iv = $oEncrypt->SetIV($iv3);
$enc = $oEncrypt->Decode();
$card_number2 = $enc;
echo "<br>";
echo $card_number2;

?>

mcrypt.class.php

class CEncoder {
    var $__encString;
    var $__cypher;  
    var $__iv;      
    var $__key;     
    function CEncoder($string, $key, $mode=0, $cypher=MCRYPT_RIJNDAEL_128) {
        if($mode) {
            $string = $this->_hex2bin($string);
        }
        //print "$string<br>$key<br>$cypher<br>";
        $this->__encString  = $string;
        $this->__key        = $key;
        $this->__cypher     = $cypher;
        $this->CreateIV();
    }
    function _hex2bin($data) {
        $len = strlen($data);
        return pack("H" . $len, $data);
    }
    function Encode() {
        $encoded = mcrypt_encrypt($this->__cypher, $this->__key, $this->__encString, 
                                  MCRYPT_MODE_CBC, $this->__iv);
        return bin2hex($encoded);
    }
    function Decode() {
        //print $this->__cypher ."<br>". $this->__key ."<br>". $this->__encString ."<br>". $this->__iv ."<br>";
        $decoded = mcrypt_decrypt($this->__cypher, $this->__key, $this->__encString, 
                                  MCRYPT_MODE_CBC, $this->__iv);
        return trim($decoded);
    }
    function CreateIV() {
        $iv = mcrypt_create_iv(mcrypt_get_iv_size($this->__cypher, MCRYPT_MODE_ECB), MCRYPT_RAND);
        //print "<br>New IV Set to $iv<br>";
        $this->__iv = $iv;
    }
    function SetIV($iv) {
        $iv = $this->_hex2bin($iv);
        $this->__iv = $iv;
    }
    function GetIV() {
        $iv = $this->__iv;
        $iv = bin2hex($iv);
        return $iv;
    }
    function SetString($string, $mode=0) {
        if($mode) {
            $string = $this->_hex2bin($string);
        }
        $this->__encString  = $string;
    }

} ?&GT;

1 个答案:

答案 0 :(得分:0)

你可以看看这个 - 或者使用它,解构它......

class encryption{
    private $config;

    public function __construct( $options=array() ){
        $this->config=array_merge(
            array(
                'cipher'    =>  MCRYPT_RIJNDAEL_256,
                'mode'      =>  MCRYPT_MODE_ECB,
                'key'       =>  FALSE,
                'iv'        =>  FALSE,
                'size'      =>  FALSE,
                'base64'    =>  TRUE,
                'salt'      =>  FALSE
            ),
            $options
        );
    }
    private function getivs( $config=object ){
        $config->size=mcrypt_get_iv_size( $config->cipher, $config->mode );
        $config->iv=mcrypt_create_iv( $config->size, MCRYPT_RAND );
    }
    public function encrypt( $data=NULL ){
        $config=(object)$this->config;
        $this->getivs( $config );
        $data=trim( $data );
        $module = mcrypt_module_open( $config->cipher, '', $config->mode, '' );
        mcrypt_generic_init( $module, $config->key, $config->iv );

        $output = $config->base64 ? base64_encode( mcrypt_generic( $module, $data ) ) : mcrypt_generic( $module, $data );

        mcrypt_generic_deinit( $module );
        mcrypt_module_close( $module );
        return $output;
    }
    public function decrypt( $data=NULL ){
        $config=(object)$this->config;
        $this->getivs( $config );
        mb_detect_order( 'auto' );
        $encoding=mb_detect_encoding( $data );
        if( !$data or is_null( $data ) or empty( $data ) or !$encoding or $data=='' or base64_decode( $data )=='' ) return FALSE;

        $module = mcrypt_module_open( $config->cipher, '', $config->mode, '' );
        mcrypt_generic_init( $module, $config->key, $config->iv );

        $output = $config->base64 ? rtrim( mdecrypt_generic( $module, base64_decode( $data ) ),"\0" ) : rtrim( mdecrypt_generic( $module, $data ),"\0" );

        mcrypt_generic_deinit( $module );
        mcrypt_module_close( $module );
        return urldecode( $output );
    }
}//end class