dongleman4760 2014-08-28 17:49
浏览 108
已采纳

简单加密:PHP中需要的解密算法


I'm working the webpage that I have some text like this:
AA,BB,CC,A1,B2,C3
And I want some code to replace the the word (like AA) to the string that saved in variable for example this is my php variable:

<?php
    $var1 = "A"; //For AA
    $var2 = "B"; //For BB 
    $var3 = "C"; //For CC
    $var4 = "1"; //For A1
    $var5 = "2"; //For B2
    $var6 = "3"; //For C3
?>

And the text AA,BB,CC,A1,B2,B3 will be like this:
ABC123
How can I do this with php?

In summery I need a decryption algorithm for a cypher text of AA,BB,CC,A1,B2,C3 to be converted as plan text of ABC123.

  • 写回答

4条回答 默认 最新

  • doupike2351 2014-08-28 17:58
    关注

    Full working script with sample encryption catalog given here.

    <?php
    $encript = array(
    'a'=>'GG', 
    'b'=>'HH',  
    'c'=>'II',
    
    'A'=>'DD',  
    'B'=>'EE',
    'B'=>'FF',
    
    '1'=>'AA', 
    '2'=>'BB',
    '3'=>'CC');
    
    
    function decrypt($str,$encript)
    {
        $decript = array_flip($encript);
        $str_arr = explode(",",$str);
        $dec = "";
        foreach($str_arr as $val)
        {
            $dec .= $decript[strtoupper(trim($val))];
        }
        return $dec;
    }
    
    function encrypt($str,$encript)
    { 
        $str_arr = str_split($str);
        $dec = "";
        foreach($str_arr as $val)
        {
            $dec .= $encript[trim($val)].",";
        }
        return $dec;
    }
    
    
    $cypher = "AA,BB,DD,CC,EE,FF,GG,HH";
    $text = "Ab1Ca";
    
    echo decrypt($cypher,$encript);
    echo "<br/>";
    echo encrypt($text,$encript);
    ?>
    
    • First we have to create the mapping for each character as associative array.
    • In decrypt function first we have to flip the array.
    • Then have to loop through each encoding string and build the real text from flipped array.
    • In encryption split the string as single letter array.
    • Loop through the array and build encryption using the mapping array.

    The working out put available at following url: http://sugunan.net/demo/str1.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧