簡體   English   中英

在PHP中顯示加密密碼

[英]Displaying Encrypted Passwords in PHP

我正在建立一個網站的客戶在其“僅限會員”部分使用密碼/促銷代碼,使會員可以訪問特殊內容。 我正在通過MySQL存儲上述密碼,並使用sha1進行了加密。

客戶端需要在管理員控制面板中以純文本格式查看這些密碼。 顯然,這不是使用sha1的選項。

與存儲純文本相比,在加密數據庫中的密碼時有何取舍? 我是否應該將它們簡單地以純文本格式存儲在數據庫中,然后在發布后加鹽?

能夠顯示純文本密碼並保持適當安全級別的最佳方法是什么?

不顯示密碼。 只需為客戶端提供以其他用戶身份登錄的功能(我想這就是他們需要密碼的原因?),也許是在本身受僅客戶端已知密碼保護的頁面上。

您應該向您的客戶解釋為什么他們必須無權訪問密碼。 太多事情可能出錯。 流氓用戶,被盜的數據庫,錯過了SQL注入攻擊,認為這樣做不值得冒險。

這樣就可以了。

<?php
    # --- ENCRYPTION ---

    # the key should be random binary, use scrypt, bcrypt or PBKDF2 to
    # convert a string into a key
    # key is specified using hexadecimal
    $key = pack('H*', "bcb04b7e103a0cd8b54763051cef08bc55abe029fdebae5e1d417e2ffb2a00a3");

    # show key size use either 16, 24 or 32 byte keys for AES-128, 192
    # and 256 respectively
    $key_size =  strlen($key);
    echo "Key size: " . $key_size . "\n";

    $plaintext = "This string was AES-256 / CBC / ZeroBytePadding encrypted.";

    # create a random IV to use with CBC encoding
    $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
    $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);

    # use an explicit encoding for the plain text
    $plaintext_utf8 = utf8_encode($plaintext);

    # creates a cipher text compatible with AES (Rijndael block size = 128)
    # to keep the text confidential 
    # only suitable for encoded input that never ends with value 00h
    # (because of default zero padding)
    $ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key,
                                 $plaintext_utf8, MCRYPT_MODE_CBC, $iv);

    # prepend the IV for it to be available for decryption
    $ciphertext = $iv . $ciphertext;

    # encode the resulting cipher text so it can be represented by a string
    $ciphertext_base64 = base64_encode($ciphertext);

    echo  $ciphertext_base64 . "\n";

    # === WARNING ===

    # Resulting cipher text has no integrity or authenticity added
    # and is not protected against padding oracle attacks.

    # --- DECRYPTION ---

    $ciphertext_dec = base64_decode($ciphertext_base64);

    # retrieves the IV, iv_size should be created using mcrypt_get_iv_size()
    $iv_dec = substr($ciphertext_dec, 0, $iv_size);

    # retrieves the cipher text (everything except the $iv_size in the front)
    $ciphertext_dec = substr($ciphertext_dec, $iv_size);

    # may remove 00h valued characters from end of plain text
    $plaintext_utf8_dec = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key,
                                         $ciphertext_dec, MCRYPT_MODE_CBC, $iv_dec);

    echo  $plaintext_utf8_dec . "\n";
?>

資料來源: http : //www.php.net/manual/en/function.mcrypt-encrypt.php

您可以使用兩種方式加密來存儲和顯示密碼。 mcrypt_encrypt()mcrypt_decrypt 看看這個問題使用PHP的最簡單的雙向加密

不要這樣 查找替代路線。

如果您的客戶出於任何原因想要查看其用戶密碼,請詢問他們為什么要查看這些密碼,以及如果他們擁有這些密碼,他們會怎么做-然后為他們提供一種替代的直接方式,那就是永遠涉及到必須知道用戶密碼是什么。

無論您的客戶使用多么無辜, 大多數人都在各處重復使用他們的密碼。 您的客戶可能會認為他們僅知道自己系統的用戶密碼,但這意味着他們現在可能已經知道所有內容的用戶密碼-電子郵件,金融服務(例如網上銀行)等。

它只需要一名惡意員工,並且用戶的帳戶就會在Internet上的其他地方遭到破壞。 除了某人的隱私遭到侵犯之外,這可能完全是法律上的泥潭

尋找替代品。 永遠不要以純文本形式顯示用戶密碼。

它是使用SHA1存儲的,因此由於SHA1是單向哈希,因此無法以明文形式檢索。 您不能將其轉換為原始格式。 http://php.net/manual/zh/function.sha1.php

因此,您既可以使用md5格式,也可以將它們簡單地存儲為純文本格式,然后在發布后添加鹽

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM