簡體   English   中英

如何使用 PDO 以安全的方式顯示和隱藏密碼

[英]How to show and hide a password in a secure way using PDO

再會,

我想顯示存儲在 SQL-Server 中的密碼,並在單擊按鈕后顯示。 單擊按鈕時,必須使用 PDO 發出請求。 我將如何 go 並通過存儲在 SQL 服務器中加密的密碼並在用戶單擊后解密密碼來以安全的方式執行此操作?

最好添加一個按鈕來編輯此輸入字段,以便用戶在擁有所需權限時可以更新此密碼。

我將添加步驟來審核誰查看了密碼。

例如

<!DOCTYPE html>
<html>
<head>
        <title>Change Input Text Value OnClick Event</title>
</head>
<body>
<input type="text" id="my_field" value="***********">
<button onclick="change()">Show Password</button>
<script>
function change(){
    var txt = "My Password"; // get this value using php, what would the best way be toencrypt and decrypt passwords
    document.getElementById("my_field").value = txt;
}
</script>
</body>
</html>

密碼驗證示例

// See the password_hash() example to see where this came from.
$hash = '$2y$07$BCryptRequires22Chrcte/VlQH0piJtjXl.0t1XkA8pw9dMXTpOq';

if (password_verify('rasmuslerdorf', $hash)) {
    echo 'Password is valid!';
} else {
    echo 'Invalid password.';
}

我找到了我正在尋找的東西。

不幸的是將無法使用密碼散列方法,因此我需要使用“openssl_encrypt()”創建我的加密和解密 function,並從 geeksforgeeks.com 找到這篇文章並解釋了該過程。

然后,我將在 SQL 和 PHP 之間進行修改和加密,以使其按我想要的方式工作。

鏈接: https://www.geeksforgeeks.org/how-to-encrypt-and-decrypt-a-php-string/

示例代碼

<?php

// Store a string into the variable which
// need to be Encrypted
$simple_string = "Welcome to GeeksforGeeks\n";

// Display the original string
echo "Original String: " . $simple_string;

// Store the cipher method
$ciphering = "AES-128-CTR";

// Use OpenSSl Encryption method
$iv_length = openssl_cipher_iv_length($ciphering);
$options = 0;

// Non-NULL Initialization Vector for encryption
$encryption_iv = '1234567891011121';

// Store the encryption key
$encryption_key = "GeeksforGeeks";

// Use openssl_encrypt() function to encrypt the data
$encryption = openssl_encrypt($simple_string, $ciphering,
            $encryption_key, $options, $encryption_iv);

// Display the encrypted string
echo "Encrypted String: " . $encryption . "\n";

// Non-NULL Initialization Vector for decryption
$decryption_iv = '1234567891011121';

// Store the decryption key
$decryption_key = "GeeksforGeeks";

// Use openssl_decrypt() function to decrypt the data
$decryption=openssl_decrypt ($encryption, $ciphering,
        $decryption_key, $options, $decryption_iv);

// Display the decrypted string
echo "Decrypted String: " . $decryption;

?>

暫無
暫無

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

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