Home> php教程> PHP开发> body text

yii2.0 encryption and decryption processing method

黄舟
Release: 2017-01-03 09:44:18
Original
1328 people have browsed it

Yii provides convenient helper functions that allow you to encrypt and decrypt data using a security key. Data is transmitted through an encryption function so that only someone with the security key can decrypt it. For example, we need to store some information in our database, but we need to ensure that only people with the security key can see it (even if the application's database is leaked)

$data is the content you want to encrypt ,

$secretKey is the password you set yourself,

$encryptedData = Yii::$app->getSecurity()->encryptByPassword($data, $secretKey);
Copy after login

Later, when the user needs to read the data:

$encryptedData 是你要解密的内容 $secretKey 是你自己设置加密时的密码 $data = Yii::$app->getSecurity()->decryptByPassword($encryptedData, $secretKey);
Copy after login

But Encrypt the string, and the encrypted string is a string of garbled characters (it does look like garbled characters, and the specifics need to be verified), which is not conducive to our next operation.

We can use base64 to process the encrypted string. The processed string is composed of letters and numbers

Application example:

//Invitation registration

$id = Yii::$app->user->getId();//获取登录用户id //加密(此处加密密码设为空) $uid = base64_encode(yii::$app->security->encryptByPassword($id,'')); //解密 $iss=yii::$app->security->decryptByPassword(base64_decode($uid),'');
Copy after login

The above is the content of yii2.0 encryption and decryption processing method. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!


Related labels:
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 Recommendations
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!