我几乎失去了想要扭转这个功能的想法,我的一个朋友建议问“职业选手”所以我在这里。
<?php
$data = "Data to be encrypted";
$ceva = $data;
$textHos = 'MCRYPT_RIJNDAEL_128';
function encrypt($plaintext,$textHos) {
$textLen=str_pad(dechex(strlen($plaintext)),8, '0', STR_PAD_LEFT);
$salt='WSj2g7jTvc8ISmL60Akn';
$textHosHash=hash('sha256',$salt.$textHos);
$textHos= md5($textHos,true);
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $textHos,
$plaintext, MCRYPT_MODE_CBC,$iv);
$ciphertext = $iv . $textHosHash . $textLen . $ciphertext;
$ciphertext_base64 = base64_encode($ciphertext);
return $ciphertext_base64;
}
$data = encrypt($ceva,$textHos);
echo $data;
?>
输出结果为:
P8avDeviXdd7bKfNMP0gwmZmZjg1OWMzOWFlNzRiMzU2Y2JiMTQ5OTY4MTI3MWNiYjQzYjBkMTAyNDUzM2ZhNGJjZmZhNzQ4M2QxN2M0ZGYwMDAwMDAxNN2xStdw/bhxIxSOevRp37HiXJeVXz7Ge31KEvq9dZjT
有关将加密文本再次解析为可读文本的任何帮助吗? 感谢。
答案 0 :(得分:1)
您的加密功能会产生很多废话,我希望它不会在任何生产环境中运行。
function encrypt($plaintext,$textHos) {
// not needed..
//$textLen=str_pad(dechex(strlen($plaintext)),8, '0', STR_PAD_LEFT);
//$salt='WSj2g7jTvc8ISmL60Akn';
//$textHosHash=hash('sha256',$salt.$textHos);
$textHos = md5($textHos,true);
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
$ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $textHos,
$plaintext, MCRYPT_MODE_CBC,$iv);
// i commented out the unneccessary parts..
$ciphertext = $iv /* . $textHosHash . $textLen . */ $ciphertext;
$ciphertext_base64 = base64_encode($ciphertext);
return $ciphertext_base64;
}
因此,加密数据中剩下的是iv向量(以及一些不必要数据的72个字符)和加密数据本身 - 用base64编码
扭转这一点非常容易
function decrypt($ciphertext, $textHos) {
$text = base64_decode($ciphertext);
$iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
$iv = substr($text, 0, $iv_size);
$textHos = md5($textHos, true);
// the +72 is neccessary for your original code - the code above doesn't need this part
$ciphertext = substr($text, $iv_size + 72);
$encrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $textHos, $ciphertext, MCRYPT_MODE_CBC, $iv);
return rtrim($encrypted, chr(0));
}
注意:请勿在生产中使用此代码!现在AES128不安全