php加密用数据库中的密钥解密

时间:2014-07-05 19:04:29

标签: php database encryption

我简单地尝试将加密的电子邮件地址和加密密钥存储在数据库中的两个单独字段中,并在另一个页面上解密。有时候它有效,但它大部分没有。

function encrypt($string, $key){
    $encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $string, MCRYPT_MODE_CBC, md5(md5($key))));
    return $encrypted;
}
function decrypt($string, $key){
    $decrypted = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($string), MCRYPT_MODE_CBC, md5(md5($key))), "\0");
    return $decrypted;
}

我尝试将我的关键字段从varchar更改为varbinary,似乎没有什么区别。 我按计划进行,所以所有的想法都是受欢迎的 编辑:来自我的代码的片段 的index.php

    $mysqli = new mysqli(DBHOST, DBUSER, DBPASSWORD, DB);
// $crypter = new Crypter();
if($mysqli->connect_errno){
    echo "Failed to connect to MYSQL (".$mysqli->connect_errno.")".$mysqli->connect_error;
}
//construct string and mail to email
$email = trim($_POST['signUpEmail']);
$encryption_key = substr(md5(microtime()), 0,10);

//insert details into table
$password = $_POST['signUpPassword'];
$hashed_password = encrypt_password($password);
$stmt = $mysqli->prepare('INSERT INTO signup (username, hashed_password, enc_key) VALUES (?,?,?)');
$stmt->bind_param("sss", $email, $hashed_password, $encryption_key);
if($stmt->execute()){
    $encrypted_email = encrypt($email, $encryption_key);

    $row = $stmt->insert_id;
    //encrypting the row num
    $enc_row = mc_encrypt($row, '000');
    $link = "signup/signuser.php?u=".$enc_row."&&e=".$encrypted_email;
    echo 'mail link <a href="'.$link.'">'.$link.'</a>'."<br />";

signuser.php

    $enc_id = $_GET['u'];
$enc_email = $_GET['e'];
//$crypter = new Crypter();
echo $id = mc_decrypt($enc_id, '000');

if((int)$id > 0){

    $validation = new Validation();
    if($validation->is_num($id, 'u')){
        $mysqli = new mysqli(DBHOST, DBUSER, DBPASSWORD, DB);
        if($mysqli->connect_errno){
            die('Connection Error');
        }
        if(!$stmt = $mysqli->prepare('SELECT signup.enc_key FROM loo_signup WHERE id = ?')){
            die('preparing failed');
        }
        if(!$stmt->bind_param('i', $id)){
            die('binding undone');
        }
        if(!$stmt->execute()){
            die('executing failed');
        }
        if(!$stmt->bind_result($key)){
            die('binding failed');
        }
        if($stmt->fetch()){
            echo decrypt($enc_email, $key);
        }

1 个答案:

答案 0 :(得分:0)

有一个AES加密类,http://asirra-php.googlecode.com/svn/trunk/AES.class.php 可以像这样使用:

function crypt_data($content, $encrypt) {
    include_once ('AES.class.php');
    $_content = "";
    if (!empty($content)) {
        $key256 = '603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4';
        $Cipher = new AES(AES::AES256);

        if ($encrypt) {
            $_content = $Cipher->encrypt($Cipher->stringToHex($content), $key256);    
        } else {
            $_content = $Cipher->hexToString($Cipher->decrypt($content, $key256));    
        }
    }

    return $_content;
}

然后使用它:

$encrypted = crypt_data($string_to_encrypt, true);
$decrypted = crypt_data($string_to_decrypt, false);