Path: blob/master/bitget-php-sdk-api/src/internal/RsaUtil.php
518 views
<?php12namespace bitget\internal;345/**6* rsa加密类7* Class Rsa8*/9class RsaUtil10{1112const CHAR_SET = "UTF-8";13const BASE_64_FORMAT = "UrlSafeNoPadding";14const RSA_ALGORITHM_KEY_TYPE = OPENSSL_KEYTYPE_RSA;15const RSA_ALGORITHM_SIGN = OPENSSL_ALGO_SHA256;1617protected $public_key;18protected $private_key;19protected $key_len;2021public function __construct($pub_key = '', $pri_key = null)22{23if ($pub_key) {24$this->public_key = $pub_key;25$pub_id = openssl_pkey_get_public($this->public_key);26$this->key_len = openssl_pkey_get_details($pub_id)['bits'];27}28if ($pri_key) {29$this->private_key = $pri_key;30$pri_id = openssl_pkey_get_private($this->private_key);31print_r($pri_id);3233// print_r("=1=".$pri_key."\n");34// print_r("=2=".$this->private_key."\n");35// print_r("=3=".$pri_id."\n");3637// $this->key_len = openssl_pkey_get_details($pri_id)['bits'];38}39}4041/*42* 创建密钥对43*/44public static function createKeys($key_size = 1024)45{46$config = array(47"private_key_bits" => $key_size,48"private_key_type" => self::RSA_ALGORITHM_KEY_TYPE,49);50$res = openssl_pkey_new($config);51openssl_pkey_export($res, $private_key);52$public_key_detail = openssl_pkey_get_details($res);53$public_key = $public_key_detail["key"];5455return array(56"public_key" => $public_key,57"private_key" => $private_key,58);59}6061/*62* 公钥加密63*/64public function publicEncrypt($data)65{66$encrypted = '';67$part_len = $this->key_len / 8 - 11;68$parts = str_split($data, $part_len);6970foreach ($parts as $part) {71$encrypted_temp = '';72openssl_public_encrypt($part, $encrypted_temp, $this->public_key);73$encrypted .= $encrypted_temp;74}7576return base64_encode($encrypted);77}7879/*80* 私钥解密81*/82public function privateDecrypt($encrypted)83{84$decrypted = "";85$part_len = $this->key_len / 8;86//url 中的get传值默认会吧+号过滤成' ',替换回来就好了87str_replace('% ', '+', $encrypted);88echo $encrypted;89$base64_decoded = base64_decode($encrypted);90$parts = str_split($base64_decoded, $part_len);91foreach ($parts as $part) {92$decrypted_temp = '';93openssl_private_decrypt($part, $decrypted_temp, $this->private_key);94$decrypted .= $decrypted_temp;95}96return $decrypted;97}9899/*100* 私钥加密101*/102public function privateEncrypt($data)103{104$encrypted = '';105$part_len = $this->key_len / 8 - 11;106$parts = str_split($data, $part_len);107108foreach ($parts as $part) {109$encrypted_temp = '';110openssl_private_encrypt($part, $encrypted_temp, $this->private_key);111$encrypted .= $encrypted_temp;112}113return base64_encode($encrypted);114}115116/*117* 公钥解密118*/119public function publicDecrypt($encrypted)120{121$decrypted = "";122$part_len = $this->key_len / 8;123$base64_decoded = base64_decode($encrypted);124$parts = str_split($base64_decoded, $part_len);125126foreach ($parts as $part) {127$decrypted_temp = '';128openssl_public_decrypt($part, $decrypted_temp, $this->public_key);129$decrypted .= $decrypted_temp;130}131return $decrypted;132}133134/*135* 数据加签136*/137public function sign($data)138{139// print_r("hehe ".$this->private_key);140openssl_sign($data, $sign, $this->private_key, self::RSA_ALGORITHM_SIGN);141return base64_encode($sign);142}143144/*145* 数据签名验证146*/147public function verify($data, $sign)148{149$pub_id = openssl_get_publickey($this->public_key);150$res = openssl_verify($data, base64_decode($sign), $pub_id, self::RSA_ALGORITHM_SIGN);151return $res;152}153154}155156157