Php 用于存储加密文本的mysqli函数

Php 用于存储加密文本的mysqli函数,php,mysqli,Php,Mysqli,我已经使用了下面的代码,它看起来相当健壮:它可以使用不同的语言(UTF-8),现在我想将结果存储在数据库中。我的问题是,我只熟悉过时的mysql语句,我想使用mysqli。有人能给我指出一个简单但安全的函数,它将接受输入并将其存储在数据库中…然后告诉我在代码中需要添加它的位置吗 <?php /* * PHP 5.3.18 on windows XP * * I don't have open_ssl active from PHP so used MCRYPT_RAND for th

我已经使用了下面的代码,它看起来相当健壮:它可以使用不同的语言(UTF-8),现在我想将结果存储在数据库中。我的问题是,我只熟悉过时的mysql语句,我想使用mysqli。有人能给我指出一个简单但安全的函数,它将接受输入并将其存储在数据库中…然后告诉我在代码中需要添加它的位置吗

<?php
/*
 * PHP 5.3.18 on windows XP
 *
 * I don't have open_ssl active from PHP so used MCRYPT_RAND for the salt.
 * It is adequate for this exercise.
 *
 * As the encoded SALT and encrypted output are binary code i have converted all
 * the output to Base64 encoding to ensure it is HTML safe.
 *
 * It selects the appropriate default action in the 'do' select list.
 *
 * There is a new 'salt' generated at each encryption and the user is prevented from
 * changing it by making the display field as 'readonly'. Normally this would be a 'hidden' field'.
 *
 */

$isEncrypted = null; // used to set default output options
                     // i like to pre-declare the script 'global' variables


$key_size = mcrypt_get_key_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CFB);
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CFB);


if($_POST){ // we have some input...
$encryption_key = $_POST["key"];
$string = $_POST["msg"]; // this may be base64 encoded...


if($_POST["do"]=="encrypt"){
    $isEncrypted = true; // used to set defaults

    $iv = mcrypt_create_iv($iv_size,  MCRYPT_RAND); // new salt with each encryption
    $result = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $encryption_key, $string, MCRYPT_MODE_CFB, $iv);
    $result = base64_encode($result); // $result is binary so encode as HTML safe.


}else{
    $isEncrypted = false; // used to set defaults

    $iv = base64_decode($_POST["iv"]); // get current salt converted back to binary format
    $string = base64_decode($string); // convert encoded text back to binary string
    $result = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $encryption_key, $string, MCRYPT_MODE_CFB, $iv);
}
}else{ // no input so create something useful...
  $isEncrypted = false; // used to set default actions

  $result = 'enter text to encrypt...'; // sample text
  $iv = mcrypt_create_iv($iv_size,  MCRYPT_RAND); // new salt
  $encryption_key = substr('testing!' . uniqid() . '!testing', 0, $key_size);
}
?>
<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Test Encryption with base64 encoding.</title>
  </head>

  <body>
    <div class="main" id="main">
    <!-- heading -->
    <strong><?php echo $isEncrypted ? 'Encrypted' : 'Decrypted'; ?></strong><br/>

    <form method="POST" action="">

        <!-- do not allow the user to change the salt by setting 'readonly' -->
        <input type="text" value="<?php echo base64_encode($iv); ?>" readonly name="iv"/> <br/>

        <!-- supply a suggested password but the user can change it -->
        <input type="text" value="<?php echo $encryption_key; ?>" name="key"/><br/>

        <!-- either show the encoded text as HTML safe string -->
        <!--- or show as plain text -->
        <textarea name="msg" ><?php echo $result; ?></textarea><br/>

        <!-- set the appropriate action as the default -->
        <select name="do">
          <option <?php echo $isEncrypted ? 'selected' : ''; ?>>decrypt</option>
          <option <?php echo $isEncrypted ? '' : 'selected'; ?>>encrypt</option>
        </select><br/>

        <input type="submit" value="GO"/>
    </form>
</div>
  </body>
</html>

使用base64编码测试加密。

>解密
您仍将编写“旧”MySQL语句,因为“mysqli”是一个改进的驱动程序(因此称为“i”),可以利用MySQL>=4.1中的功能。它被认为是“双重过程和面向对象的API”。例如,要连接到MySQL数据库,可以执行以下操作:


您还应该看看(PHP数据对象),它提供了一个数据访问抽象层,允许您的代码访问除MySQL之外的更多DBMs。

另一方面,有人知道为什么相同的代码可以在我的服务器上正常工作,但当我将其剪切并粘贴到我的本地开发服务器上时,会导致一个白色页面?这不是打字错误,我仔细检查过了。配置中的任何东西,也许?我不打算使用mysql以外的任何东西,但如果我使用过,那么PDO是有意义的。现在我正在努力学习这两种方法中哪一种是有意义的。我可以用“mysqli\u connect”替换“mysql\u connect”吗?所有其他的(如mysql\u result、mysql\u fetch\u row等)都可以替换为“mysqli”版本吗?它是否像全局搜索一样简单并替换为“mysqli”?不幸的是,不是。但是您可能还是应该切换,因为从PHP 5.5.0开始,“mysql”驱动程序就不受欢迎,并且“将来”将被删除。
// mysqli, procedural way
$mysqli = mysqli_connect('localhost','username','password','database');

// mysqli, object oriented way
$mysqli = new mysqli('localhost','username','password','database');