Home > PHP Framework > YII > body text

How yii implements data encryption and decryption

王林
Release: 2020-05-10 17:00:26
forward
2833 people have browsed it

How yii implements data encryption and decryption

1. Encrypt the password and verify whether the password transmitted by the client is correct

1. Encrypt the password

$hash = Yii::$app->getSecurity()->generatePasswordHash($password);
Copy after login

2. Verify the password passed by the client to determine whether it is correct

//$password:客户端传递的明文密码,$hash:对密码进行加密后的哈希值 
if (Yii::$app->getSecurity()->validatePassword($password, $hash)) { 
  // 验证成功(密码正确) 
} else { 
  // 验证失败(密码错误) 
}
Copy after login

2. Generate a pseudo-random data

When we reset the password, we often send an email to In the user's mailbox, give him a reset password. At this time, we can use Yii's pseudo-random data method to generate a pseudo-random data as a password for the user

//默认生成32为随机字符,可以指定位数生成指定位数的伪随机数 
$key = Yii::$app->getSecurity()->generateRandomString();
Copy after login

3. Yii common data encryption and decryption

Common encryption methods in Yii include: encryptByPassword() and encryptByKey()

Common decryption methods in Yii include: decryptByPassword() and decryptByKey()

1. encryptByPassword() and decryptByPassword()

Encryption:

//$data:需要加密的信息,$secretKey:加密时使用的密钥(key) 
$encryptedData = Yii::$app->getSecurity()->encryptByPassword($data, $secretKey);
Copy after login
Copy after login

Decryption:

//$encryptedData:需要解密的信息,$secretKey:加密时使用的密钥(key) 
$data = Yii::$app->getSecurity()->decryptByPassword($encryptedData, $secretKey);
Copy after login

2. encryptByKey() and decryptByKey()

Encryption:

//$data:需要加密的信息,$secretKey:加密时使用的密钥(key) 
$encryptedData = Yii::$app->getSecurity()->encryptByPassword($data, $secretKey);
Copy after login
Copy after login

Decryption:

//$encryptedData:需要解密的信息,$secretKey:加密时使用的密钥(key) 
$data = Yii::$app->getSecurity()->decryptByKey($encryptedData, $secretKey);
Copy after login

Recommended tutorial: yii

The above is the detailed content of How yii implements data encryption and decryption. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:jb51.net
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 [email protected]
Popular Tutorials
More>
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!