首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在PHP中生成一个针对hash的个人算法

如何在PHP中生成一个针对hash的个人算法
EN

Stack Overflow用户
提问于 2018-01-19 09:26:20
回答 2查看 270关注 0票数 0

我想为PHP中的文本哈希创建一个个人算法。“xyz”中的字母“a”,“256”中的“b”等等。这怎么可能?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-01-19 09:45:08

我对工作感到厌烦,所以我想我可以解决这个问题。这一点都不安全。密码必须是硬编码的,密码字符必须有3的大小。

代码语言:javascript
复制
<?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链接:

https://3v4l.org/chR2A

票数 0
EN

Stack Overflow用户

发布于 2018-01-19 09:39:35

可以通过简单创建一个函数来替换字符,如下所示:

代码语言:javascript
复制
function myEncrypt ($text)
{
    $text = str_replace(array('a', 'b'), array('xby', '256'), $text);
    // ... others

    return $text;
}

带有两个数组"search“和"replaceWith”的版本作为参数传递:

代码语言:javascript
复制
function myEncrypt ($text, $search=array(), $replaceWith=array())
{
    return str_replace($search, $replaceWith, $text);   
}

警告:用这种方法加密文本并不是正确的解决方案,有很多更好的方法可以使用进行安全加密(例如,this post)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48337821

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档