Lavael + PHP和JAVA对接接口,RSA+DES加解密和验签的总结

/**
 * @param string/array      $data       [待加密的字符串或者是数组]
 * @uses rsa公钥加密
 */
private function rsaPublicEncrypt($data = '')
{
   if (is_array($data)) {
      $formatData = json_encode($data, JSON_UNESCAPED_UNICODE);
   } else {
      $formatData = $data;
   }
   //三方rsa公钥文件路径
   $publicKeyPath = $this->config['rsaKey'] . 'tongchengjieqian.pub.key';
   $keyContent    = @file_get_contents($publicKeyPath);
   $formatKey     = $this->rsaPubKey($keyContent);
   $publicKey     = openssl_pkey_get_public($formatKey);
   $publicLength  = openssl_pkey_get_details($publicKey)['bits'];
   $encrypted     = '';
   $part_len      = $publicLength / 8 - 11;
   $parts         = str_split($formatData, $part_len);
   foreach ($parts as $part) {
      $encrypted_temp = '';
      openssl_public_encrypt($part, $encrypted_temp, $publicKey);
      $encrypted .= $encrypted_temp;
   }
   return $this->dataBase64Encode($encrypted);
}
/**
 * @param $rsaPubKeyStr
 * @return string
 * @uses rsa公钥处理
 */
private function rsaPubKey($rsaPubKeyStr)
{
   $base64 = str_replace(array('-', '_'), array('+', '/'), $rsaPubKeyStr);
   $strKey = (wordwrap($base64, 64, PHP_EOL, true)) . PHP_EOL;
   return "-----BEGIN PUBLIC KEY-----" . PHP_EOL . $strKey . "-----END PUBLIC KEY-----" . PHP_EOL;
}
/**
 * @param $data
 * @return string|string[]
 * @uses urlBase64加码处理
 */
private function dataBase64Encode($data)
{
   return str_replace(array('+', '/', '='), array('-', '_', ''), base64_encode($data));
}
/**
 * @param json $data [转换为json后的待签名数据]
 * @uses 数据加签
 */
private function rsaSign($data)
{
   //己方rsa私钥文件路径
   $privateKeyPath = $this->config['rsaKey'] . 'ryt.pri.key';
   $privateContent = @file_get_contents($privateKeyPath);
   $privateKey     = openssl_pkey_get_private($privateContent);
   openssl_sign($data, $sign, $privateKey, self::RSA_ALGORITHM_SIGN_TYPE);
   return $this->dataBase64Encode($sign);
}
/**
 * @param json $decryptData [同步返回解密后的json数据]
 * @param string $signature [同步返回的签名]
 * @return int     0 || 1       [1:验签成功 0:验签失败]
 * @uses 数据验签
 */
private function verifySign($decryptData, $signature)
{
   //三方rsa公钥文件路径
   $publicKeyPath = $this->config['rsaKey'] . 'tongchengjieqian.pub.key';
   $keyContent    = @file_get_contents($publicKeyPath);
   $formatKey     = $this->rsaPubKey($keyContent);
   $publicKey     = openssl_pkey_get_public($formatKey);
   $sign          = $this->dataBase64Decode($signature);
   $result        = openssl_verify($decryptData, $sign, $publicKey, self::RSA_ALGORITHM_SIGN_TYPE);
   return $result;
}
/**
 * @param $data
 * @return false|string
 * @uses urlBase64解码处理
 */
private function dataBase64Decode($data)
{
   $base_64 = str_replace(array('-', '_'), array('+', '/'), $data);
   return base64_decode($base_64);
}

目录

随机生成流水号

/**
 * @return string   $uniqid     [唯一流水号]
 * @uses 生成请求流水号
 */
private function generateNumber()
{
   $uniqid = substr(uniqid(), 5, 8) . '-' . rand(1000, 9999) . '-' . substr(uniqid(), 9, 4) . '-' . rand(1000, 9999) . '-' . $this->getRandKey(2);
   return $uniqid;
}

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值