您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關基于PHP如何解決RSA密文過長加密解密越過1024的問題,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
如下所示:
<?php namespace helpers; class OpensslRSA{ //echo $private_key 私鑰; public $private_key = '-----BEGIN RSA PRIVATE KEY----- MIICXQIBAAKBgQC+L7ENzBHxKOqjuFHPFKlAt40BatVZhUAHw/G05XshpTGqm9Rv 8wG0EAbFbdo9PuB8DiXdPQLyIfaqkTgpsPjJ1Ow7WKxmYbqZN5IW/GN+TyFWP+MB 2W6IBLPWBDvAl2NJlmU8j3LCPJW1dH4zP1OitkxZMyUuUYYfPuOtO9RJ2wIDAQAB AoGAUFCbmJQBT7JTxGfmRGkZQLdC2MJg7rkS3TSmMhpm8UJtwvqjr9MTeRL7iQxn CU4wRrNC0jcds1sca9N/wDt4FCkCala+bg7mwQuPpg5QhXelfFr88ibRnP8y8LmZ 7PPNqx9c4jivhMzJrzNh4luqg6awjsig2w3+EW1/Ubb30AECQQDshTvyc5mpDgiO 4g8q1ztszszL9eCp+IjlUaN51vC3Nj1eXpjbtdSZ0JVKrDdhKcd3rEZVYzMQN/lI pyq85e/bAkEAzdmN6TF3Y1h4LouumCy6+61ChTFrl/yjw13CGApmAQHhEVyANHr7 NjoxP06eimzn7KHff/eYxd1Emf1SYA8uAQJBAN1ibFUpLRgXAZ20LNw9r+rNutXi ZJLUBlcXTjv6G0ByLYkKZGuqy7/ZhBPsFL4GnCUBBKhh/ObebaA6kH9VfmcCQGfg 0WxMOiM4EWy7sG+6ouE+ncL5HYKlSz7boYbgOHlpqVpJg6j4Jq1G0HNSCU9xhdg0 F8VL/RxcfLH41AkFoAECQQCR8NDB3BgHqyJfarKKMWQ3qrXHaLfBKExMrpQ8MDzs MlSBzFOnucufo110lSgjdRlgr8smtU2hx9gXFIqxvfWF -----END RSA PRIVATE KEY-----'; //公鑰 public $public_key = '-----BEGIN PUBLIC KEY----- MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC+L7ENzBHxKOqjuFHPFKlAt40B atVZhUAHw/G05XshpTGqm9Rv8wG0EAbFbdo9PuB8DiXdPQLyIfaqkTgpsPjJ1Ow7 WKxmYbqZN5IW/GN+TyFWP+MB2W6IBLPWBDvAl2NJlmU8j3LCPJW1dH4zP1OitkxZ MyUuUYYfPuOtO9RJ2wIDAQAB -----END PUBLIC KEY-----';
public $pi_key; public $pu_key; //判斷公鑰和私鑰是否可用 public function __construct() { $this->pi_key = openssl_pkey_get_private($this->private_key);//這個函數可用來判斷私鑰是否是可用的,可用返回資源id Resource id $this->pu_key = openssl_pkey_get_public($this->public_key);//這個函數可用來判斷公鑰是否是可用的 // print_r($this->pi_key);echo "\n"; // print_r($this->pu_key);echo "\n"; } //私鑰加密 /*public function PrivateEncrypt($data){ openssl_private_encrypt($data,$encrypted,$this->pi_key); $encrypted = $this->urlsafe_b64encode($encrypted);//加密后的內容通常含有特殊字符,需要編碼轉換下,在網絡間通過url傳輸時要注意base64編碼是否是url安全的 return $encrypted; }*/ public function PrivateEncrypt($data){ // openssl_private_encrypt($data,$encrypted,$this->pi_key); $crypto = ''; foreach (str_split($data, 117) as $chunk) { openssl_private_encrypt($chunk, $encryptData, $this->pi_key); $crypto .= $encryptData; } $encrypted = $this->urlsafe_b64encode($crypto);//加密后的內容通常含有特殊字符,需要編碼轉換下,在網絡間通過url傳輸時要注意base64編碼是否是url安全的 return $encrypted; }
//加密碼時把特殊符號替換成URL可以帶的內容 function urlsafe_b64encode($string) { $data = base64_encode($string); $data = str_replace(array('+','/','='),array('-','_',''),$data); return $data; } //解密碼時把轉換后的符號替換特殊符號 function urlsafe_b64decode($string) { $data = str_replace(array('-','_'),array('+','/'),$string); $mod4 = strlen($data) % 4; if ($mod4) { $data .= substr('====', $mod4); } return base64_decode($data); } //私鑰加密的內容通過公鑰可用解密出來 public function PublicDecrypt($encrypted){ // $encrypted = $this->urlsafe_b64decode($encrypted); $crypto = ''; foreach (str_split($this->urlsafe_b64decode($encrypted), 128) as $chunk) { openssl_public_decrypt($chunk, $decryptData, $this->pu_key); $crypto .= $decryptData; } //openssl_public_decrypt($encrypted,$decrypted,$this->pu_key);//私鑰加密的內容通過公鑰可用解密出來 return $crypto; } //公鑰加密 public function PublicEncrypt($data){ //openssl_public_encrypt($data,$encrypted,$this->pu_key);//公鑰加密 $crypto = ''; foreach (str_split($data, 117) as $chunk) { openssl_public_encrypt($chunk, $encryptData, $this->pu_key); $crypto .= $encryptData; } $encrypted = $this->urlsafe_b64encode($crypto); return $encrypted; } //私鑰解密 public function PrivateDecrypt($encrypted) { $crypto = ''; foreach (str_split($this->urlsafe_b64decode($encrypted), 128) as $chunk) { openssl_private_decrypt($chunk, $decryptData, $this->pi_key); $crypto .= $decryptData; } //$encrypted = $this->urlsafe_b64decode($encrypted); //openssl_private_decrypt($encrypted,$decrypted,$this->pi_key); return $crypto; } }
關于“基于PHP如何解決RSA密文過長加密解密越過1024的問題”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。