PHP RSA加签的实现过程

一、得到私钥文件mycert.key

2.从pfx提取密钥信息,并转换为key格式(pfx使用pkcs12模式补足)

  (1)提取密钥对

       openssl pkcs12 -in 1.pfx -nocerts -nodes -out 1.key

       //如果pfx证书已加密,会提示输入密码。如果cer证书没有安装,则密码没法验证

  (2)从密钥对提取私钥

       openssl rsa -in  1.key -out 1_pri.key

  (3)从密钥对提取公钥

       openssl rsa -in 1.key -pubout -out 1_pub.key

  (4)因为RSA算法使用的是pkcs8模式补足,需要对提取的私钥进一步处理

       openssl pkcs8 -topk8 -inform PEM -in 1_pri.key -outform PEM -nocrypt

       复制窗口中生成的密钥,保存为1_pri_pkcs8.key

  (5)得到密钥对1_pri_pkcs8.key和1_pub.key

 

二、PHP加签

        $timestr=$this->byte2Str($this->toBE($timestamp));
        $hash=strtoupper(sha1($data.$timestr));
        $certfile = "d:/tmp/mycert.key";
        $res = openssl_pkey_get_private(file_get_contents($certfile), '');
        openssl_sign($hash, $encrypted, $res);
        openssl_free_key($res);
        $encrypted=strtoupper(bin2hex($encrypted));
        return $encrypted;

 

http://blog.csdn.net/problem2050/article/details/50528704

posted on 2017-01-04 16:17  xihong  阅读(1156)  评论(0编辑  收藏  举报

导航