php AES加密解密 和 java 加密解密 互通

1,废话不多说直接上代码,先写一个类

class Crypt {

    /**
     * [$cipher 加密模式]
     * @var [type]
     */
    private $cipher = MCRYPT_RIJNDAEL_128;
    private $mode = MCRYPT_MODE_CBC;

    /**
     * [$key 密匙]
     * @var string
     */
    private $secret_key = 'dc1e8b9ace2d60d3992ecb5e30825253';
    
    function setCipher($cipher=''){
        $cipher && $this->cipher = $cipher;
    }

    function setMode($mode=''){
        $mode && $this->mode = $mode;
    }

    function setSecretKey($secret_key=''){
        $secret_key && $this->secret_key = $secret_key;
    }


    //加密
    function encrypt($str,$iv)
    {       
        $size = mcrypt_get_block_size ( MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC );
        $str = $this->pkcs5Pad ( $str, $size );

        //$data=mcrypt_cbc(MCRYPT_RIJNDAEL_128, $this->secret_key, $str, MCRYPT_ENCRYPT, $this->iv);
        //$data=mcrypt_encrypt(MCRYPT_DES, $this->secret_key, $str, MCRYPT_MODE_CBC, $this->iv);
         $data=mcrypt_encrypt ( MCRYPT_RIJNDAEL_128, hex2bin($this->secret_key), $str, MCRYPT_MODE_CBC, $iv );

        //bin2hex() 函数把 ASCII 字符的字符串转换为十六进制值
        $data=strtolower(bin2hex($data));
        return $data;
    }

    //解密
    function decrypt($str)
    {
        $str = $this->hex2bin( strtolower($str));
        //$str = mcrypt_cbc(MCRYPT_RIJNDAEL_128, $this->secret_key, $str, MCRYPT_DECRYPT, $this->iv);
        $str =mcrypt_decrypt( MCRYPT_DES, $this->secret_key, $str, MCRYPT_MODE_CBC, $iv);
        $str = $this->pkcs5Unpad( $str );
        return $str;
    }

    //bin2hex还原
    private function hex2bin($hexData)
    {
        $binData = "";
        for($i = 0; $i < strlen ( $hexData ); $i += 2)
        {
            $binData .= chr(hexdec(substr($hexData, $i, 2)));
        }
        return $binData;
    }

    //PKCS5Padding
    private function pkcs5Pad($text, $blocksize)
    {
        $pad = $blocksize - (strlen ( $text ) % $blocksize);
        return $text . str_repeat ( chr ( $pad ), $pad );
    }

    private function pkcs5Unpad($text)
    {
        $pad = ord ( $text {strlen ( $text ) - 1} );
        if ($pad > strlen ( $text ))
            return false;
        if (strspn ( $text, chr ( $pad ), strlen ( $text ) - $pad ) != $pad)
            return false;
        return substr ( $text, 0, - 1 * $pad );
    }

}

 

2. 传递参数,调用加密方法。

#iv偏移量  128-对应16位长度,这里我用时间戳拼接的
$iv=time().'000000';
echo "iv——   ".$iv;
echo "
"; # java那边需要32位的,我需要转换一下传给java $biniv=bin2hex($iv); echo "bin2iv—— ".$biniv; #调用AES加密方法 $Crypt=new Crypt(); #加密的参数 $idfa="EF578312-2718-470E-966E-7F36602CEE64"; $idfa=$Crypt->encrypt($idfa,$iv);

 

3,下面再例举一个方法,代码就比较少了。

 $cipherText = encrypt("Hello", 'aes-256-cbc');
 exit();

        function encrypt($data, $algo)
        {
            $key = '89fd51c740fa5be134ad23cdf72afe9d';
            //$iv = random_bytes(openssl_cipher_iv_length($algo));
            $iv ='0102030405060708';
            $cipherText = openssl_encrypt(
                    $data,
                    $algo,
                    $key,
                    OPENSSL_RAW_DATA,
                    $iv
                );
        $cipherText = base64_encode($cipherText);
        printData("Ciper Text : $cipherText");

        $cipherText = base64_decode($cipherText);
        $plaintext = openssl_decrypt(
                    $cipherText,
                    $algo,
                    $key,
                    OPENSSL_RAW_DATA,
                    $iv
                );
        printData("Plain Text after decryption : $plaintext");
        }

        function printData($obj)
        {
            print_r($obj);
        }

 

 

你可能感兴趣的:(php,功能,php,java)