How to use PHPrsa for encryption and decryption-PHP Tutorial

Source: Internet
Author: User
Tags begin rsa private key openssl rsa php server asymmetric encryption
How to use PHPrsa for encryption and decryption. How to use PHPrsa to encrypt and decrypt PHPrsa this article mainly introduces how to use PHPrsa to encrypt and decrypt PHPrsa, this article describes how to use the generated public key, private key, and PHP rsa encryption and decryption.

 PHP rsa encryption and decryption usage

This article mainly introduces the use of PHP rsa encryption and decryption. This article describes how to generate a public key, private key, and example of using the generated public key and private key in PHP for encryption and decryption, for more information, see

When the php server interacts with the client and provides open APIs, it usually needs to encrypt sensitive part of api data transmission. at this time, rsa asymmetric encryption can be used, the following example shows how to use php to encrypt and decrypt data.

1. the first step of encryption and decryption is to generate a public key and private key pair. the content encrypted by the private key can be decrypted through the public key (or vice versa)

Download the open-source RSA key generation tool openssl (usually the program is provided in Linux), decompress it to an independent folder, enter the bin directory, and execute the following command:

?

1

2

3

Openssl genrsa-out rsa_private_key.pem 1024

Openssl pkcs8-topk8-inform PEM-in rsa_private_key.pem-outform PEM-nocrypt-out private_key.pem

Openssl rsa-in rsa_private_key.pem-pubout-out rsa_public_key.pem

The first command generates the original RSA private key file rsa_private_key.pem. the second command converts the original RSA private key to pkcs8. The third command generates the RSA public key rsa_public_key.pem.

We can see from the above that the corresponding public key can be generated through the private key, so we use the private key private_key.pem on the server side, and the public key is issued to the android and ios frontend.

2. use the generated public key and private key in php for encryption and decryption, and directly upload the code

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

$ Private_key = '----- begin rsa private key -----

MIICXQIBAAKBgQC3 // sR2tXw0wrC2DySx8vNGlqt3Y7ldU9 + LBLI6e1KS5lfc5jl

Bytes/

Gqx7/IRia3zQfxGv/qEkyGOx + XALVoOlZqDwh76o2n1vP1D + tD3amHsK7QIDAQAB

AoGBAKH14bMitESqD4PYwODWmy7rrrvyFPEnJJTECLjvKB7IkrVxVDkp1XiJnGKH

2h5syHQ5qslPSGYJ1M/XkDnGINwaLVHVD3BoKKgKg1bZn7ao5pXT + herqxaVwWs6

Ga63yVSIC8jcODxiuvxJnUMQRLaqoF6aUb/2VWc2T5MDmxLhAkEA3pwGpvXgLiWL

3366qlyzlrlrbfrurn4cyl4uyaakokkavzly04glle8ycgoc2dzl4eil4l/+ x/gaq

DeJU/cHLRQJBANOZY0mEoVkwhU4bScSdnfM6usQowYBEwHYYh/OTv1a3SqcCE1f +

Bytes

Oiq + Y8SgCCs73qixrU1YpJy9yKA/meG9smsl4Oh9IOIGI + zUygh9YdSmEq0CQQC2

4G3IP2G3lNDRdZIm5NZ7PfnmyRabxk/UgVUWdk47IwTZHFkdhxKfC8QepUhBsAHL

Bytes

YI9lblCsyfpl25l7l5zmJrAHn45zAiOoBrWqpM5edu7c

----- End rsa private key -----';

$ Public_key = '----- begin public key -----

MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC3 // sR2tXw0wrC2DySx8vNGlqt

3Y7ldU9 + LBLI6e1KS5lfc5jlTGF7KBTSkCHBM3ouEHWqp1ZJ85iJe59aF5gIB2kl

Bd6h4wrbbHA2XE1sq21ykja/Gqx7/IRia3zQfxGv/qEkyGOx + XALVoOlZqDwh76o

2n1vP1D + tD3amHsK7QIDAQAB

----- End public key -----';

// Echo $ private_key;

$ Pi_key = openssl_pkey_get_private ($ private_key); // This function can be used to determine whether the private key is available. Resource id can be returned.

$ Pu_key = openssl_pkey_get_public ($ public_key); // This function can be used to determine whether the public key is available.

Print_r ($ pi_key); echo "\ n ";

Print_r ($ pu_key); echo "\ n ";

$ Data = "aassssasssddd"; // raw data

$ Encrypted = "";

$ Decrypted = "";

Echo "source data:", $ data, "\ n ";

Echo "private key encrypt: \ n ";

Openssl_private_encrypt ($ data, $ encrypted, $ pi_key); // private key encryption

$ Encrypted = base64_encode ($ encrypted); // The encrypted content usually contains special characters. if you need to encode and convert the content, pay attention to whether base64 encoding is url-safe during url transmission between networks.

Echo $ encrypted, "\ n ";

Echo "public key decrypt: \ n ";

Openssl_public_decrypt (base64_decode ($ encrypted), $ decrypted, $ pu_key); // The private key-encrypted content can be decrypted using the public key.

Echo $ decrypted, "\ n ";

Echo "--------------------------------------- \ n ";

Echo "public key encrypt: \ n ";

Openssl_public_encrypt ($ data, $ encrypted, $ pu_key); // public key encryption

$ Encrypted = base64_encode ($ encrypted );

Echo $ encrypted, "\ n ";

Echo "private key decrypt: \ n ";

Openssl_private_decrypt (base64_decode ($ encrypted), $ decrypted, $ pi_key); // private key decryption

Echo $ decrypted, "\ n ";

 

Http://www.bkjia.com/PHPjc/990990.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/990990.htmlTechArticlePHP rsa encryption and decryption methods PHP rsa encryption and decryption methods this article mainly introduces the use of PHP rsa encryption and decryption methods, this article describes how to generate a public key, private key, and PHP...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.