Phprsa encryption and decryption instance

Source: Internet
Author: User
Phprsa encryption and decryption instance 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:

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. $ Private_key = '----- begin rsa private key -----
  2. MIICXQIBAAKBgQC3 // sR2tXw0wrC2DySx8vNGlqt3Y7ldU9 + LBLI6e1KS5lfc5jl
  3. Bytes/
  4. Gqx7/IRia3zQfxGv/qEkyGOx + XALVoOlZqDwh76o2n1vP1D + tD3amHsK7QIDAQAB
  5. AoGBAKH14bMitESqD4PYwODWmy7rrrvyFPEnJJTECLjvKB7IkrVxVDkp1XiJnGKH
  6. 2h5syHQ5qslPSGYJ1M/XkDnGINwaLVHVD3BoKKgKg1bZn7ao5pXT + herqxaVwWs6
  7. Ga63yVSIC8jcODxiuvxJnUMQRLaqoF6aUb/2VWc2T5MDmxLhAkEA3pwGpvXgLiWL
  8. 3366qlyzlrlrbfrurn4cyl4uyaakokkavzly04glle8ycgoc2dzl4eil4l/+ x/gaq
  9. DeJU/cHLRQJBANOZY0mEoVkwhU4bScSdnfM6usQowYBEwHYYh/OTv1a3SqcCE1f +
  10. Bytes
  11. Oiq + Y8SgCCs73qixrU1YpJy9yKA/meG9smsl4Oh9IOIGI + zUygh9YdSmEq0CQQC2
  12. 4G3IP2G3lNDRdZIm5NZ7PfnmyRabxk/UgVUWdk47IwTZHFkdhxKfC8QepUhBsAHL
  13. Bytes
  14. YI9lblCsyfpl25l7l5zmJrAHn45zAiOoBrWqpM5edu7c
  15. ----- End rsa private key -----';
  16. $ Public_key = '----- begin public key -----
  17. MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC3 // sR2tXw0wrC2DySx8vNGlqt
  18. 3Y7ldU9 + LBLI6e1KS5lfc5jlTGF7KBTSkCHBM3ouEHWqp1ZJ85iJe59aF5gIB2kl
  19. Bd6h4wrbbHA2XE1sq21ykja/Gqx7/IRia3zQfxGv/qEkyGOx + XALVoOlZqDwh76o
  20. 2n1vP1D + tD3amHsK7QIDAQAB
  21. ----- 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"; echo"
";
Print_r ($ pu_key); echo "\ n"; echo"
";
Echo "";

$ Data = 'php ras encryption algorithmic ';
$ Encrypted = "";
$ Decrypted = "";

Echo "encrypted source data:". $ data. "\ n"; echo"
";
Echo "private key encrypt: \ n"; echo"
";
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 'private key encrypted: '. $ encrypted. "\ n"; echo"
"; Echo"
";

Echo "public key decrypt: \ n"; echo"
";
Openssl_public_decrypt (base64_decode ($ encrypted), $ decrypted, $ pu_key); // The private key-encrypted content can be decrypted using the public key.
Echo 'public key decrypted: '. $ decrypted. "\ n"; echo"
";
Echo "";

Echo "public key encrypt: \ n"; echo"
";
Openssl_public_encrypt ($ data, $ encrypted, $ pu_key); // public key encryption
$ Encrypted = base64_encode ($ encrypted );
Echo $ encrypted, "\ n"; echo"
";

Echo "private key decrypt: \ n"; echo"
";
Openssl_private_decrypt (base64_decode ($ encrypted), $ decrypted, $ pi_key); // private key decryption
Echo $ decrypted, "\ n"; echo"
";

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.