富贵资源网 Design By www.hznty.com

php rsa 加密,解密,签名,验签

由于对接第三方机构使用的是Java版本的rsa加解密方法,所有刚开始在网上搜到很多PHP版本的rsa加解密,但是对接java大多都不适用。

以下php版本是适用于对接java接口,java适用密钥再php语言使用是需要添加

-----BEGIN CERTIFICATE-----
 
-----END CERTIFICATE----- 
  

使用密钥:
加密公钥  public_key.cer

解密私钥  private_key.key

签名私钥 sign_key.key

验签公钥 verify.cer

<"D:/encryptions.cer";   
    $encryptionKey4Server = file_get_contents($encryptionKeyPath);

    $pem = chunk_split(base64_encode($encryptionKey4Server),64,"\n");//转换为pem格式的公钥
    $pem = "-----BEGIN CERTIFICATE-----\n".$pem."-----END CERTIFICATE-----\n";
    $publicKey = openssl_pkey_get_public($pem);
    return $publicKey;
  }
  
  //解密私钥
  function redPikey()
  {    
    //拼接解密私钥路径
    $decryptKeyPath="D:/decrypts.key";   
    $decryptKey4Server = file_get_contents($decryptKeyPath);

    $pem = chunk_split($decryptKey4Server,64,"\n");//转换为pem格式的私钥
    $pem = "-----BEGIN PRIVATE KEY-----\n".$pem."-----END PRIVATE KEY-----\n";
    $privateKey = openssl_pkey_get_private($pem);
    return $privateKey;
  }
  
  //签名私钥
  function redSignkey()
  {    
    //拼接签名路径
    $signKeyPath="D:/DEMO/sign.key";
    $signKey4Server = file_get_contents($signKeyPath);

    $pem = chunk_split($signKey4Server,64,"\n");//转换为pem格式的私钥
    $pem = "-----BEGIN PRIVATE KEY-----\n".$pem."-----END PRIVATE KEY-----\n";
    $signKey = openssl_pkey_get_private($pem);
    return $signKey;
  }
  
  //验签公钥
  function redVerifykey()
  {    
    //拼接验签路径
    $verifyKeyPath="D:/DEMO/verify.cer";  
    $verifyKey4Server = file_get_contents($verifyKeyPath);

    $pem = chunk_split(base64_encode($verifyKey4Server),64,"\n");//转换为pem格式的公钥
    $pem = "-----BEGIN CERTIFICATE-----\n".$pem."-----END CERTIFICATE-----\n";
    $verifyKey = openssl_pkey_get_public($pem);
    return $verifyKey;
  }
  
  //公钥加密
  function pubkeyEncrypt($source_data, $pu_key) {
    $data = "";
    $dataArray = str_split($source_data, 117);
    foreach ($dataArray as $value) {
      $encryptedTemp = ""; 
      openssl_public_encrypt($value,$encryptedTemp,$pu_key);//公钥加密
      $data .= base64_encode($encryptedTemp);
    }
    return $data;
  }
  
  //私钥解密
  function pikeyDecrypt($eccryptData,$decryptKey) {
    $decrypted = "";
    $decodeStr = base64_decode($eccryptData);
    $enArray = str_split($decodeStr, 256);

    foreach ($enArray as $va) {
      openssl_private_decrypt($va,$decryptedTemp,$decryptKey);//私钥解密
      $decrypted .= $decryptedTemp;
    }
    return $decrypted;
  }
  
 
}
"color: #0000ff">如下: $oid=base64_decode(str_replace(" ","+",$_GET[oid]));

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

富贵资源网 Design By www.hznty.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
富贵资源网 Design By www.hznty.com