PHP RSA encryption, decryption, signature, verification
Because the docking of third-party organizations using the Java version of the RSA encryption and decryption method, all just started on the internet to find a lot of PHP version of the RSA encryption, but most of the docking Java does not apply.
The following PHP version is suitable for docking Java interface, Java applicable key again PHP language use is need to add
-----BEGIN CERTIFICATE----------END CERTIFICATE-----
Use key:
Encrypting public Key Public_key.cer
Decrypt the private key Private_key.key
Signature private Key Sign_key.key
Check public key Verify.cer
<?phpclass rsautils{//Cryptographic public key function Redpukey () {///stitching encrypted public key path $encryptionKeyPath = "D:/encryptions.cer"; $encryptionKey 4Server = file_get_contents ($encryptionKeyPath); $pem = Chunk_split (Base64_encode ($encryptionKey 4Server), "\ n"), or//the public key converted to PEM format $PEM = "-----BEGIN CERTIFICATE-----\ n ". $pem." -----END CERTIFICATE-----\ n "; $publicKey = Openssl_pkey_get_public ($PEM); return $publicKey; }//Decrypt private key function Redpikey () {///Splice Decrypt private key path $decryptKeyPath = "D:/decrypts.key"; $decryptKey 4Server = file_get_contents ($decryptKeyPath); $pem = Chunk_split ($decryptKey 4server,64, "\ n"), or//convert the private key to PEM format $pem = "-----BEGIN private key-----\ n". $pem. " -----END PRIVATE KEY-----\ n "; $privateKey = Openssl_pkey_get_private ($PEM); return $privateKey; }//Signature private key function Redsignkey () {//stitching signature Path $signKeyPath = "D:/demo/sign.key"; $signKey 4Server = file_get_contents ($signKeyPath); $pem = Chunk_split ($signKey 4server,64, "\ n");//the private key converted to PEM format $PEM= "-----BEGIN PRIVATE KEY-----\ n". $pem. " -----END PRIVATE KEY-----\ n "; $signKey = Openssl_pkey_get_private ($PEM); return $signKey; }//Check public key function Redverifykey () {//stitching verification path $verifyKeyPath = "D:/demo/verify.cer"; $verifyKey 4Server = file_get_contents ($verifyKeyPath); $pem = Chunk_split (Base64_encode ($verifyKey 4Server), "\ n"), or//the public key converted to PEM format $PEM = "-----BEGIN CERTIFICATE-----\ n". $ Pem. " -----END CERTIFICATE-----\ n "; $verifyKey = Openssl_pkey_get_public ($PEM); return $verifyKey; }//Public key cryptographic function Pubkeyencrypt ($source _data, $pu _key) {$data = ""; $dataArray = Str_split ($source _data, 117); foreach ($dataArray as $value) {$encryptedTemp = ""; Openssl_public_encrypt ($value, $encryptedTemp, $pu _key);//public key encryption $data. = Base64_encode ($encryptedTemp); } return $data; }//private key decryption function Pikeydecrypt ($eccryptData, $decryptKey) {$decrypted = ""; $DECODESTR = Base64_decode ($eccryptData); $enArray = Str_split ($decodeStr, 256); foreach ($enArray as $va) {Openssl_private_decrypt ($va, $decryptedTemp, $decryptKey);//private key decryption $decrypted. = $decryp Tedtemp; } return $decrypted; }}?>
Attention:
Sometimes with Base64_encode encryption, in the form of get to other pages, with Base64_decode decryption, there is garbled.
When I encounter this problem, I wonder why some of them can be decrypted correctly, but some of them are garbled.
Later after the inspection, found that there are some Chinese characters, in the form of Get pass, the + number will be replaced by a space.
In order to prevent garbled situation, I did a step to replace, and then decrypt, sure enough, garbled problem, no longer exist!
For example, if you send an OID variable in the form of get, then replace the space with the + number first when decrypting the restore. Then the output is normal.
As follows: $oid =base64_decode (Str_replace ("", "+", $_get[oid]));