Problems encountered and solved in the process of connecting jiangxi public service: 1. Use AES/ECB/PKCS5Padding to decrypt the return receipt message. 2

In my work, I encountered the situation of using AES encryption for the receipt message sent by the receiver, and the encryption mode is written in the docking document: AES/ECB/PKCS5Padding. Although the other party provided the JAVA version of the encryption decryption method, more or less do not understand. The following analyzes the problems in the process and their solutions

Methods in JAVA:

DecodeBase64 (key.getBytes(charseName:”UTF-8″)) In fact, it is equivalent to PHP base64_decode(key), don’t bother with the long string behind

To solve

AES/ECB/PKCS5Padding decryption in PHP As of PHP7.1, McRypt is deprecated and replaced with OpenSSL. Here are two implementations:

  1. PHP version < 7.1

Refer to the article: blog.csdn.net/zhang494071…

  1. PHP version >= 7.1

Regarding the openSSL_decrypt parameter, the second parameter is selected according to the length of your key, using the 16-bit key as an example. About openssl_decrypt parameters, suggest you to look at this article: www.cnblogs.com/jingxiaoniu…

function decrypt($str, $key) { $decrypted = openssl_decrypt( $str, 'AES-128-ECB', $key, OPENSSL_ZERO_PADDING ); $dec_s = strlen($decrypted); $padding = ord($decrypted[$dec_s-1]); $decrypted = substr($decrypted, 0, -$padding); return $decrypted; } $original_data = 'ciphertext '; $key = $key; $result = decrypt($original_data, base64_decode($key)); var_dump($result);Copy the code