You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
141 lines
5.0 KiB
141 lines
5.0 KiB
<?php
|
|
// 签名类
|
|
|
|
//************************ 签名 非对称秘钥 start ***********************************
|
|
/**
|
|
* 生成签名
|
|
* @param string $str 待加密字符串
|
|
* @param string $private_key 私钥路径或者字符
|
|
* @param int $private_key_type 1--路径 2--字符串
|
|
* @param int|string $cipher_algo 签名算法 整型-- 字符串--可以通过openssl_get_cipher_methods()获得有效密码方式列表
|
|
* @date 2022-06-06
|
|
*/
|
|
function getSignData($str, $private_key, $private_key_type = 1, $algorithm = 'SHA256')
|
|
{
|
|
if ($private_key_type == 1) {
|
|
$private_key = file_get_contents($private_key);
|
|
} else if ($private_key_type == 2) {
|
|
$private_key = "-----BEGIN PRIVATE KEY-----\n" .
|
|
wordwrap($private_key, 64, "\n", true) .
|
|
"\n-----END PRIVATE KEY-----";
|
|
}
|
|
|
|
$key = openssl_get_privatekey($private_key);
|
|
|
|
//algorithm可以通过openssl_get_md_methods()获得
|
|
openssl_sign($str, $signature, $key, $algorithm);
|
|
|
|
openssl_free_key($key);
|
|
|
|
$signature = base64_encode($signature);
|
|
return $signature;
|
|
}
|
|
|
|
/**
|
|
* 验签
|
|
* @param string $data 需要验证的数据
|
|
* @param string $sign 签名
|
|
* @param string $public_key 公钥路径或者字符
|
|
* @param int $public_key_type 1--路径 2--字符串
|
|
* @param int|string $cipher_algo 签名算法 整型-- 字符串--可以通过openssl_get_cipher_methods()获得有效密码方式列表
|
|
* @date 2022-06-06
|
|
*/
|
|
function verifySignData($data, $sign, $public_key, $public_key_type = 1, $algorithm = 'SHA256')
|
|
{
|
|
if ($public_key_type == 1) {
|
|
$public_key = file_get_contents($public_key);
|
|
} else if ($public_key_type == 2) {
|
|
$public_key = "-----BEGIN PUBLIC KEY-----\n" .
|
|
wordwrap($public_key, 64, "\n", true) .
|
|
"\n-----END PUBLIC KEY-----";
|
|
}
|
|
|
|
$key = openssl_get_publickey($public_key);
|
|
|
|
$result = openssl_verify($data, base64_decode($sign), $key, $algorithm);
|
|
|
|
openssl_free_key($key);
|
|
|
|
return $result;
|
|
}
|
|
|
|
//************************ 签名 非对称秘钥 end ***********************************
|
|
|
|
//************************ 加密解密 对称秘钥 start ***********************************
|
|
/**
|
|
* AES-265-ECB加密
|
|
* @param string $str 需要加密的字符串
|
|
* @param string $key 加密秘钥
|
|
* @param string $cipher_algo 密码学方式 可以通过openssl_get_cipher_methods()获得有效密码方式列表
|
|
* @date 2022-06-08
|
|
*/
|
|
function encryptData($str, $key, $cipher_algo = 'aes-256-ecb')
|
|
{
|
|
return openssl_encrypt($str, $cipher_algo, $key);
|
|
}
|
|
|
|
/**
|
|
* AES-265-ECB解密
|
|
* @param string $str 需要解密的数据
|
|
* @param string $key 解密秘钥
|
|
* @param string $cipher_algo 密码学方式 可以通过openssl_get_cipher_methods()获得有效密码方式列表
|
|
* @date 2022-06-08
|
|
*/
|
|
function decryptData($str, $key, $cipher_algo = 'aes-256-ecb')
|
|
{
|
|
return openssl_decrypt($str, $cipher_algo, $key);
|
|
}
|
|
|
|
//************************ 加密解密 对称秘钥 end ***********************************
|
|
|
|
//************************ 加密解密 使用接收方公钥加密数据 使用接收方私钥解密数据 start ***********************************
|
|
/**
|
|
* 使用公钥加密数据
|
|
* @param string $str 需要加密的字符串
|
|
* @param string $public_key 公钥路径或者字符(接收方的公钥)
|
|
* @param int $public_key_type 1--路径 2--字符串
|
|
* @param int $padding OPENSSL_PKCS1_PADDING, OPENSSL_SSLV23_PADDING, OPENSSL_PKCS1_OAEP_PADDING, OPENSSL_NO_PADDING
|
|
* @date 2022-06-08
|
|
*/
|
|
function encryptDataByReceiveCert($str, $public_key, $public_key_type = 1, $padding = OPENSSL_PKCS1_PADDING)
|
|
{
|
|
if ($public_key_type == 1) {
|
|
$public_key = file_get_contents($public_key);
|
|
} else if ($public_key_type == 2) {
|
|
$public_key = "-----BEGIN PUBLIC KEY-----\n" .
|
|
wordwrap($public_key, 64, "\n", true) .
|
|
"\n-----END PUBLIC KEY-----";
|
|
}
|
|
|
|
//使用公钥加密数据
|
|
openssl_public_encrypt($str, $encrypt_data, $public_key, $padding);
|
|
|
|
$encrypt_data = base64_encode($encrypt_data);
|
|
|
|
return $encrypt_data;
|
|
}
|
|
|
|
/**
|
|
* 使用私钥解密数据
|
|
* @param string $str 需要解密的数据
|
|
* @param string $private_key 私钥路径或者字符(接收方的私钥)
|
|
* @param int $private_key_type 1--路径 2--字符串
|
|
* @param int $padding OPENSSL_PKCS1_PADDING, OPENSSL_SSLV23_PADDING, OPENSSL_PKCS1_OAEP_PADDING, OPENSSL_NO_PADDING
|
|
* @date 2022-06-08
|
|
*/
|
|
function decryptDataByReceiveCert($str, $private_key, $private_key_type = 1, $padding = OPENSSL_PKCS1_PADDING)
|
|
{
|
|
if ($private_key_type == 1) {
|
|
$private_key = file_get_contents($private_key);
|
|
} else if ($private_key_type == 2) {
|
|
$private_key = "-----BEGIN PRIVATE KEY-----\n" .
|
|
wordwrap($private_key, 64, "\n", true) .
|
|
"\n-----END PRIVATE KEY-----";
|
|
}
|
|
|
|
//使用私钥解密数据
|
|
openssl_private_decrypt(base64_decode($str), $decrypted, $private_key, $padding);
|
|
|
|
return $decrypted;
|
|
}
|
|
//************************ 加密解密 使用接收方公钥加密数据 使用接收方私钥解密数据 end ***********************************
|