Home Backend Development PHP Tutorial php AES encryption compatible with net

php AES encryption compatible with net

Aug 10, 2016 am 08:48 AM
content return string this

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.

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Convert basic data types to strings using Java's String.valueOf() function Convert basic data types to strings using Java's String.valueOf() function Jul 24, 2023 pm 07:55 PM

Convert basic data types to strings using Java's String.valueOf() function In Java development, when we need to convert basic data types to strings, a common method is to use the valueOf() function of the String class. This function can accept parameters of basic data types and return the corresponding string representation. In this article, we will explore how to use the String.valueOf() function for basic data type conversions and provide some code examples to

Detailed explanation of the usage of return in C language Detailed explanation of the usage of return in C language Oct 07, 2023 am 10:58 AM

The usage of return in C language is: 1. For functions whose return value type is void, you can use the return statement to end the execution of the function early; 2. For functions whose return value type is not void, the function of the return statement is to end the execution of the function. The result is returned to the caller; 3. End the execution of the function early. Inside the function, we can use the return statement to end the execution of the function early, even if the function does not return a value.

How to convert char array to string How to convert char array to string Jun 09, 2023 am 10:04 AM

Method of converting char array to string: It can be achieved by assignment. Use {char a[]=" abc d\0efg ";string s=a;} syntax to let the char array directly assign a value to string, and execute the code to complete the conversion.

What is the execution order of return and finally statements in Java? What is the execution order of return and finally statements in Java? Apr 25, 2023 pm 07:55 PM

Source code: publicclassReturnFinallyDemo{publicstaticvoidmain(String[]args){System.out.println(case1());}publicstaticintcase1(){intx;try{x=1;returnx;}finally{x=3;}}}#Output The output of the above code can simply conclude: return is executed before finally. Let's take a look at what happens at the bytecode level. The following intercepts part of the bytecode of the case1 method, and compares the source code to annotate the meaning of each instruction in

2w words detailed explanation String, yyds 2w words detailed explanation String, yyds Aug 24, 2023 pm 03:56 PM

Hello everyone, today I will share with you the basic knowledge of Java: String. Needless to say the importance of the String class, it can be said to be the most used class in our back-end development, so it is necessary to talk about it.

Use Java's String.replace() function to replace characters (strings) in a string Use Java's String.replace() function to replace characters (strings) in a string Jul 25, 2023 pm 05:16 PM

Replace characters (strings) in a string using Java's String.replace() function In Java, strings are immutable objects, which means that once a string object is created, its value cannot be modified. However, you may encounter situations where you need to replace certain characters or strings in a string. At this time, we can use the replace() method in Java's String class to implement string replacement. The replace() method of String class has two types:

Use java's String.length() function to get the length of a string Use java's String.length() function to get the length of a string Jul 25, 2023 am 09:09 AM

Use Java's String.length() function to get the length of a string. In Java programming, string is a very common data type. We often need to get the length of a string, that is, the number of characters in the string. In Java, we can use the length() function of the String class to get the length of a string. Here is a simple example code: publicclassStringLengthExample{publ

How to use the split method in Java String How to use the split method in Java String May 02, 2023 am 09:37 AM

The split method in String uses the split() method of String to split the String according to the incoming characters or strings and return the split array. 1. General usage When using general characters, such as @ or, as separators: Stringaddress="Shanghai@Shanghai City@Minhang District@Wuzhong Road";String[]splitAddr=address.split("@");System .out.println(splitAddr[0]+splitAddr[1]+splitAddr[2]+splitAddr[3

See all articles