• pfx,cer转pem,并对通过pem文件进行签名与验签


      因为PHP无法读取.pfx文件,所以可以先转换为.pem文件然后在读取里面的数据,可以读取.cer文件,为了两方面统一,就都换成.pem然后再进行加签和验签。

    sign.php

     1 <?php
     2 
     3 define("filePath","D:\digitalCertificate\");//.pfx和.cer文件放置的地址
     4 define("pfxFileName","jsc.pfx");//.pfx文件名
     5 define("password","abc123");//.pfx文件的密码
     6 
     7 /*实现.pfx文件转为.pem文件*/
     8 $file = filePath.pfxFileName;
     9 $results = array();
    10 $worked = openssl_pkcs12_read(file_get_contents($file), $results, password);
    11 $certificateCApem =$file.'.pem';
    12 @file_put_contents($certificateCApem, $results);
    13 
    14 /*实现对传来的数据进行排序*/
    15 function dataSort($data) {
    16     $dataArr=explode(";",$data);
    17     sort($dataArr);
    18     $dataStr=implode(',',$dataArr);
    19     $TransData=str_replace(",","&",$dataStr);
    20     return  $TransData;
    21 }
    22 
    23 /*实现加签功能*/
    24 function sign($data) {
    25     $priKey = file_get_contents(filePath.pfxFileName.'.pem');
    26     $res = openssl_get_privatekey($priKey);
    27     openssl_sign($data, $sign, $res);
    28     openssl_free_key($res);
    29     $sign = base64_encode($sign);
    30     return $sign;
    31 }
    32 
    33 $data="";//用户传进的要加签的数据,中间每个参数用';'分隔开
    34 // $data="name=amdin;code=12313;pass=admin;email=admin@qq.com;id=25536"; //测试用的$data数据
    35 
    36 $dataReturn=dataSort($data).'&'.'signType="RSA"'.'&'.'sign='.sign(dataSort($data));//业务需求需要返回的数据格式,使用者可根据实际需要改变
    37 print_r($dataReturn);//生成加签后的数据

    signCheck.php

     1 <?php
     2 
     3 define("filePath","D:\digitalCertificate\");//.pfx和.cer文件放置的地址
     4 define("cerFileName","jsc.cer");//.cer文件名
     5 
     6 /*实现.cer文件转为.pem文件*/
     7 $certificateCAcer = filePath.cerFileName;
     8 $certificateCAcerContent = file_get_contents($certificateCAcer);
     9 $certificateCApem=filePath.cerFileName.'.pem';
    10 file_put_contents($certificateCApem,$certificateCAcerContent);
    11 
    12 /*实现传来的数据进行排序*/
    13 function dataSort($data) {
    14     $dataArr=explode(";",$data);
    15     sort($dataArr);
    16     $dataStr=implode(',',$dataArr);
    17     $TransData=str_replace(",","&",$dataStr);
    18     return  $TransData;
    19 }
    20 
    21 /*实现验签功能*/
    22 function verify($data, $sign)  {
    23     $pubKey = file_get_contents(filePath.cerFileName.'.pem');
    24     $res = openssl_get_publickey($pubKey);
    25     $result = (bool)openssl_verify($data, base64_decode($sign), $res);
    26     openssl_free_key($res);
    27     if($result) {
    28         return "true";
    29     }else {
    30         return "false";
    31     }
    32 }
    33 $signData='';//用户实现验签是传进的参数,不能修改为双引号,因为用户传进的参数内部含有双引号
    34 
    35 // $signData='code=12313&email=admin@qq.com&id=25536&name=amdin&pass=admin&signType="RSA"&sign=LLYTYTDzoehSCIoRLreVMYeBnlpB6ESVZ29VsvrYwhTPXJ0OzNlMs3t3L+FoqULwe9+p3mmY1YFqk8g4oOZzUpblrt0xEE6qDBakvSJDmoBsQi3CHRxcXtxiMcZO4w/JFPnj0ld6DfeNPsjg43OjLA7sgBGc3jj2eb/3KpsL/yAXziXJdrfdRKfXsIejxrsLQna7UwxaBZKeyY48ZYTJgMUX9dQemRHOe3lcJkGUJTJuKRjykwvXWQmoZJIvu/DBrQ9vJIIZOUOMDCPC3QQJrtZsxr1uNssZ6NlV/eVhKbFPEjZeCm+53RNsfirl12GCtv68eQC0CpVUEwuw4kLkNA==';//测试时sign.php中生成的加签数据
    36 
    37 $data=explode('&signType="RSA"&sign=',$signData)[0];
    38 $sign=explode('&signType="RSA"&sign=',$signData)[1];
    39 
    40 echo (verify(dataSort($data),$sign)); //返回验签结果,成功则为true,否则为false
  • 相关阅读:
    CF110A Nearly Lucky Number
    Max Sum Plus Plus HDU – 1024
    洛谷 p1003 铺地毯
    poj-1226
    Where is the Marble? UVA – 10474
    Read N Characters Given Read4
    Guess Number Higher or Lower && 九章二分法模板
    Intersection of Two Arrays II
    Reverse Vowels of a String
    Meeting Rooms
  • 原文地址:https://www.cnblogs.com/jacson/p/4719245.html
Copyright © 2020-2023  润新知