Php rsa encryption, decryption, signature, Signature Details, rsa details
Php rsa encryption, decryption, signature, Signature Verification
Since the Third-Party organizations are connected to the Java version of the rsa encryption and decryption method, most of the rsa encryption and decryption methods that have been found on the internet for many PHP versions are not applicable.
The following php versions are suitable for interfacing with java interfaces. For java keys, you need to add
-----BEGIN CERTIFICATE----- -----END CERTIFICATE-----
Use key:
Public Key public_key.cer
Decrypt Private Key private_key.key
Signature Private Key sign_key.key
Verification Public Key verify. cer
<? Phpclass RSAUtils {// encryption public key function redPukey () {// concatenate the encryption Public Key Path $ encryptionKeyPath = "D:/encryptions. cer "; $ encryptionKey4Server = file_get_contents ($ encryptionKeyPath); $ pem = chunk_split (base64_encode ($ encryptionKey4Server), 64," \ n "); // convert the public key to pem format $ pem = "----- begin certificate ----- \ n ". $ pem. "----- end certificate ----- \ n"; $ publicKey = openssl_pkey_get_public ($ pem); return $ publicKey;} // decrypt the private key function red Pikey () {// concatenate and decrypt the Private Key Path $ decryptKeyPath = "D:/decrypts. key "; $ decryptKey4Server = file_get_contents ($ decryptKeyPath); $ pem = chunk_split ($ decryptKey4Server, 64," \ n "); // 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 () {// concatenate the signature path $ signKeyPath = "D:/DEMO/sign. key "; $ signKey4S Erver = file_get_contents ($ signKeyPath); $ pem = chunk_split ($ signKey4Server, 64, "\ n "); // convert the private key to pem format $ pem = "----- begin private key ----- \ n ". $ pem. "----- end private key ----- \ n"; $ signKey = openssl_pkey_get_private ($ pem); return $ signKey;} // sign the public KEY function redVerifykey () {// splice the signature path $ verifyKeyPath = "D:/DEMO/verify. cer "; $ verifyKey4Server = file_get_contents ($ verifyKeyPath); $ pem = chunk_split (base64_enco De ($ verifyKey4Server), 64, "\ n"); // convert the public key to pem format $ pem = "----- begin certificate ----- \ n ". $ pem. "----- end certificate ----- \ n"; $ verifyKey = openssl_pkey_get_public ($ pem); return $ verifyKey;} // public key encryption function pubkeyEncrypt ($ source_data, $ pu_key) {$ data = ""; $ dataArray = str_split ($ source_data, 117); foreach ($ dataArray as $ value) {$ encryptedTemp = ""; openssl_public_encrypt ($ value, $ encryptedTemp, $ pu_ke Y); // 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. = $ decryptedTemp;} return $ decrypted ;}}?>
Note:
Sometimes, after base64_encode encryption, it is uploaded to other pages in the form of GET. garbled characters occur when base64_decode is used for decryption.
When I encountered this problem, I wondered why some of them could be correctly decrypted, but some were garbled?
Later, I checked and found some Chinese characters. When passed in the GET form, the plus sign will be replaced with a space.
In order to prevent garbled characters, I made a step-by-step replacement and decrypted it. As a result, the garbled problem no longer exists!
For example, if you pass an oid variable in the form of GET, when decryption and restoration are performed, replace the space with the plus sign (+). Then the output is normal.
$ Oid = base64_decode (str_replace ("", "+", $ _ GET [oid]);
Thank you for reading this article. I hope it will help you. Thank you for your support for this site!