问题描述
在函数中需要密码加密字符串,而不需要在PHP中使用mcrypt libraly。
函数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;
}
函数解密($ 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;
}
在这不适合我的字符我测试它
这段代码效率很低,我不感到惊讶,没有给出预期的结果。
echo $($)code echo echoEncrypt('cheese','pie')。 \\\
< br> \\\
.RotDecrypt(RotEncrypt('cheese','pie'),'pie');
//ÓÑÊÕÊÊ
// cheese
函数RotEncrypt($ str,$ pass){
$ pass = str_split(str_pad('',strlen ),$ pass,STR_PAD_RIGHT));
$ stra = str_split($ str);
foreach($ stra as $ k => $ v){
$ tmp = ord($ v)+ ord($ pass [$ k]);
$ stra [$ k] = chr($ tmp> 255?($ tmp-256):$ tmp);
}
return join('',$ stra);
}
函数RotDecrypt($ str,$ pass){
$ pass = str_split(str_pad('',strlen($ str),$ pass,STR_PAD_RIGHT));
$ stra = str_split($ str);
foreach($ stra as $ k => $ v){
$ tmp = ord($ v)-ord($ pass [$ k]);
$ stra [$ k] = chr($ tmp< 0?($ tmp + 256):$ tmp);
}
return join('',$ stra);
}
经过测试,对我来说似乎工作正常。
无论哪种方式,我觉得我应该警告你,这是一种非常不安全的加密形式,大多数密码使用相同的字符集和相当小的长度范围,这使得该系统非常容易受到人们能够以一个相当好的准确度来制定密码。
in function need key to encrypt string without mcrypt libraly in php
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;
}
in this not work for i character i test it
This code is rather inefficient, I'm not surprised it doesn't give intended results.
echo RotEncrypt('cheese', 'pie') . "\n<br>\n"
.RotDecrypt(RotEncrypt('cheese', 'pie'), 'pie');
// ÓÑÊÕÜÊ
// cheese
function RotEncrypt($str, $pass){
$pass = str_split(str_pad('', strlen($str), $pass, STR_PAD_RIGHT));
$stra = str_split($str);
foreach($stra as $k=>$v){
$tmp = ord($v)+ord($pass[$k]);
$stra[$k] = chr( $tmp > 255 ?($tmp-256):$tmp);
}
return join('', $stra);
}
function RotDecrypt($str, $pass){
$pass = str_split(str_pad('', strlen($str), $pass, STR_PAD_RIGHT));
$stra = str_split($str);
foreach($stra as $k=>$v){
$tmp = ord($v)-ord($pass[$k]);
$stra[$k] = chr( $tmp < 0 ?($tmp+256):$tmp);
}
return join('', $stra);
}
Tested and it appears to work fine for me.
Either way I feel I should warn you that this is a really insecure form of encryption, most passwords use the same sets of characters and a fairly small range of lengths, which makes this system very vulnerable to people being able to work out with a fairly good level of accuracy the password.
这篇关于如何加密字符串没有mcrypt库在PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!