zoukankan      html  css  js  c++  java
  • PHP生成公私钥,签名和验签

    数字签名是什么?

    What is a Digital Signature?

    RSA签名与验签

     1 /**
     2  * 签名算法名称            标准签名算法名称     备注
     3  *  RSA2               SHA256WithRSA       强制要求 RSA 密钥的长度至少为 2048
     4  *  RSA                   SHA1WithRSA           对 RSA 密钥的长度不限制,(推荐使用 2048 位以上)
     5  */
     6 
     7 
     8 /**
     9  * 生成公私钥
    10  */
    11 $config = array(
    12     "digest_alg" => "sha512",//加密模式 sha256 sha512 md5
    13     "private_key_bits" => 2048,//指定应该使用多少位来生成私钥  512 1024  2048  4096等
    14     "private_key_type" => OPENSSL_KEYTYPE_RSA,////选择在创建CSR时应该使用哪些扩展。可选值有 OPENSSL_KEYTYPE_DSA, OPENSSL_KEYTYPE_DH, OPENSSL_KEYTYPE_RSA 或 OPENSSL_KEYTYPE_EC. 默认值是 OPENSSL_KEYTYPE_RSA.
    15 );
    16 //创建密钥对
    17 $res = openssl_pkey_new($config);
    18 //生成私钥
    19 openssl_pkey_export($res, $privKey);
    20 //生成公钥
    21 $pubKey = openssl_pkey_get_details($res);
    22 $pubKey = $pubKey["key"];
    23 
    24 // print_r($privKey);
    25 // print_r($pubKey);
    26 //输出公私钥文件
    27 // file_put_contents('private.key',$privKey);
    28 // file_put_contents('public.key',$pubKey);
    29 
    30 
    31 
    32 /**
    33  * 公钥加密私钥解密
    34  */
    35 $data = '公钥加密内容';
    36 //公钥加密
    37 openssl_public_encrypt($data, $encrypted, $pubKey);
    38 //私钥解密
    39 openssl_private_decrypt($encrypted, $decrypted, $privKey);
    40 // echo $decrypted;
    41 
    42 
    43 
    44 /**
    45  * 私钥签名公钥验签
    46  */
    47 $priStr = '私钥签名内容';
    48 //私钥签名
    49 $signature  = "";
    50 openssl_sign($priStr, $signature, $privKey, OPENSSL_ALGO_SHA256);
    51 $signature = base64_encode($signature);
    52 // var_dump($signature);
    53 //公钥验签
    54 /**如果签名正确返回 1, 签名错误返回 0, 内部发生错误则返回-1. */
    55 $result = (bool)openssl_verify($priStr, base64_decode($signature), $pubKey,OPENSSL_ALGO_SHA256);
    56 var_dump($result);
    57 // var_dump($signature);
    58 
    59 
    60 
    61 /**
    62  * 封装签名function
    63  */
    64 /**私钥签名 */
    65 function getRSA2Sign($paramsArr,$privateKey) {
    66     uksort($paramsArr, "strcmp");
    67     $Arr = [];
    68     foreach ($paramsArr as $key => $value) {
    69         $Arr[] = $key . "=" . rawurlencode($value);
    70     }
    71     $str = implode("&", $Arr);
    72     $signature  = "";
    73     openssl_sign($str, $signature, $privateKey, OPENSSL_ALGO_SHA256);
    74     return base64_encode($signature);
    75 }
    76 
    77 /**公钥验签 */
    78 function checkRSA2($checkArr,$sign,$publicKey){
    79     ksort($checkArr);
    80     $Arr = [];
    81     foreach ($checkArr as $key => $value) {
    82         $Arr[] = $key . '=' . rawurlencode($value);
    83     }
    84     $str = implode('&', $Arr);
    85     $rsaPubKeyStr = chunk_split($publicKey, 64, "
    ");
    86     $pubKey = openssl_pkey_get_public($rsaPubKeyStr);
    87     $result = (bool)openssl_verify($str, base64_decode($sign), $pubKey,OPENSSL_ALGO_SHA256);
    88     openssl_free_key($pubKey);
    89     /**如果签名正确返回 1, 签名错误返回 0, 内部发生错误则返回-1. */
    90     return $result;
    91 }
     1 /**其他常用加密 ******************************************************************************************/
     2 
     3     /**
     4      * @params 加密参数
     5      * @secrent 秘钥
     6      * @signType 加密类型 MD5
     7      */
     8     function signSort($params,$secrent,$signType){
     9         ksort($params);
    10         $sortStr = urldecode(http_build_query($params)).$secrent;
    11         return $signType($sortStr);
    12     }
    13 
    14     /**
    15      * hash_hmac — 使用 HMAC 方法生成带有密钥的哈希值
    16      * hash_hmac ( string $algo , string $data , string $key [, bool $raw_output = FALSE ] ) : string
    17      * 
    18      * algo
    19      * 要使用的哈希算法名称,例如:"md5","sha256","haval160,4" 等。 如何获取受支持的算法清单,请参见 hash_hmac_algos() 函数。
    20      * 
    21      * data
    22      * 要进行哈希运算的消息。
    23      * 
    24      * key
    25      * 使用 HMAC 生成信息摘要时所使用的密钥。
    26      * 
    27      * raw_output
    28      * 设置为 TRUE 输出原始二进制数据, 设置为 FALSE 输出小写 16 进制字符串。
    29      */
    30     function hmac($params, $secret){
    31         ksort($params);
    32         $my_sign = hash_hmac("sha1", urlencode(http_build_query($params)), $secret,true);
    33         $my_sign = base64_encode($my_sign);
    34         return $my_sign;
    35     }
  • 相关阅读:
    Educational Codeforces Round 10 C. Foe Pairs 水题
    Educational Codeforces Round 10 B. z-sort 构造
    CDOJ 1048 Bob's vector 三分
    Educational Codeforces Round 10 A. Gabriel and Caterpillar 模拟
    第14届电子科大初赛民间盗版部分题目题解
    HDU 5654 xiaoxin and his watermelon candy 离线树状数组 区间不同数的个数
    HDU 5653 Bomber Man wants to bomb an Array. dp
    HDU 5652 India and China Origins 二分+并查集
    HDU 5651 xiaoxin juju needs help 数学
    HDU 5650 so easy 数学
  • 原文地址:https://www.cnblogs.com/sener/p/13921649.html
Copyright © 2011-2022 走看看