2014-06-23 2 views
0

아래 코드를 사용하면 매우 강력 해 보입니다. 다른 언어 (UTF-8)로 작동하고 이제 결과를 데이터베이스에 저장하려고합니다. 내 문제는 내가 오래되고 구형 인 mysql 문에 익숙하고 mysqli를 사용하고 싶다는 것이다. 누구든지 입력을 받아 데이터베이스에 저장하는 간단하면서도 안전한 기능을 가르쳐 주시겠습니까? 그리고이 코드에서이 코드를 어디에 추가해야하는지 알려주십시오.암호화 된 텍스트를 저장하는 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> 
+0

다른 메모에서 누구도 왜 내 서버에서 야생에서 잘 작동하는지 알지만, 잘라내어 내 로컬 dev 서버에 붙여 넣으면 흰색 페이지가 표시됩니까? 오타가 아니라 신중히 확인했습니다. 구성에있는 게 뭐니? 아마? – user1149499

답변

0

당신은 여전히 ​​'오래된'MySQL의 문을 작성한다 'mysqli'는 드라이버를 개선하기 때문에 (따라서는 "I")의 MySQL> = 4.1의 기능을 활용할 수 있습니다. '이중 프로 시저 및 객체 지향 API'로 간주됩니다. 예를 들어, MySQL의 DB에 연결하려면 either of the following을 할 수있는 :

// mysqli, procedural way 
$mysqli = mysqli_connect('localhost','username','password','database'); 

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

당신은 또한 PDO driver (PHP 데이터 개체)를 보라, 당신의 코드가 많은 액세스 할 수 있도록 데이터 액세스 추상화 계층을 제공하는 MySQL을 비롯한 더 많은 DBM.

+0

mysql 이외의 것을 사용하지는 않겠지 만 필자가 그렇게한다면 PDO는 의미가 있습니다. 지금 나는이 두 가지 방법 중 어느 것이 합리적인지 배우려하고 있습니다. "mysql_connect"를 "mysqli_connect"로 대체하고 "mysql_result", "mysql_fetch_row"등의 다른 모든 것을 "mysqli"버전으로 바꿀 수 있습니까? 글로벌 검색만큼 쉽고 "mysqli"로 대체 할 수 있습니까? – user1149499

+0

불행히도, 아니오. 그러나 "mysql"드라이버는 PHP 5.5.0에서 사용되지 않으므로 "나중에"제거됩니다. [매뉴얼 참조] (http://php.net/manual/en/intro.mysql.php) – Joseph8th

관련 문제