This article illustrates the PHP implementation of SSL encryption and decryption, authentication and signature methods. Share with you for your reference, as follows:
1 Sign Signature code:
function sign($data) {
// Read the private key file
$priKey = file_get_contents('key/rsa_private_key.pem');
The openSSL key must be a private key that has not been converted by PKCS8
$res = openssl_get_privatekey($priKey);
// Call the openSSL built-in signature method to generate the signature $sign
openssl_sign($data.$sign.$res);
// Release resources
openssl_free_key($res);
return $sign;
}
Copy the code
2 verify code:
function verify($data.$sign) {
// Read alipay public key file
$pubKey = file_get_contents('key/alipay_public_key.pem');
// Convert the key to openSSL format
$res = openssl_get_publickey($pubKey);
// Call openSSL built-in method check, return bool
$result = (bool)openssl_verify($data.$sign.$res);
// Release resources
openssl_free_key($res);
return $result;
}
Copy the code
3. Decrypt the code
function decrypt($content) {
// Read the merchant private key
$priKey = file_get_contents('key/rsa_private_key.pem');
The openSSL key must be a private key that has not been converted by PKCS8
$res = openssl_get_privatekey($priKey);
// Declare a plaintext string variable
$result = ' ';
// Loop decryption at 128 bits
for($i = 0; $i < strlen($content) /128; $i{+ +)$data = substr($content.$i * 128.128);
// A string fragment of length 128 is decrypted by the private key and returns the plaintext parsed by $decrypt
openssl_private_decrypt($data.$decrypt.$res);
// Plaintext fragment stitching
$result. =$decrypt;
}
// Release resources
openssl_free_key($res);
// Return plaintext
return $result;
}
Copy the code
The above content hopes to help you, more PHP factory PDF, PHP advanced architecture video materials, PHP wonderful good article can be searched on wechat: PHP open source community
2021 Jinsanyin four big factory interview real questions collection, must see!
Four years of PHP technical articles collation collection – PHP framework
A collection of four years’ worth of PHP technical articles – Microservices Architecture
Distributed Architecture is a four-year collection of PHP technical articles
Four years of PHP technical essays – High Concurrency scenarios
Four years of elite PHP technical article collation collection – database