我想为PHP中的文本哈希创建一个个人算法。“xyz”中的字母“a”,“256”中的“b”等等。这怎么可能?
发布于 2018-01-19 09:45:08
我对工作感到厌烦,所以我想我可以解决这个问题。这一点都不安全。密码必须是硬编码的,密码字符必须有3的大小。
<?php
//define our character->crypted text
$cryptArray = array( "a"=>"xyz","b"=>"256");
//This is our input
$string = "aab";
//Function to crypt the string
function cryptit($string,$cryptArray){
//create a temp string
$temp = "";
//pull the length of the input
$length = strlen($string);
//loop thru the characters of the input
for($i=0; $i<$length; $i++){
//match our key inside the crypt array and store the contents in the temp array, this builds the crypted output
$temp .= $cryptArray[$string[$i]];
}
//returns the string
return $temp;
}
//function to decrypt
function decryptit($string,$cryptArray){
$temp = "";
$length = strlen($string);
//Swap the keys with data
$cryptArray = array_flip($cryptArray);
//since our character->crypt is count of 3 we must $i+3 to get the next set to decrypt
for($i =0; $i<$length; $i = $i+3){
//read from the key
$temp .= $cryptArray[$string[$i].$string[$i+1].$string[$i+2]];
}
return $temp;
}
$crypted = cryptit($string,$cryptArray);
echo $crypted;
$decrypted = decryptit($crypted,$cryptArray);
echo $decrypted; 输入是:aab
产出如下:
xyzxyz256
aab
以下是3v4l链接:
发布于 2018-01-19 09:39:35
可以通过简单创建一个函数来替换字符,如下所示:
function myEncrypt ($text)
{
$text = str_replace(array('a', 'b'), array('xby', '256'), $text);
// ... others
return $text;
}带有两个数组"search“和"replaceWith”的版本作为参数传递:
function myEncrypt ($text, $search=array(), $replaceWith=array())
{
return str_replace($search, $replaceWith, $text);
}警告:用这种方法加密文本并不是正确的解决方案,有很多更好的方法可以使用进行安全加密(例如,this post)。
https://stackoverflow.com/questions/48337821
复制相似问题