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条)

报告相同问题?

悬赏问题

  • ¥30 vb net 使用 sendMessage 如何输入鼠标坐标
  • ¥200 求能开发抖音自动回复卡片的软件
  • ¥15 关于freesurfer使用freeview可视化的问题
  • ¥100 谁能在荣耀自带系统MagicOS版本下,隐藏手机桌面图标?
  • ¥15 求SC-LIWC词典!
  • ¥20 有关esp8266连接阿里云
  • ¥15 C# 调用Bartender打印机打印
  • ¥15 我这个代码哪里有问题 acm 平台上显示错误 90%,我自己运行好像没什么问题
  • ¥50 C#编程中使用printDocument类实现文字排版打印问题
  • ¥15 找会编程的帅哥美女 可以用MATLAB里面的simulink编程,用Keil5编也可以。