In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
How to use asymmetric encryption algorithm in PHP, many novices are not very clear about this. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can get something.
explain
The asymmetric encryption algorithm requires two keys: public key (publickey) and private key (privatekey).
The public key and the private key are a pair. If the data is encrypted with the public key, it can only be decrypted with the corresponding private key.
If the data is encrypted with a private key, it can only be decrypted with the corresponding public key.
Because encryption and decryption use two different keys, this algorithm is called asymmetric encryption algorithm.
Working with scen
PHP writes API for the client to encrypt / decrypt the data.
Create private key and public key
/ / generate the original RSA private key file
Openssl genrsa-out rsa_private_key.pem 1024
/ / convert the original RSA private key to pkcs8 format
Openssl pkcs8-topk8-inform PEM-in rsa_private_key.pem-outform PEM-nocrypt-out private_key.pem
/ / generate RSA public key
Openssl rsa-in rsa_private_key.pem-pubout-out rsa_public_key.pem
/ / We use the private key rsa_private_key.pem on the server side, and issue the public key to the front end such as android and ios.
Server class library
Class Rsa {
Private static $PRIVATE_KEY = 'rsa_private_key.pem content'
Private static $PUBLIC_KEY = 'rsa_public_key.pem content'
/ * *
* obtain the private key
* @ return bool | resource
, /
Private static function getPrivateKey ()
{
$privKey = self::$PRIVATE_KEY
Return openssl_pkey_get_private ($privKey)
}
/ * *
* obtain the public key
* @ return bool | resource
, /
Private static function getPublicKey ()
{
$publicKey = self::$PUBLIC_KEY
Return openssl_pkey_get_public ($publicKey)
}
/ * *
* Private key encryption
* @ param string $data
* @ return null | string
, /
Public static function privEncrypt ($data ='')
{
If (! is_string ($data)) {
Return null
}
Return openssl_private_encrypt ($data,$encrypted,self::getPrivateKey ())? Base64_encode ($encrypted): null
}
/ * *
* Public key encryption
* @ param string $data
* @ return null | string
, /
Public static function publicEncrypt ($data ='')
{
If (! is_string ($data)) {
Return null
}
Return openssl_public_encrypt ($data,$encrypted,self::getPublicKey ())? Base64_encode ($encrypted): null
}
/ * *
* decryption of private key
* @ param string $encrypted
* @ return null
, /
Public static function privDecrypt ($encrypted ='')
{
If (! is_string ($encrypted)) {
Return null
}
Return (openssl_private_decrypt (base64_decode ($encrypted), $decrypted, self::getPrivateKey ())? $decrypted: null
}
/ * *
* Public key decryption
* @ param string $encrypted
* @ return null
, /
Public static function publicDecrypt ($encrypted ='')
{
If (! is_string ($encrypted)) {
Return null
}
Return (openssl_public_decrypt (base64_decode ($encrypted), $decrypted, self::getPublicKey ())? $decrypted: null
}
}
Server-side use
Require_once "Rsa.php"
$rsa = new Rsa ()
$data ['name'] =' Tom'
$data ['age'] =' 20'
PrivEncrypt = $rsa- > privEncrypt (json_encode ($data))
After echo 'private key encryption:'. $privEncrypt.'
'
$publicDecrypt = $rsa- > publicDecrypt ($privEncrypt)
Echo 'public key decryption:'. $publicDecrypt.'
'
PublicEncrypt = $rsa- > publicEncrypt (json_encode ($data))
After echo 'public key encryption:'. $publicEncrypt.'
'
$privDecrypt = $rsa- > privDecrypt ($publicEncrypt)
Echo 'private key decrypted:'. $privDecrypt.'
'
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.