Home > Backend Development > PHP Tutorial > Garbled characters problem after PHP aes (ecb) decryption_PHP tutorial

Garbled characters problem after PHP aes (ecb) decryption_PHP tutorial

WBOY
Release: 2016-07-13 09:49:17
Original
1374 people have browsed it

The problem of garbled characters after decryption by PHP aes (ecb)

This article mainly introduces the solution to the problem of garbled characters after decryption by PHP aes (ecb). It is very simple and practical. If necessary Friends can refer to it.

Content: abcd

KEY:1234567890123456

Encrypted base64: T7UT2NQ1AFvR9unjA0wKWA==

 ?

1

2

3

4

5

6

7

function apiDataDecrypt($data, $key='') {

$data = base64_decode($data);

$pad = 16 - (strlen($data) % 16);

$padData = $data . str_repeat(chr($pad), $pad);

return mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key,$padData, MCRYPT_MODE_ECB);

}

}

1

2

3

1

2

3

4

5

6

7

8

9

10

11

12

13

14

$key = '1234567890123456';

$content = 'T7UT2NQ1AFvR9unjA0wKWA==';

$desStr = decryptString($content,$key);

echo($desStr);

 

function decryptString($str,$key) {

$str = base64_decode($str);

$str = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $str, MCRYPT_MODE_ECB);

$block = mcrypt_get_block_size('rijndael_128', 'ecb');

$pad = ord($str[($len = strlen($str)) - 1]);

$len = strlen($str);

$pad = ord($str[$len-1]);

return substr($str, 0, strlen($str) - $pad);

}

4

5

6

function apiDataDecrypt($data, $key='') { $data = base64_decode($data); $pad = 16 - (strlen($data) % 16); $padData = $data . str_repeat(chr($pad), $pad); return mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key,$padData, MCRYPT_MODE_ECB); }
}
Attach the solution directly  ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 $key = '1234567890123456'; $content = 'T7UT2NQ1AFvR9unjA0wKWA=='; $desStr = decryptString($content,$key); echo($desStr); function decryptString($str,$key) { $str = base64_decode($str); $str = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $str, MCRYPT_MODE_ECB); $block = mcrypt_get_block_size('rijndael_128', 'ecb'); $pad = ord($str[($len = strlen($str)) - 1]); $len = strlen($str); $pad = ord($str[$len-1]); return substr($str, 0, strlen($str) - $pad); }
The above is the entire content of this article, I hope you all like it. http://www.bkjia.com/PHPjc/1020275.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1020275.htmlTechArticlePHP aes (ecb) garbled code problem after decryption This article mainly introduces the garbled code problem after PHP aes (ecb) decryption The solution is very simple and practical, friends in need can refer to it. ...
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