RSA Encryption Decryption:
Openssl_pkey_get_private series functions, under Windows need to load Extension=php_openssl.dll;linux under extension=openssl.so.
Download the open source RSA key generation tool OpenSSL (usually the Linux system comes with the program), unzip to a separate 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 the PKCS8 format, and the third generates the RSA public key Rsa_public_key.pem
From the above, we can generate the corresponding public key through the private key, so we use the private key Private_key.pem on the server side, the public key is issued to the Android and iOS and other front
Openssl_pkey_get_private
Openssl_pkey_get_public
Openssl_private_encrypt
Openssl_public_decrypt
Openssl_public_encrypt
Openssl_private_decrypt
<?PHP//private key encryption, public key decryption$privateFile= "Rsa_private_key.pem";$privateKey=file_get_contents($privateFile);$pi _key= Openssl_pkey_get_private ($privateKey);//Print_r ($pi _key); echo "\ n";$data= "AASSSSASSSDDD";//Raw DataEcho"Source:".$data." \ n ";$encrypted= ""; $decrypted= ""; Openssl_private_encrypt ($data,$encrypted,$pi _key);//private key EncryptionEcho $encrypted."|||| \ n ";$encrypted=Base64_encode($encrypted);Echo $encrypted, "\ n"; $publicFile= "Rsa_public_key.pem";$publicKey=file_get_contents($publicFile);$pu _key= Openssl_pkey_get_public ($publicKey); Openssl_public_decrypt (Base64_decode($encrypted),$decrypted,$pu _key);//private key encrypted content can be decrypted by public keyEcho $decrypted, "\ n";//public key encryption, private key decryption$data= "ABCDEFG"; Openssl_public_encrypt ($data,$encrypted,$pu _key); Openssl_private_decrypt ($encrypted,$decrypted,$pi _key);Echo $decrypted;
RSA Encryption Decryption