Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use asymmetric encryption algorithm in PHP

Shulou Source: shulou.com Published: 2022-06-02 05:46:34 10月04日 Update

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.

Tags: Public key encryption algorithm symmetry content data service original two only key help generation different clear front end scenario client client for this Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Shulou Information Apple vpn OPPO Reno