PHP RSA介面加接密
阿新 • • 發佈:2018-12-15
加密解密類 //公鑰 public function public_key() { $public_key = "-----BEGIN PUBLIC KEY----- MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC7F99EGpGmjREsoWqg++Lu+j90 qRtkxhNfP6RXJmOebeynN4qwpfKmKWm6YRXZUr1/HCRxaabpT20P23yB5ZTHOBYD fxF65CO1GtXkrZmiGZRyvxjMlvK0iKmAtWe6pbjO0J0MyHZdjVs0BEZpG1GHlXpx eKoUqwX3Ul3FhdLKpQIDAQAB -----END PUBLIC KEY-----"; return $public_key; } //私鑰 public function private_key() { $private_key = "-----BEGIN RSA PRIVATE KEY----- MIICXAIBAAKBgQC7F99EGpGmjREsoWqg++Lu+j90qRtkxhNfP6RXJmOebeynN4qw pfKmKWm6YRXZUr1/HCRxaabpT20P23yB5ZTHOBYDfxF65CO1GtXkrZmiGZRyvxjM lvK0iKmAtWe6pbjO0J0MyHZdjVs0BEZpG1GHlXpxeKoUqwX3Ul3FhdLKpQIDAQAB AoGBAJpc/+D46uM30Mu6AngAWyRBZMmkHO3wX1te7wDYs6irmW397XgB80oVMR6N q7E7Cw4s8gSA3y3wUlObDAdAnJa1Sf+VRZYHcT6qpk3tMUNF9ItEXGXfLPzXqx8g 9CM3qY4x0fYytwbyA1dcRWYMzuCleKt8W5kvFbIHyUeaAh+hAkEA/PJgCHGH0RGJ dc/htisKs2NC6+dFZzuyeRQj4SfdhROqXRcIZvTBEV7r2ScClow+c2eA1CkDhzB2 TkcI/5GjIwJBAL1aASuux/LN9z6VtU4rqc9y5Resf77yOeezZ00h4nMw3/6QPk7i fR5YurSgNCv3oljV6w0VIjWG8e893VjLu5cCQF73LIDfBuvCVsZphsda/4h8AtsI mDD647W7jwDwc6WLzZFnnVryFy5LYebEU59sBeJUALUmtNFjq3cWClXROtsCQHW+ rzWrtPhebZ3EWH7olerDJoRnJvTlCW70V4tWQ8ufm9pfde911Zn2UPubF3Yzwgd9 1nFHMAObC3zaunbKEbcCQDSk/f8s9fVZ98GIQOaJVP5WIjTAvxpH9ryZzj8oRwMW wcJcWTobW1sEpxvU85KsgUwdfrvcA+yY8yWnsu4K4X8= -----END RSA PRIVATE KEY-----"; return $private_key; } //公鑰加密 protected function publicEncrypt($public_key,$data){ $encrypted = ''; $pi_key = openssl_pkey_get_public($public_key); //最大允許加密長度為117,得分段加密 $plainData = str_split($data, 117);//生成金鑰位數 1024 bit key foreach($plainData as $chunk){ $partialEncrypted = ''; $encryptionOk = openssl_public_encrypt($chunk,$partialEncrypted,$pi_key);//公鑰加密 if($encryptionOk === false){ return false; } $encrypted .= $partialEncrypted; } $encrypted = base64_encode($encrypted);//加密後的內容通常含有特殊字元,需要編碼轉換下,在網路間通過url傳輸時要注意base64編碼是否是url安全的 return $encrypted; } //私鑰解密 protected function privateDecrypt($private_key,$data){ $decrypted = ''; $pu_key = openssl_pkey_get_private($private_key); $plainData = str_split(base64_decode($data), 128); foreach($plainData as $chunk){ $str = ''; $decryptionOk = openssl_private_decrypt($chunk,$str,$pu_key);//私鑰解密 if($decryptionOk === false){ return false; } $decrypted .= $str; } return $decrypted; } //私鑰加密 protected function privateEncrypt($private_key,$data){ $encrypted = ''; $pi_key = openssl_pkey_get_private($private_key);//這個函式可用來判斷私鑰是否是可用的,可用返回資源id Resource id //最大允許加密長度為117,得分段加密 $plainData = str_split($data, 100);//生成金鑰位數 1024 bit key foreach($plainData as $chunk){ $partialEncrypted = ''; $encryptionOk = openssl_private_encrypt($chunk,$partialEncrypted,$pi_key);//私鑰加密 if($encryptionOk === false){ return false; } $encrypted .= $partialEncrypted; } $encrypted = base64_encode($encrypted);//加密後的內容通常含有特殊字元,需要編碼轉換下,在網路間通過url傳輸時要注意base64編碼是否是url安全的 return $encrypted; } //公鑰解密 protected function publicDecrypt($public_key,$data){ $decrypted = ''; $pu_key = openssl_pkey_get_public($public_key);//這個函式可用來判斷公鑰是否是可用的 $plainData = str_split(base64_decode($data), 128);//生成金鑰位數 1024 bit key foreach($plainData as $chunk){ $str = ''; $decryptionOk = openssl_public_decrypt($chunk,$str,$pu_key);//公鑰解密 if($decryptionOk === false){ return false; } $decrypted .= $str; } return $decrypted; }
加密解密具體方法
//公鑰加密 public function actionAa(){ $data = Yii::$app->request->post(); $datas = json_encode($data); $public_key = $this->public_key(); $list = $this->publicEncrypt($public_key,$datas); p($list); } //私鑰解密 public function actionBb(){ $data = Yii::$app->request->post(); $private_key = $this->private_key(); $list = $this->privateDecrypt($private_key,$data['data']); $datas = json_decode($list,true); return $datas; } //私鑰加密 public function actionCc() { $data = Yii::$app->request->post(); $datas = json_encode($data); $private_key = $this->private_key(); $list = $this->privateEncrypt($private_key,$datas); return $list; } //公鑰解密 public function actionDd() { $data = Yii::$app->request->post(); $public_key = $this->public_key(); $list = $this->publicDecrypt($public_key,$data['data']); p($list);die; }