gpt4 book ai didi

php - 如何在php中没有mcrypt库的情况下加密字符串

转载 作者:可可西里 更新时间:2023-11-01 13:36:49 25 4
gpt4 key购买 nike

在函数中需要 key 来加密字符串而无需在 php 中使用 mcrypt libraly

function encrypt($str, $pass){
$str_arr = str_split($str);
$pass_arr = str_split($pass);
$add = 0;
$div = strlen($str) / strlen($pass);
while ($add <= $div) {
$newpass .= $pass;
$add++;
}
$pass_arr = str_split($newpass);
foreach($str_arr as $key =>$asc) {
$pass_int = ord($pass_arr[$key]);
$str_int = ord($asc);
$int_add = $str_int + $pass_int;
$ascii .= chr($int_add);
}
return $ascii;
}

function decrypt($enc, $pass){
$enc_arr = str_split($enc);
$pass_arr = str_split($pass);
$add = 0;
$div = strlen($enc) / strlen($pass);
while ($add <= $div) {
$newpass .= $pass;
$add++;
}
$pass_arr = str_split($newpass);
foreach($enc_arr as $key =>$asc) {
$pass_int = ord($pass_arr[$key]);
$enc_int = ord($asc);
$str_int = $enc_int - $pass_int;
$ascii .= chr($str_int);
}
return $ascii;
}

这对我测试的角色不起作用

最佳答案

还有一个方法:

$key = 'the quick brown fox jumps over the lazy ';
$string = 'Hey we are testing encryption';

echo enc_encrypt($string, $key)."\n";
echo enc_decrypt(enc_encrypt($string, $key), $key)."\n";

function enc_encrypt($string, $key) {
$result = '';
for($i = 0; $i < strlen($string); $i++) {
$char = substr($string, $i, 1);
$keychar = substr($key, ($i % strlen($key))-1, 1);
$char = chr(ord($char) + ord($keychar));
$result .= $char;
}

return base64_encode($result);
}

function enc_decrypt($string, $key) {
$result = '';
$string = base64_decode($string);

for($i = 0; $i < strlen($string); $i++) {
$char = substr($string, $i, 1);
$keychar = substr($key, ($i % strlen($key))-1, 1);
$char = chr(ord($char) - ord($keychar));
$result .= $char;
}

return $result;
}

同样,不要指望这会非常安全。

关于php - 如何在php中没有mcrypt库的情况下加密字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/800922/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com