[导入]php 安全基础 附录C. 加密
附錄C. 加密
? 作為一本相關安全方面的書,通常加密是需要提及的話題。我之所以在本書的主體部分忽略了加密問題,是因為它的用途是狹窄的,而開發者應從大處著眼來考慮安全問題。過分依賴于加密常常會混淆問題的根源。盡管加密本身是有效的,但是進行加密并不會神奇地提高一個應用的安全性。
? 一個PHP開發人員應主要熟悉以下的加密方式:
?
l ? ? ? ?對稱加密
l ? ? ? ?非對稱加密(公鑰)
l ? ? ? ?Hash函數(信息摘要)
l ? ? ? ?信息驗證碼
?
? 本附錄主要關注于使用mcrypt擴展的對稱加密算法。你需要參考的資料如下:
?
? 實用加密技術(Applied Cryptography), by Bruce Schneier (Wiley)
http://www.schneier.com/blog/
http://wikipedia.org/wiki/Cryptography
http://phpsec.org/articles/2005/password-hashing.html
http://pear.php.net/package/Crypt_HMAC
http://pear.php.net/package/Crypt_RSA
?
C.1. 密碼的存儲
? 當你在數據庫內存儲的密碼時,永遠不要以明碼方式存入,而是應該存儲密碼的hash值并同時使用附加字串:
?
? <?php
?
? /* $password contains the password. */
?
? $salt = 'SHIFLETT';
? $password_hash = md5($salt . md5($password . $salt));
?
? /* Store password hash. */
?
? ?>
?
? 當你需要確認一個密碼是否正確時,以同樣的方式計算出hash值并比較異同:
?
? <?php
?
? $salt = 'SHIFLETT';
? $password_hash = md5($salt . md5($_POST['password'] . $salt));
?
? /* Compare password hashes. */
?
? ?>
?
? 如果hash值完全相同,你就有理由認為密碼也是相同的。
? 如果使用了這個技巧,是不可能告訴用戶他們的密碼是什么的。當用戶忘記密碼時,你只能讓他錄入一個新密碼并重新計算hash值存入數據庫。當然,你需要非常小心地對用戶進行身份確認——密碼提醒機制是易受頻繁攻擊的目標,同時也是經常出現安全漏洞的源頭。
?
C.2. 使用mcrypt
? PHP的標準加密擴展是mcrypt,它支持很多不同的加密算法。你可以通過mcrypt_list_algorithms( )函數來查看你的平臺上支持的算法列表:
?
? <?php
?
? echo '<pre>' . print_r(mcrypt_list_algorithms(), TRUE) . '</pre>';
?
? ?>
?
? 加密和解密分別由mcrypt_encrypt( ) 及 mcrypt_decrypt( )函數來實現。這兩個函數都有5個參數,第一個參數是用于指定使用的算法:
?
? <?php
?
? mcrypt_encrypt($algorithm,
? ? ? ? ? ? ? ? ?$key,
? ? ? ? ? ? ? ? ?$cleartext,
? ? ? ? ? ? ? ? ?$mode,
? ? ? ? ? ? ? ? ?$iv);
?
? mcrypt_decrypt($algorithm,
? ? ? ? ? ? ? ? ?$key,
? ? ? ? ? ? ? ? ?$ciphertext,
? ? ? ? ? ? ? ? ?$mode,
? ? ? ? ? ? ? ? ?$iv);
?
? ?>
?
? 其中的加密鍵(第二個參數)是非常敏感的數據,因此你要確保把它存放在安全的地方。可以用第八章中保護數據庫權限的方法來保護加密鍵。如果經濟條件允許的話,硬件加密鍵是最好的選擇,它提供了超級強大的安全性。
? 函數有多種模式可供選擇,你可以使用mcrypt_list_modes( )來列出所有支持的模式:
? <?php
?
? echo '<pre>' . print_r(mcrypt_list_modes(), TRUE) . '</pre>';
?
? ?>
?
? 第五個參數($iv)為初始化向量,可以使用mcrypt_create_iv( )函數建立。
? 下面的示例類提供了基本的加密解密方法:
?
? class crypt
? {
? ? private $algorithm;
? ? private $mode;
? ? private $random_source;
?
? ? public $cleartext;
? ? public $ciphertext;
? ? public $iv;
?
? ? public function __construct($algorithm = MCRYPT_BLOWFISH,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? $mode = MCRYPT_MODE_CBC,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? $random_source = MCRYPT_DEV_URANDOM)
? ? {
? ? ? $this->algorithm = $algorithm;
? ? ? $this->mode = $mode;
? ? ? $this->random_source = $random_source;
? ? }
?
? ? public function generate_iv()
? ? {
? ? ? $this->iv = mcrypt_create_iv(mcrypt_get_iv_size($this->algorithm,
? ? ? ? $this->mode), $this->random_source);
? ? }
?
? ? public function encrypt()
? ? {
? ? ? $this->ciphertext = mcrypt_encrypt($this->algorithm,
? ? ? ? $_SERVER['CRYPT_KEY'], $this->cleartext, $this->mode, $this->iv);
? ? }
?
? ? public function decrypt()
? ? {
? ? ? $this->cleartext = mcrypt_decrypt($this->algorithm,
? ? ? ? $_SERVER['CRYPT_KEY'], $this->ciphertext, $this->mode, $this->iv);
? ? }
? }
?
? ?>
?
? 上面的類會在其它示例中使用,下面是它的使用方法示例:
?
? <?php
?
? $crypt = new crypt();
?
? $crypt->cleartext = 'This is a string';
? $crypt->generate_iv();
? $crypt->encrypt();
?
? $ciphertext = base64_encode($crypt->ciphertext);
? $iv = base64_encode($crypt->iv);
?
? unset($crypt);
?
? /* Store $ciphertext and $iv (initialization vector). */
?
? $ciphertext = base64_decode($ciphertext);
? $iv = base64_decode($iv);
?
? $crypt = new crypt();
?
? $crypt->iv = $iv;
? $crypt->ciphertext = $ciphertext;
? $crypt->decrypt();
?
? $cleartext = $crypt->cleartext;
?
? ?>
?
小提示
? 本擴展要求你在編譯PHP時使用-mcrypt標識。安裝指南及要求詳見http://php.net/mcrypt。
?
C.3. 信用卡號的保存
? 我常常被問到如何安全地保存信用卡號。我的總是會首先詢問他們是否確實有必要保存信用卡號。畢竟不管具體是如何操作的,引入不必要的風險是不明智的。同時國家法律還有關于信用卡信息處理方面的規定,我還時刻小心地提醒我并不是一個法律專家。
? 本書中我并不會專門討論信用卡處理的方法,而是會說明如何保存加密信息到數據庫及在讀取時解密。該流程會導致系統性能的下降,但是確實提供了一層保護措施。其主要優點是如果數據庫內容泄密暴露出的只是加密信息,但是前提是加密鍵是安全的。因此,加密鍵與加密的實現方法本身同樣重要。
? 保存加密數據到數據的過程是,首先加密數據,然后通過初始向量與明文建立密文來保存到數據庫。由于密文是二進制字符串,還需要通過base64_encode( )轉換成普通文本字符串以保證二進制編碼的安全存儲。
?
? <?php
?
? $crypt = new crypt();
?
? $crypt->cleartext = '1234567890123456';
? $crypt->generate_iv();
? $crypt->encrypt();
?
? $ciphertext = $crypt->ciphertext;
? $iv = $crypt->iv;
?
? $string = base64_encode($iv . $ciphertext);
?
? ?>
?
? 保存該字串至數據庫。在讀取時,則是上面流程的逆處理:
?
? <?php
?
? $string = base64_decode($string);
?
? $iv_size = mcrypt_get_iv_size($algorithm, $mode);
?
? $ciphertext = substr($string, $iv_size);
? $iv = substr($string, 0, $iv_size);
?
? $crypt = new crypt();
?
? $crypt->iv = $iv;
? $crypt->ciphertext = $ciphertext;
? $crypt->decrypt();
?
? $cleartext = ?$crypt->cleartext;
?
? ?>
?
? 本實現方法假定加密算法與模式不變。如果它們是不定的話,你還要保存它們以用于解密數據。加密鍵是唯一需要保密的數據。
?
C.4. 加密會話數據
? 如果你的數據庫存在安全問題,或者部分保存在會話中的數據是敏感的,你可能希望加密會話數據。除非很有必要,一般我不推薦這樣做,但是如果你覺得在你的情形下需要這樣做的話,本節提供了一個實現方法的示例。
? 這個方案十分簡單。實際上,在第八章中,已經說明了如何通過調用session_set_save_handler( )來執行你自己的會話機制。通過對保存和讀取數據的函數的少量調整,你就能加密存入數據庫的數據及在讀取時解密數據:
?
? <?php
?
? function _read($id)
? {
? ? global $_sess_db;
?
? ? $algorithm = MCRYPT_BLOWFISH;
? ? $mode = MCRYPT_MODE_CBC;
?
? ? $id = mysql_real_escape_string($id);
?
? ? $sql = "SELECT data
? ? ? ? ? ? FROM ? sessions
? ? ? ? ? ? WHERE ?id = '$id'";
?
? ? if ($result = mysql_query($sql, $_sess_db))
? ? {
? ? ? ? $record = mysql_fetch_assoc($result);
?
? ? ? ? $data = base64_decode($record['data']);
?
? ? ? ? $iv_size = mcrypt_get_iv_size($algorithm, $mode);
?
? ? ? ? $ciphertext = substr($data, $iv_size);
? ? ? ? $iv = substr($data, 0, $iv_size);
?
? ? ? ? $crypt = new crypt();
?
? ? ? ? $crypt->iv = $iv;
? ? ? ? $crypt->ciphertext = $ciphertext;
? ? ? ? $crypt->decrypt();
?
? ? ? ? return $crypt->cleartext;
? ? }
?
? ? return '';
? }
?
? function _write($id, $data)
? {
? ? global $_sess_db;
?
? ? $access = time();
?
? ? $crypt = new crypt();
?
? ? $crypt->cleartext = $data;
? ? $crypt->generate_iv();
? ? $crypt->encrypt();
?
? ? $ciphertext = $crypt->ciphertext;
? ? $iv = $crypt->iv;
?
? ? $data = base64_encode($iv . $ciphertext);
?
? ? $id = mysql_real_escape_string($id);
? ? $access = mysql_real_escape_string($access);
? ? $data = mysql_real_escape_string($data);
?
? ? $sql = "REPLACE
? ? ? ? ? ? INTO ? ?sessions
? ? ? ? ? ? VALUES ?('$id', '$access', '$data')";
?
? ? return mysql_query($sql, $_sess_db);
? }
[新聞]NHibernate 2.0.0.Beta1發布了
文章來源:http://www.cnblogs.com/czh-liyu/archive/2008/04/02/1133998.html
轉載于:https://www.cnblogs.com/xfliu/archive/2008/07/01/1233385.html
總結
以上是生活随笔為你收集整理的[导入]php 安全基础 附录C. 加密的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 再学 GDI+[68]: 路径画刷(8)
- 下一篇: 用 JOTM 向Servlet中添加事务