RSA加密解密算法
阿新 • • 發佈:2019-03-24
else suffix ret 特殊 pathinfo 生成 註意 ckey exce
/** * RSA加密解密算法 * Class Rsa */ class Rsa { /** * 獲取pem格式的公鑰 * @param $public_key 公鑰文件路徑或者字符串 * @return bool|mixed|string */ public static function public_key($public_key) { try{ // 先判斷是否是文件 $suffix = pathinfo($public_key,PATHINFO_EXTENSION); if(!empty($suffix) && is_file($public_key)){ $public_key = file_get_contents($public_key); } if(false === strpos($public_key, ‘-----‘)){ $public_key = str_replace("\n", "", $public_key);$public_key = "-----BEGIN PUBLIC KEY-----\n" . chunk_split($public_key, 64, "\n") . "-----END PUBLIC KEY-----"; } }catch (\Exception $e){ $public_key = ‘‘; } return $public_key; } /** * 獲取pem格式的私鑰 * @param $private_key 私鑰文件路徑或者字符串 * @return bool|mixed|string*/ public static function private_key($private_key, $key_password=‘‘) { try{ // 先判斷是否是文件 $suffix = pathinfo($private_key,PATHINFO_EXTENSION); if(!empty($suffix) && is_file($private_key)){ $private_key = file_get_contents($private_key); } if(false === strpos($private_key, ‘-----‘)){ $private_key = str_replace("\n", "", $private_key); if(empty($key_password)){ $private_key = "-----BEGIN PRIVATE KEY-----\n" . chunk_split($private_key, 64, "\n") . "-----END PRIVATE KEY-----"; }else{ $private_key = "-----BEGIN ENCRYPTED PRIVATE KEY-----\n" . chunk_split($private_key, 64, "\n") . "-----END ENCRYPTED PRIVATE KEY-----"; } } }catch (\Exception $e){ $private_key = ‘‘; } return $private_key; } /** * RSA公鑰加密 * @param $decrypted 待加密字符串 * @param $public_key 公鑰 * @return bool|string */ public static function public_encrypt($decrypted, $public_key) { try{ $public_key = self::public_key($public_key); $publicKey = openssl_pkey_get_public($public_key); //這個函數可用來判斷公鑰是否是可用的,可用返回資源id Resource id if(!$publicKey) return false; $decrypted = str_split($decrypted, 117); $encrypted = ‘‘; foreach ($decrypted as $decrypt){ $encrypt = ‘‘; openssl_public_encrypt($decrypt, $encrypt, $publicKey);//公鑰加密 $encrypted .= $encrypt; //加密後的內容通常含有特殊字符,需要編碼轉換下,在網絡間通過url傳輸時要註意base64編碼是否是url安全的 } $encrypted = base64_encode($encrypted); openssl_free_key($publicKey); unset($decrypted, $public_key, $publicKey, $decrypt, $encrypt); }catch (\Exception $e){ $encrypted = ‘‘; } return $encrypted; } /** * RSA私鑰加密 * @param $decrypted 待加密字符串 * @param $private_key 私鑰 * @return bool|string */ public static function private_encrypt($decrypted, $private_key, $key_password=‘‘) { try { $private_key = self::private_key($private_key, $key_password); $privateKey = openssl_pkey_get_private($private_key, $key_password); //這個函數可用來判斷私鑰是否是可用的,可用返回資源id Resource id if (!$privateKey) return false; $decrypted = str_split($decrypted, 117); $encrypted = ‘‘; foreach ($decrypted as $decrypt) { $encrypt = ‘‘; openssl_private_encrypt($decrypt, $encrypt, $privateKey);//公鑰加密 $encrypted .= $encrypt; //加密後的內容通常含有特殊字符,需要編碼轉換下,在網絡間通過url傳輸時要註意base64編碼是否是url安全的 } $encrypted = base64_encode($encrypted); openssl_free_key($privateKey); unset($decrypted, $private_key, $privateKey, $decrypt, $encrypt); }catch (\Exception $e){ $encrypted = ‘‘; } return $encrypted; } /** * RSA公鑰解密 * @param $encrypted 待解密密文 * @param $public_key 公鑰 * @param string $key_password 證書密碼 * @return bool|string */ public static function public_decrypt($encrypted, $public_key) { try{ $public_key = self::public_key($public_key); $publicKey = openssl_pkey_get_public($public_key); //這個函數可用來判斷公鑰是否是可用的,可用返回資源id Resource id if(!$publicKey) return false; $encrypted = str_split(base64_decode($encrypted), 128); $decrypted = ‘‘; foreach ($encrypted as $encrypt){ $decrypt = ‘‘; openssl_public_decrypt($encrypt, $decrypt, $publicKey);//私鑰解密 $decrypted .= $decrypt; } openssl_free_key($publicKey); unset($encrypted, $public_key, $publicKey, $encrypt, $decrypt); }catch (\Exception $e){ $decrypted = ‘‘; } return $decrypted; } /** * RSA私鑰解密 * @param $encrypted 待解密密文 * @param $private_key 私鑰 * @param string $key_password 證書密碼 * @return bool|string */ public static function private_decrypt($encrypted, $private_key, $key_password=‘‘) { try{ $private_key = self::private_key($private_key, $key_password); $privateKey = openssl_pkey_get_private($private_key, $key_password); //這個函數可用來判斷私鑰是否是可用的,可用返回資源id Resource id if(!$privateKey) return false; $encrypted = str_split(base64_decode($encrypted), 128); $decrypted = ‘‘; foreach ($encrypted as $encrypt){ $decrypt = ‘‘; openssl_private_decrypt($encrypt, $decrypt, $privateKey);//私鑰解密 $decrypted .= $decrypt; } openssl_free_key($privateKey); unset($encrypted, $private_key, $privateKey, $encrypt, $decrypt); }catch (\Exception $e){ $decrypted = ‘‘; } return $decrypted; } /** * 私鑰生成簽名 * @param $string 待簽名字符串 * @param $private_key 私鑰 * @param string $key_password 證書密碼 * @return bool|string */ public static function sign($string, $private_key, $key_password=‘‘) { try{ $private_key = self::private_key($private_key, $key_password); $privateKey = openssl_pkey_get_private($private_key, $key_password); //這個函數可用來判斷私鑰是否是可用的,可用返回資源id Resource id if(!$privateKey) return false; openssl_sign($string, $sign, $privateKey); openssl_free_key($privateKey); $sign = base64_encode($sign);//最終的簽名 unset($string, $private_key, $key_password, $privateKey); }catch (\Exception $e){ $sign = ‘‘; } return $sign; } /** * 公鑰校驗簽名 * @param $string 待簽名字符串 * @param $sign 簽名 * @param $public_key 公鑰 * @return bool */ public static function verify($string, $sign, $public_key) { try{ $public_key = self::public_key($public_key); $publicKey = openssl_pkey_get_public($public_key); //這個函數可用來判斷公鑰是否是可用的,可用返回資源id Resource id if(!$publicKey) return false; $sign = base64_decode($sign);//得到的簽名 $result = openssl_verify($string, $sign, $publicKey); openssl_free_key($publicKey); unset($string, $sign, $public_key, $publicKey); }catch (\Exception $e){ $result = 0; } return $result === 1 ? true : false; } }
RSA加密解密算法