dputlf5431 2014-01-17 03:45
浏览 30
已采纳

PHP Caesar密码

(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?

  • 写回答

2条回答 默认 最新

  • donglinyi4313 2014-01-17 04:36
    关注

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

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

报告相同问题?