PHP凯撒密码 [英] PHP Caesar cipher

查看:131
本文介绍了PHP凯撒密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(jsfiddle例子在这里:



我正在制作凯撒密码 - 基本上这是一个字母转换,移位参数为3如下:
a => d,b => e,c => f,...



到目前为止,我的代码是

  $ string =hello; 
回显< p> 。 $字符串。 < / P> 中;
$ newstring =hello; ($ i = 0; $ i< strlen($ string); $ i ++){

$ ascii = ord($ string [$ i]);

;
if($ ascii == 90){//大写字母绑定
$ ascii = 65; //重置为'A'
}
else if($ ascii == 122){//小写绑定
$ ascii = 97; //重置为'a'
}
else {
$ ascii ++;
}
$ newstring [$ i] = chr($ ascii);

}

echo< p> 。 $ newstring。 < / P> 中;

并且它可以正常工作。



I如下所示:

 < form> 
< table>
< tr>< th class =w>消息:< / th>
< td>< textarea class =tonfocus ='this.select()'name = msg> HELLO< / textarea>< / td>
< / tr>
< tr>第> Shift参数:< / th>
< td>< input type = text size = 2 name = sp value ='11'>< / td>
< / tr>
< tr>< td>< / td>< td>< input type = submit name = submit value ='Encode'>
< input type = submit name = submit value ='Decode'>
< input type = button value ='清除'onclick ='this.form.elements.msg.value ='< / td>< / tr>
< / table>
< / form>

我只需要更改PHP来输出表格,但不太清楚。所以,在textarea中,用户可以输入TESTING,选择一个8的移位参数,然后点击Encode,这将运行一个编码功能;同样,解码将运行解码功能。



任何想法如何更改PHP代码以使其按我喜欢的方式工作?

解决方案

首先,更改

  $ string = 你好; 

  $ string = $ _POST ['msg']; 

  $ newstring =hello; 

  $ newstring = $ string; 

然后,读取shift参数的值:

  $ sp = $ _POST ['sp'] 

现在,而不是做

  $ ascii ++; 

通过shift参数的值增加其值。如果$ sp的值是8,那么最终的$ ascii值将是$ ascii + 8



或者您可以执行elclanrs建议的操作:$ char ='一个'; ++ $字符; // =>b。但不是++ $ char; do $ char + = $ sp;

在增加$ ascii的值的同时,您必须检查边界。可以使用循环并将$ ascii的值增加1。



因此在for循环中:

<$ ($ i = 0; $ i< strlen($ string); $ i ++){...}
p>

会有另一个for循环,并且所有的边界条件都会进入循环。例如:

$ p $ for($ i = 0; $ i< strlen($ string); $ i ++){

$ ascii = ord($ string [$ i]);
for($ j = 0; $ j <$ sp; $ j ++){
if($ ascii == 90){//大写限制
$ ascii = 65; //重置为'A'
}
else if($ ascii == 122){//小写绑定
$ ascii = 97; //重置为'a'
}
else {
$ ascii ++;
}
}
$ newstring [$ i] = chr($ ascii);




(我不是PHP开发人员,所以语法可能是错的。)


(jsfiddle example here: caesar cipher)

I am making a Caesar cipher - it's an alphabetic shift, basically; a shift parameter of 3 is as follows: a => d, b => e, c => f, ...

My code so far is

$string = "hello";
echo "<p>" . $string . "</p>";
$newstring = "hello";

for ($i=0;$i<strlen($string);$i++) {

    $ascii = ord($string[$i]);
    if($ascii == 90) { //uppercase bound
        $ascii = 65; //reset back to 'A' 
        } 
    else if($ascii == 122) { //lowercase bound
        $ascii = 97; //reset back to 'a' 
        } 
    else {
        $ascii++;
    }
    $newstring[$i] = chr($ascii);

}

echo "<p>" . $newstring . "</p>";

and it works okay.

I would like a user to be able to enter their own string into a form's textarea as follows:

<form>
    <table>
        <tr><th class="w">Message:</th>
            <td><textarea class="t" onfocus='this.select()' name=msg>HELLO</textarea></td>
        </tr>
        <tr><th>Shift Parameter:</th>
            <td><input type=text size=2 name=sp value='11'></td>
        </tr>
        <tr><td></td><td><input type=submit name=submit value='Encode'>
        <input type=submit name=submit value='Decode'>
        <input type=button value='Clear' onclick='this.form.elements.msg.value=""'</td></tr>
    </table>
</form>

I just need to change the PHP to output the table, but don't quite know how. So, in textarea, a user could type "TESTING", choose a shift parameter of 8, and click "Encode" and that would run an encode function; likewise, "Decode" would run a decode function.

Any ideas how to change the PHP code to make it work the way I'd like?

解决方案

First,change

$string = "hello";

to

$string = $_POST['msg'];

and

$newstring = "hello";

to

$newstring = $string;

Then, read the value of shift parameter:

$sp = $_POST['sp']

Now, instead of doing

$ascii++;

increment its value by the value of shift parameter. if the value of $sp is 8, the final $ascii value would be $ascii + 8

Or you can do what "elclanrs" has suggested : $char = 'a'; ++$char; //=> "b". But instead of ++$char; do $char+=$sp;

While incrementing the value of $ascii, you must check for the bounds. May be use a loop and increment the value of $ascii by 1.

So Inside your for loop:

for ($i=0;$i<strlen($string);$i++) {...}

there will be another for loop and all the conditions for the bound would go inside it. Something like :

for ($i=0;$i<strlen($string);$i++) {

  $ascii = ord($string[$i]);
  for($j=0;$j<$sp;$j++){
    if($ascii == 90) { //uppercase bound
      $ascii = 65; //reset back to 'A' 
    } 
    else if($ascii == 122) { //lowercase bound
      $ascii = 97; //reset back to 'a' 
    } 
    else {
      $ascii++;
    }
  }
  $newstring[$i] = chr($ascii);

}

(I'm not a PHP developer, so the syntax might be wrong.)

这篇关于PHP凯撒密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆