Home > Article > Backend Development > How to solve the php encryption garbled problem
# Recommended: "The solution to php encryption garbled characters: first encrypt the ciphertext with MD5; then use base64 encryption to avoid garbled characters. The code statement is "$bs_test = base64_encode($test);".
PHP Video Tutorial》
The operating environment of this tutorial: Windows 7 system, PHP version 5.6. This method is suitable for all brands of computers. In a recent project, the original functions were encapsulated into an interface for third-party calls. Among them, signature encryption was involved. RES encryption was used. The idea was to use public keys and The POST parameters are spliced into a string and then public key encrypted. However, RES encryption has requirements for ciphertext and has a length limit. If the POST data is too long, the ciphertext data will be too long and the decryption will fail. Therefore, our idea to solve this problem is to encrypt the ciphertext with MD5 and then encrypt the encrypted data with RES. However, the RES-encrypted data may also be encrypted due to Encoding problems lead to garbled characters, so we also need to perform base64 encryption to avoid garbled characters. The simple code is shown below:/* *$rsa_pub为RES公钥 * */$secret = md5($querystr . $api_secret); //组成密文 openssl_public_encrypt($secret ,$test, $rsa_pub); //公钥加密 $bs_test = base64_encode($test); //进行base64加密
The above is the detailed content of How to solve the php encryption garbled problem. For more information, please follow other related articles on the PHP Chinese website!