Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement AES-128-CBC-PKCS5Padding encryption by PHP

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how to implement AES-128-CBC-PKCS5Padding encryption in PHP, Xiaobian thinks it is quite practical, so share it with you as a reference, I hope you can gain something after reading this article.

In the process of interfacing API and data interaction, it is inevitable that some sensitive data will be transmitted. In order to make the data interaction more secure, the data is encrypted.

The other party's request is to perform AES-128-CBC-PKCS5 Padding encryption on the service data of the interface, and then perform Base64 encoding to submit the obtained final character string. The key (key) and initialization vector (offset) corresponding to the encryption method are also given.

First of all, after seeing this encryption method, I searched in the PHP function library to see if there is a corresponding encryption function. However, after looking for a circle, I found that there is no corresponding encryption function. I need to implement it myself. After that, I learned that I can use the mcrypt function extension to implement it, but the mcrypt function has been abandoned since PHP 7.1.0. It is strongly recommended not to use this function. I have to look for other encryption function libraries. I saw the OpenSSL function at the bottom of the manual. By understanding the openssl_encrypt(encrypted data) function found that you can achieve the requirements,openssl_encrypt specific use method, please see the manual

The implementation function is as follows:

function encrypt($input, $key, $iv){ return base64_encode(openssl_encrypt($input, 'AES-128-CBC', $key, OPENSSL_RAW_DATA,$iv)); } //decryption function decrypt($input, $key, $iv){ return openssl_decrypt (base64_decode($input), 'AES-128-CBC',$key, OPENSSL_RAW_DATA, $iv); } //Test encryption (I use the json string here) $dataJson = '[{"Code": "123123", "Name": "Bob", "Address": "\u94f6\u5ddd\u5e02"}, {"Code": "464776", "Name": "Hello", "Address":"\u5317\u4eac\u5e02"}]'; print_r (encrypt($dataJson, $key, $iv));//u9Bd8oHXDGvjZcTIX9HK1r1q+aSu+/48gsfoGVrxoScZuX8yaj/xco8F2yHt2T987JNHil9LwjAmu9o5NJaicWQDaiKwMD5o70k1A9bGjDd71xb4hXRx3ddZwI85oTQQEUQLadR5C759SdaN 8AOXlzH+yGlAWTOaEleulKoRTwaknG1NCM/qIRQ8gI5lzv+D //Test decryption $strr = 'u9Bd8oHXDGvjZcTIX9HK1r1q+aSu+/48gsfoGVrxoScZuX8yaj/xco8F2yHt2T987JNHil9LwjAmu9o5NJaicWQDaiKwMD5o70k1A9bGjDd71xb4hXRx3ddZwI85oTQQEUQLadR5C759SdaN8AOxlzH+yGlAWTOaEleulKoRTwaknG1NCM/qIRQ8gI5lzv+D'; print_r (decrypt($strr, $key, $iv)); // [{"Code": "123123", "Name": "Bob", "Address": "\u94f6\u5ddd\u5e02"}, {"Code": "464776", "Name": "Hello", "Address":"\u5317\u4eac\u5e02"}]

Self-test pass, a face proudly waiting for the joint adjustment. Then began to write other business to go, after a few days, the results of a joint investigation found that I encrypted the other party can not decrypt, the other party encrypted I can not decrypt, encryption algorithm does not match (scratching his head. After Google search related questions, found a sentence, roughly meaning that in the abandoned mcrypt encryption library, 128 actually refers to the block size rather than the key size, but in openssl aes-128-cbc 128 refers to the key size, that is, when using a valid 256-bit key, they are all aes-256, and if you want to convert mcrypt to openssl encryption, Mcrypt 128 needs to be written as openssl 256, so hold a try attitude, change aes-128-cbc to aes-256-cbc, and then debug, finally found that the joint debugging passed. However, the specific technical details and principles are not very clear. First modify and use it like this, and then study it when you have time.

> ** The solution is to replace the encryption method AES-128-CBC with AES-256-CBC** About "PHP how to achieve AES-128-CBC-PKCS5Padding encryption" This article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report