日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php 加密cer_php 生成RSA非对称加密用的证书-cer-pfx文件

發布時間:2024/1/23 php 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php 加密cer_php 生成RSA非对称加密用的证书-cer-pfx文件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

首先,關于RSA的加密介紹文章,就不多說了。直接看這個網頁吧(作者寫的計算機科普文章是極好的)

http://www.ruanyifeng.com/blog/2013/06/rsa_algorithm_part_one.html

我要實現的功能就是通過這對公鑰和密鑰,實現客戶端軟件對密碼進行加密(采用公鑰),然后網站端(php)采用密鑰進行解密驗證。

開始吧。

php服務端利用內置函數生成公鑰和密鑰。記得這需要php服務器安裝了openssl擴展。先生成cer文件和pfx文件。cer文件相當于公鑰(可以發給客戶端),pfx是密鑰(必須嚴格保存于服務器端不能泄露)。

$dn = array(

"countryName" => 'zh', //所在國家名稱

"stateOrProvinceName" => 'GuangDong', //所在省份名稱

"localityName" => 'ShenZhen', //所在城市名稱

"organizationName" => 'baibai', //注冊人姓名

"organizationalUnitName" => 'company', //組織名稱

"commonName" => 'bbb', //公共名稱

"emailAddress" => '123@.qq.com' //郵箱

);

$privkeypass = 'cf'; //私鑰密碼

$numberofdays = 3650; //有效時長

$cerpath = "./test.cer"; //生成證書路徑

$pfxpath = "./test.pfx"; //密鑰文件路徑//生成證書

$privkey = openssl_pkey_new();

$csr = openssl_csr_new($dn, $privkey);

$sscert = openssl_csr_sign($csr, null, $privkey, $numberofdays);

openssl_x509_export_to_file($sscert, $cerpath); //導出證書到文件

//openssl_pkcs12_export_to_file($sscert, $pfxpath, $privkey, $privkeypass); //生成密鑰文件

openssl_pkey_export_to_file($privkey, $pfxpath); //生成密鑰文件

以下是是php端進行簡單測試的代碼:

/*

//私鑰加密

$cer_key = file_get_contents($pfxpath); //獲取密鑰內容

openssl_pkcs12_read($cer_key, $certs, $privkeypass);

openssl_sign($data, $signMsg, $certs['pkey'],OPENSSL_ALGO_SHA1); //注冊生成加密信息

$signMsg = base64_encode($signMsg); //base64轉碼加密信息

//echo $signMsg;

//公鑰解密

$cer_key = file_get_contents($cerpath); //獲取證書內容

$unsignMsg=base64_decode($signMsg);//base64解碼加密信息

$cer = openssl_x509_read($cer_key); //讀取公鑰

$res = openssl_verify($data, $unsignMsg, $cer); //驗證

echo $res; //輸出驗證結果,1:驗證成功,0:驗證失敗

*/

$data = "123456";

$crypted = "";

$key = file_get_contents($cerpath);

//公鑰加密

openssl_public_encrypt($data, $crypted, $key);

echo base64_encode($crypted)."
";

//echo $crypted."
";

//私鑰解密

$decrypted = "";

$s = file_get_contents($pfxpath);

//echo "
$s
";

$key2 = openssl_pkey_get_private(file_get_contents($pfxpath));

if(openssl_private_decrypt($crypted, $decrypted, $key2)){

echo $decrypted;

}

else{

echo "failed";

}

客戶端通過加載cer文件,將要加密的文本用公鑰加密。以下是一個函數

//通過讀取本地的cer文件,得到公鑰,然后對文本內容加密,返回加密后的文本,最后傳到服務器端,與密鑰進行比對

public static String getRSAText(Context context,String strText){

try {

//讀取證書文件

InputStream inStream = context.getResources().getAssets().open("test.cer");

//創建X509工廠類

CertificateFactory cf = CertificateFactory.getInstance("X.509");

//創建證書對象

X509Certificate cert = (X509Certificate)cf.generateCertificate(inStream);

inStream.close();

Cipher c1 = Cipher.getInstance("RSA/None/PKCS1Padding");

c1.init(Cipher.PUBLIC_KEY, cert);

byte[] cipherText = c1.doFinal(strText.getBytes());

//base64轉換一下,方便傳輸

String res = Base64.encodeToString(cipherText,Base64.DEFAULT);

return res;

}

catch (InvalidKeyException e){

}

catch (IOException e){

}

catch (CertificateException e){

}

catch (NoSuchPaddingException e){

}

catch (IllegalBlockSizeException e){

}

catch (BadPaddingException e){

}

catch (NoSuchAlgorithmException e){

}

return "";

}

下邊是php端用pfx文件(里邊放有密鑰)進行解密的代碼。我改寫成了一個函數。參數是pfx文件路徑,pfx文件名,要解密的字符串

function getPassword( $path,$pfx_file_name,$str ){

$pfxpath = $path . "/public/$pfx_file_name"; //密鑰文件路徑

//$cerpath = "./test.cer"; //生成證書路徑

//私鑰解密

$decrypted = "";

$key2 = openssl_pkey_get_private(file_get_contents($pfxpath));

$res = openssl_private_decrypt(base64_decode($str), $decrypted, $key2);

if($res){

return $decrypted;

}

else{

return "";

}

}

總結

以上是生活随笔為你收集整理的php 加密cer_php 生成RSA非对称加密用的证书-cer-pfx文件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。