PHP 資料加密 (學習筆記)

wsAdmin發表於2019-07-30
1.防止通訊內容被竊聽;
2.防止通訊內容被篡改
1.對稱加密:加密與解密使用的是同一個秘鑰,:DES(Data Encryption Standard),1977-1999,1999年被破解;AES(Advance Encryption Standard),目前最流行的對稱加密演算法
2.非對稱加密:RSA
1.使用PHP加密/解密函式 openssl_encrypt/openssl_decrypt
注意:有很多例子使用的是PHP mcrypt_encrypt() 函式,官網給我的解釋:This function has been DEPRECATED as of PHP 7.1.0. Relying on this function is highly discouraged.
//獲取可用的密碼加密演算法列表
//$methods = openssl_get_cipher_methods();
//var_dump($methods);

# AES加密演示
//明文(要加密的內容)
$str = "這是測試用例 我是明文";
//秘鑰(用例:使用uniqid()函式生成了一個唯一ID)
$key = "5d3fb4acb2292";
//加密演算法
$method = "AES-128-CBC";
//加密向量(要求16個位元組)
$iv = "1234567812345678";
$encrypt_str = openssl_encrypt($str, $method, $key, 0, $iv);
var_dump("AES加密結果:".$encrypt_str);

# AES解密演示
//$encrypt_str AES加密後產生的密文
//$key 秘鑰(同上)
$decrypt_str = openssl_decrypt($encrypt_str, $method, $key, 0, $iv);
var_dump("AES解密結果:".$decrypt_str);

學習文章

1./私鑰加密演算法,屬於非對稱加密:
2.優點:極難被破解;
3.缺點:速度慢,運算次數多,不適合加密長文字;
//公鑰(專案中可線上生產亦可自己生成)
$PUBLIC_KEY = "-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApJJ7D/U9lHLNQdl4LZSr
jNvdCelIraMnSD/iujWxyw/QDLXPCtP06ll42JURGlYaO2DU5c5BKEUF0alyzlE9
XiHRXPl0LabI/CjGtrIB4RApy1PjkQ31QOt+9R2Nmb7RUkfZwnCWHBlNVnwj4U6J
woccrlUdElBWU5twFc2PNPbMR6nA/ldUwDpcveNHNp57BrgYfUFcLrjmf2LH6c7X
ngBNPbG5ha5pmsaXm8MAqBRtAvIwvUsvJLIr+XRc27pCJFe/1MtS4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pAbiQe+qXf9x/waWB/hQKBgQDKLQB9Ep9A6UFlumXaEr971A7HcQI2BsfP
kRfCcT1rphnENHCa37o+5i6tTImAXI+aayp9Jpv0rXLbzFbBkdUdUDINulXSsLRT
bq3ttbu5c+NG21XW1fvVqf4VYOP7u/l0Z2eBIsg9uLswS3zltTG8ikm+RKhMf1DV
PDAOoLmMEwKBgDn0po9a9/Rlx5qmLM7OtMFGwUQO2clXYILEwvATmc9HxncvTfOO
V0gWWTxAvUA+qsLlOXhuTGQ/0nSu4pgnusGQUXeF5N8l6Grbhj0C2itYeQUoiZd/
m8uX/01/Rwu84O/K25jZOnfDIn3uAFe6xjy7vKwstckT5txCS9S+SgNNAoGAbvLl
Sr32cUvQXMA+9r7FIHJOLfsBaJ6t9mW8cTNtrm63wym4BfXzImN1iBrxdmTVVbur
1IRkn5Cz8JUhoxahqnWBEnGIeZgJTaP2hPXvcCV9uzvQzpYdnrKsQhUq59HPYqcA
cSiiVOTUrPswLmsSQVJuh6Dr7xcLSAnAobZoPMsCgYEAsJuY5RcB1sjYortRNsKb
KHLiLI93P0MFF46V/343d3BU7TZfETg703Mj2AfOAGTM2p2BkHFri3l+4oigMqpr
hAp4hNq4KFK2SCjzedrLV7QIgtp/uMZ+q/yhRtiG8kSWlI9c0Un00+KqIwFqfwAB
l1zOX5QcMa1X7eWSvZ559ko=
-----END PRIVATE KEY-----";

//待加密明文
$data = "這是RSA待加密明文";
//用於接收加密後的密文
$content_encrypt = "";
 # 私鑰加密
openssl_private_encrypt($data, $content_encrypt, $PRIVATE_KEY, 1);
var_dump("私鑰加密結果:".$content_encrypt);

 # 公鑰解密
//$content_encrypt 私鑰加密後的密文
//用於接收解密後的明文
$content_decrypt = "";
openssl_public_decrypt($content_encrypt, $content_decrypt, $PUBLIC_KEY, 1);
var_dump("公鑰解密結果:".$content_decrypt);

1.新建 text.php 檔案——生成簽名

//根軍 MD5()函式的不可逆性進行簽名校驗
//首先必須要有 $appKey與$secretKey
$appKey = "5d3fb4acb2292";
$secretKey = "5d3fb4acb22925d3fb4acb22925d3fb4acb2292";

$url = "localhost/text_sig.php";

//待傳遞的引數
$params['appKey'] = $appKey;
$params['name'] = "張三";
$params['age'] = "26";
$params['sex'] = "男";
$params['root'] = "admin";
$params['password'] = "123456";
$params['time'] = time();
//獲取簽名
$params['sig'] = createSig($params,$secretKey);
$param_str = http_build_query($params);
$url = $url.'?'.$param_str;
var_dump($url);


//生成簽名
function createSig($params,$secretKey){
    //對引數進行排序
    ksort($params);
    $str = http_build_query($params);
    $str .= $secretKey;
    return md5($str);
}

2.使用 text.php 檔案得到的 URL 訪問 -> 服務端 text_sig.php 檔案——-驗證簽名

//獲取傳值
$get = $_GET;
//獲取appKey
$appKey = $get['appKey'];
//根據appKey獲取儲存在資料庫中對應的secretKey
$secretKey = "5d3fb4acb22925d3fb4acb22925d3fb4acb2292";
//判斷介面是否過期
if (abs($get['time'] - time()) > 100){
    die("Time Out");
}
//獲取簽名
$sig = $get['sig'];
//將簽名從引數中剔除
unset($get['sig']);
//對引數進行排序
ksort($get);
//將引數陣列轉為'&'連線的字串
$str = http_build_query($get);
//引數字串後拼接$secretKey
$str .= $secretKey;
//使用MD5加密
$md5_str = md5($str);
//將得到的加密後的結果與簽名對比
if ($sig === $md5_str){
    var_dump("驗籤成功");
}else{
    var_dump("驗籤失敗");
}

本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章