我正在使用RIJNDAEL来加密/解密密码和其他一些字符串,但它不起作用。
这是我的加密/解密功能:
function secreto($accion,$clave,$palabra) {
//SETEO DATA EN 0 PORSIACA
$data = 0;
//INICIALIZO EL VECTOR
$iv = md5($clave);
//SI TENGO QUE ENCRIPTAR
if($accion == 'encripta') {
$data = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $clave, $palabra, MCRYPT_MODE_CBC, $iv);
$data = base64_encode($data);
} elseif($accion == 'decripta') {
$data = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $clave, base64_decode($palabra), MCRYPT_MODE_CBC, $iv);
$data = rtrim($data, "");
}
return $data;
}
以下是我用来登录用户的相关部分:
session_start();
if(isset($_SESSION['usuario']) && isset($_SESSION['domadmin']) && isset($_SESSION['superadmin'])) {
$usuario = $_SESSION['usuario'];
$tipoadmin = $_SESSION['tipoadmin'];
if(isset($_GET['p'])) { $p = $_GET['p']; } else { $p = "resumen"; }
echo TPcabecera();
echo TPmenu($p);
echo TPcentral($p);
echo TPpie();
} else {
if(isset($_POST['usuario']) && isset($_POST['password'])) {
$post_usr = mysqli_real_escape_string(sqls("mail"), $_POST['usuario']);
$post_pwd = mysqli_real_escape_string(sqls("mail"), $_POST['password']);
$data = sql("mail", "SELECT email,superadmin,domadmin,syspass FROM users WHERE email = '".$post_usr."'");
if(mysqli_num_rows($data) == 0) {
echo TPlogin();
} else {
$row = mysqli_fetch_assoc($data);
$pass = secreto('decripta',$passsalt,$row['syspass']);
if($post_pwd == $pass) {
$usuario = $row['email'];
if($row['superadmin'] == '1') { $tipoadmin = 'superadmin'; } elseif($_SESSION['domadmin'] != '0') { $tipoadmin = $_SESSION['domadmin']; } else { $tipoadmin = '0'; }
if(isset($_GET['p'])) { $p = $_GET['p']; } else { $p = "resumen"; }
echo TPcabecera();
echo TPmenu($p);
echo TPcentral($p);
echo TPpie();
} else {
echo TPlogin();
}
}
} else {
echo TPlogin();
}
}
如果我回显$ pass和$ post_pwd,它们完全相同,但if($ post_pwd == $ pass)不验证...我是否以错误的方式使用加密?
答案 0 :(得分:2)
您的secreto
功能应该“正常”。使用您的登录代码,我看不到您初始化$passsalt
的位置。因此,您的代码中有一部分未显示,或者$passsalt
实际上未设置。
但您的加密不安全:
答案 1 :(得分:0)
考虑到Separation of concerns组织代码,因此请在小的单独步骤中执行每项任务。这将帮助您编写更清晰的代码并将其保留DRY。
我建议将编码/解码部分放入单独的功能中。例如:
<?php
// Encrypt function
function mc_encrypt($encrypt, $mc_key)
{
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);
$passcrypt = trim(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $mc_key, trim($encrypt), MCRYPT_MODE_ECB, $iv));
$encode = base64_encode($passcrypt);
return $encode;
}
// Decrypt function
function mc_decrypt($decrypt, $mc_key)
{
$decoded = base64_decode($decrypt);
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);
$decrypted = trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $mc_key, trim($decoded), MCRYPT_MODE_ECB, $iv));
return $decrypted;
}
这样你就可以直接打电话了。
对于身份验证,请看一下:
http://ulogin.sourceforge.net/
同时检查出来:
Highly Secure Data Encryption & Decryption Made Easy with PHP, MCrypt, Rijndael-256, and CBC