php rsa 加密,解密,簽名,驗簽
由於對接第三方機構使用的是Java版本的rsa加解密方法,所有剛開始在網上搜到很多PHP版本的rsa加解密,但是對接java大多都不適用。
以下php版本是適用於對接java接口,java適用密鑰再php語言使用是需要添加
-----BEGIN CERTIFICATE----- -----END CERTIFICATE-----
使用密鑰:
加密公鑰 public_key.cer
解密密鑰:
加密公鑰 public_key.cer解密鑰匙簽章私鑰sign_key.key驗簽公鑰verify.cer<?php class RSAUtils{ //加密公钥 function redPukey() { //拼接加密公钥路径 $encryptionKeyPath="D:/encryptions.cer"; $encryptionKey4Server = file_get_contents($encryptionKeyPath); $pem = chunk_split(base64_encode($encryptionKey4Server),64,"\n");//转换为pem格式的公钥 $pem = "-----BEGIN CERTIFICATE-----\n".$pem."-----END CERTIFICATE-----\n"; $publicKey = openssl_pkey_get_public($pem); return $publicKey; } //解密私钥 function redPikey() { //拼接解密私钥路径 $decryptKeyPath="D:/decrypts.key"; $decryptKey4Server = file_get_contents($decryptKeyPath); $pem = chunk_split($decryptKey4Server,64,"\n");//转换为pem格式的私钥 $pem = "-----BEGIN PRIVATE KEY-----\n".$pem."-----END PRIVATE KEY-----\n"; $privateKey = openssl_pkey_get_private($pem); return $privateKey; } //签名私钥 function redSignkey() { //拼接签名路径 $signKeyPath="D:/DEMO/sign.key"; $signKey4Server = file_get_contents($signKeyPath); $pem = chunk_split($signKey4Server,64,"\n");//转换为pem格式的私钥 $pem = "-----BEGIN PRIVATE KEY-----\n".$pem."-----END PRIVATE KEY-----\n"; $signKey = openssl_pkey_get_private($pem); return $signKey; } //验签公钥 function redVerifykey() { //拼接验签路径 $verifyKeyPath="D:/DEMO/verify.cer"; $verifyKey4Server = file_get_contents($verifyKeyPath); $pem = chunk_split(base64_encode($verifyKey4Server),64,"\n");//转换为pem格式的公钥 $pem = "-----BEGIN CERTIFICATE-----\n".$pem."-----END CERTIFICATE-----\n"; $verifyKey = openssl_pkey_get_public($pem); return $verifyKey; } //公钥加密 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);//公钥加密 $data .= base64_encode($encryptedTemp); } return $data; } //私钥解密 function pikeyDecrypt($eccryptData,$decryptKey) { $decrypted = ""; $decodeStr = base64_decode($eccryptData); $enArray = str_split($decodeStr, 256); foreach ($enArray as $va) { openssl_private_decrypt($va,$decryptedTemp,$decryptKey);//私钥解密 $decrypted .= $decryptedTemp; } return $decrypted; } } ?>
感謝閱讀,希望能幫助大家,謝謝大家對本站的支持!