php rsa加密实例,关于PHP语言的RSA加密实例讲解
本文主要向大家介紹了關于PHP語言的RSA加密實例講解,通過具體的內容向大家展示,希望對大家學習php語言有所幫助。
這幾天做了一些接口的對接需要用到RSA加密,百度一番發現原來有個openssl_public_encrypt函數,簡單看了一下,恩,開干。
坑!
1.確認是否開啟ssl
2.解決總是返回false
2.1檢查你的加密字符串是否超過117個字節
2.2檢查公鑰的縮進(這個貌似很嚴格)
2.3還是不行的話請用openssl_pkey_get_public($public_key)查看是否有返回resource ,如果沒有請更換你的php版本。
我在PHP5.5.7是不行,更換到了5.6.25解決了。最后附上一個RSA類(網上找的,用于加密超過117個字節的字符,希望
能幫到你)
----------------------------------------------------華麗分割------------------------------------------------------
if (! function_exists('url_safe_base64_encode')) {
function url_safe_base64_encode ($data) {
return str_replace(array('+','/', '='),array('-','_', ''), base64_encode($data));
}
}
if (! function_exists('url_safe_base64_decode')) {
function url_safe_base64_decode ($data) {
$base_64 = str_replace(array('-','_'),array('+','/'), $data);
return base64_decode($base_64);
}
}
class XRsa
{
const CHAR_SET = "UTF-8";
const BASE_64_FORMAT = "UrlSafeNoPadding";
const RSA_ALGORITHM_KEY_TYPE = OPENSSL_KEYTYPE_RSA;
const RSA_ALGORITHM_SIGN = OPENSSL_ALGO_SHA256;
protected $public_key;
protected $private_key;
protected $key_len;
public function __construct($pub_key, $pri_key = null)
{
$this->public_key = $pub_key;
$this->private_key = $pri_key;
$pub_id = openssl_get_publickey($this->public_key);
$this->key_len = openssl_pkey_get_details($pub_id)['bits'];
}
/*
* 創建密鑰對
*/
public static function createKeys($key_size = 2048)
{
$config = array(
"private_key_bits" => $key_size,
"private_key_type" => self::RSA_ALGORITHM_KEY_TYPE,
);
$res = openssl_pkey_new($config);
openssl_pkey_export($res, $private_key);
$public_key_detail = openssl_pkey_get_details($res);
$public_key = $public_key_detail["key"];
return [
"public_key" => $public_key,
"private_key" => $private_key,
];
}
/*
* 公鑰加密
*/
public function publicEncrypt($data)
{
$encrypted = '';
$part_len = $this->key_len / 8 - 11;
$parts = str_split($data, $part_len);
foreach ($parts as $part) {
$encrypted_temp = '';
openssl_public_encrypt($part, $encrypted_temp, $this->public_key);
$encrypted .= $encrypted_temp;
}
return url_safe_base64_encode($encrypted);
}
/*
* 私鑰解密
*/
public function privateDecrypt($encrypted)
{
$decrypted = "";
$part_len = $this->key_len / 8;
$base64_decoded = url_safe_base64_decode($encrypted);
$parts = str_split($base64_decoded, $part_len);
foreach ($parts as $part) {
$decrypted_temp = '';
openssl_private_decrypt($part, $decrypted_temp,$this->private_key);
$decrypted .= $decrypted_temp;
}
return $decrypted;
}
/*
* 私鑰加密
*/
public function privateEncrypt($data)
{
$encrypted = '';
$part_len = $this->key_len / 8 - 11;
$parts = str_split($data, $part_len);
foreach ($parts as $part) {
$encrypted_temp = '';
openssl_private_encrypt($part, $encrypted_temp, $this->private_key);
$encrypted .= $encrypted_temp;
}
return url_safe_base64_encode($encrypted);
}
/*
* 公鑰解密
*/
public function publicDecrypt($encrypted)
{
$decrypted = "";
$part_len = $this->key_len / 8;
$base64_decoded = url_safe_base64_decode($encrypted);
$parts = str_split($base64_decoded, $part_len);
foreach ($parts as $part) {
$decrypted_temp = '';
openssl_public_decrypt($part, $decrypted_temp,$this->public_key);
$decrypted .= $decrypted_temp;
}
return $decrypted;
}
/*
* 數據加簽
*/
public function sign($data)
{
openssl_sign($data, $sign, $this->private_key, self::RSA_ALGORITHM_SIGN);
return url_safe_base64_encode($sign);
}
/*
* 數據簽名驗證
*/
public function verify($data, $sign)
{
$pub_id = openssl_get_publickey($this->public_key);
$res = openssl_verify($data, url_safe_base64_decode($sign), $pub_id, self::RSA_ALGORITHM_SIGN);
return $res;
}
}
?>
本文由職坐標整理并發布,希望對同學們有所幫助。了解更多詳情請關注職坐標編程語言PHP頻道!
總結
以上是生活随笔為你收集整理的php rsa加密实例,关于PHP语言的RSA加密实例讲解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python2 python3 impo
- 下一篇: 用js写出水仙花数,JS 水仙花数