Home > Backend Development > PHP Tutorial > php AES encryption compatible with net

php AES encryption compatible with net

WBOY
Release: 2016-08-10 08:48:37
Original
1068 people have browsed it

In the past few days, I have been making a program to encrypt PHP AES and then decrypt it in .net. I did the complement of pkcs7. After the ciphertext was sent to .net, it still failed to decrypt, prompting The padding is invalid and cannot be removed. It passed the inspection program and it turned out that The encryption vector is written incorrectly. The format of the encrypted vector in .net is an array, and in PHP it should be converted to a string with a slash. I deleted an extra 0 during the conversion. Let’s look at the programs below. You can use these programs after setting your own keys and IVs.

class AESMcrypt{


    /** 
     * 设置默认的加密key 32位
     * @var str 
	 * 为了保密省略后半部分
     */ 
    private static $defaultKey = "1A426B316FB648...........";

    /** 
     * 设置默认加密向量
     * @var str
	 * 为了保密省略后半部分
     */
	 
	//在.net中的格式为
	//$iv='{ 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF............}';
	
    private $iv = "\x12\x34\x56\x78\x90\xAB\xCD\xEF\...........";
     
    /** 
     * 设置加密算法 
     * @var str 
     */ 
    private $cipher; 
     
    /** 
     * 设置加密模式 
     * @var str 
     */ 
    private $mode; 
     
    public function __construct($cipher = MCRYPT_RIJNDAEL_128, $mode = MCRYPT_MODE_CBC){ 
        $this->cipher = $cipher; 
        $this->mode = $mode; 
    } 
     
    /** 
     * 对内容加密,注意此加密方法中先对内容使用padding pkcs7,然后再加密。 
     * @param str $content    需要加密的内容 
     * @return str 加密后的密文 
     */ 
    public function encrypt($content){ 
        if(empty($content)){
            return null; 
        }
		
		$srcdata = $this->addPkcs7Padding($content);
        return mcrypt_encrypt($this->cipher, $this->getSecretKey(), $srcdata, $this->mode, $this->iv);
    }
	
	/**
	 * pkcs7补码
	 *
	 * @param string $string  明文
	 *
	 * @return String
	 */ 
	function addPkcs7Padding($string) {
		$blocksize = mcrypt_get_block_size($this->cipher, $this->mode);
		$len = strlen($string); //取得字符串长度
		$pad = $blocksize - ($len % $blocksize); //取得补码的长度
		$string .= str_repeat(chr($pad), $pad); //用ASCII码为补码长度的字符, 补足最后一段
		return $string;
	}

    /** 
     * 对内容解密,注意此加密方法中先对内容解密。再对解密的内容使用padding pkcs7去除特殊字符。 
     * @param String $content    需要解密的内容 
     * @return String 解密后的内容 
     */ 
    public function decrypt($content){ 
        if(empty($content)){ 
            return null; 
        } 

        $content = mcrypt_decrypt($this->cipher, $this->getSecretKey(), $content, $this->mode, $this->iv); 
        //$block = mcrypt_get_block_size($this->cipher, $this->mode); 
        $pad = ord($content[($len = strlen($content)) - 1]); 
        return substr($content, 0, strlen($content) - $pad); 
    }
	
	public function getSecretKey()
	{
		return self::$defaultKey;
	}
}
Copy after login

The above introduces PHP AES encryption compatible with net, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template