【发布时间】:2018-06-28 12:01:35
【问题描述】:
我想在 PHP 中为散列文本制作一个个人算法。 “xyz”中的字母“a”、“256”中的“b”等等。这怎么可能?
【问题讨论】:
-
认为尝试制作自己的地穴是没有用的。制定一个好的算法是相当困难的。不会很安全。
-
如果我的回答对您有帮助,请将我的回答标记为已接受。
我想在 PHP 中为散列文本制作一个个人算法。 “xyz”中的字母“a”、“256”中的“b”等等。这怎么可能?
【问题讨论】:
可以通过简单地创建一个进行字符替换的函数,如下所示:
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);
}
警告:这种方法不是加密文本的正确解决方案,有很多更好的方法可以使用 PHP 进行安全加密(例如,参见 this post)。 p>
【讨论】:
我工作很无聊,所以我想我会试一试。 这根本不安全。 crypt 必须是硬编码的,crypted 字符的大小必须为 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
输出为:xyzxyz256aab
这是 3v4l 链接:
https://3v4l.org/chR2A
【讨论】: