RSA encryption and decryption (PHP Demo)

Source: Internet
Author: User
Tags begin rsa private key openssl rsa

1 $private _key=-----BEGIN RSA PRIVATE KEY-----2 Miicxqibaakbgqdpoodvtnsztgyb//p+g/ob36jb3jzwzs2qovojpy/rrtjwlvcq3 PB2M1NZDQNPTFSG8ZBL7UPW3M81LR7NRRN6TY7OM8TBOOSRGY6U0XWBGDRSTFFVW4 Pzz1hehiq6wb8za8cuccyvuqmbrp7hojo4aa9t0rivz/howmesvjnavbmwidaqab5 aogbaoehsaiids6nkdp08r1rsxjhli9i92zawnlkdcybkw4rknfbenszj2oexnkv6 A9vmxonsu1nlcajmrh/85zasds4l+zx8iz18uwxajcppfmd7ng4fd55713lszhua7 dqixk06w2mi0ytwef4cqqmct2/bwchbxzilz9o0q70cf2brpakea/3nthrqnxbf08 Krvrrtw4c9y76pyeesemkuf8zkqu6v1qsb/v3azsigpth+vuf0oamojogx1atruk9 Dae9uq5efqjbaoohcxth7vgm5ujlyjei85jgp2bnhxmnahn6n1q44hs1wbvicujhTen SEAHHVT6HSF7/NXNGOTJXVE0JIT5GLVCX28CQCA1JASKDKG10R9J/RUAK4DIIGP2 One 29egr+zxjfmh2ia71h5mdnchaa1o6za8ivbem4doya4zyzlohdza04wwvfucqqdy A 9+cjvvq6smpyn+e3rrmrwb6iyuf6kkxbxi5gx2uykqga+e/kkis7wqlnbdij7myw - F7MJCVPDMG4PZPA8CPM3AKAFRUXYKLXLUSKBRDZSDCYCUZP/Y3QL/QWXOQCA5BRJ - PJ+COFEWD/JZQD3DRFJDGVCCFMTFEAVMXWXCNJAZU2CW the-----END RSA PRIVATE KEY-----'; -  - $public _key=-----BEGIN Public KEY----- - MIGFMA0GCSQGSIB3DQEBAQUAA4GNADCBIQKBGQDPOODVTNSZTGYB//P+G/OB36JB + 3jzwzs2qovojpy/rrtjwlvcqpb2m1nzdqnptfsg8zbl7upw3m81lr7nrrn6ty7om - 8tboosrgy6u0xwbgdrstffvwpzz1hehiq6wb8za8cuccyvuqmbrp7hojo4aa9t0r + Ivz/howmesvjnavbmwidaqab A-----END Public KEY-----'; at  - //echo $private _key; - $pi _key= Openssl_pkey_get_private ($private _key);//This function can be used to determine if the private key is available, and can be used to return the resource ID Resource ID - $pu _key= Openssl_pkey_get_public ($public _key);//This function can be used to determine if the public key is available - Print_r($pi _key);Echo"\ n"; - Print_r($pu _key);Echo"\ n"; in  -  to $data= "123";//Raw Data + $encrypted= ""; - $decrypted= ""; the  * Echo"Source data:",$data, "\ n"; $ Panax Notoginseng Echo"Private Key encrypt:\n"; -  theOpenssl_private_encrypt ($data,$encrypted,$pi _key);//private key Encryption + $encrypted=Base64_encode($encrypted);//The encrypted content usually contains special characters that need to be encoded for conversion and to be aware that the Base64 encoding is URL-safe when transmitted over a network through a URL. A Echo $encrypted, "\ n"; the  + Echo"Public Key decrypt:\n"; -  $Openssl_public_decrypt (Base64_decode($encrypted),$decrypted,$pu _key);//private key encrypted content can be decrypted by public key $ Echo $decrypted, "\ n"; -  - Echo"---------------------------------------\ n"; the Echo"Public Key encrypt:\n"; - WuyiOpenssl_public_encrypt ($data,$encrypted,$pu _key);//Public Key Cryptography the $encrypted=Base64_encode($encrypted); - Echo $encrypted, "\ n"; Wu  - Echo"Private Key decrypt:\n"; AboutOpenssl_private_decrypt (Base64_decode($encrypted),$decrypted,$pi _key);//private Key Decryption $ Echo $decrypted, "\ n";

The key is generated using the OpenSSL tool, which commands:

1. Generating the RSA private key
OpenSSL genrsa-out Rsaprivatekey.pem 1024
2. Generate the corresponding public key
OpenSSL rsa-in rsaprivatekey.pem-pubout-out Rsapublickey.pem
3. Convert the RSA private key to PKCS8 format,
OpenSSL pkcs8-topk8-inform pem-in rsaprivatekey.pem-outform pem-nocrypt-out
Rsaprivatepkcs8.pem

RSA encryption and decryption (PHP Demo)

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.