Hey everyone, I wrote a Caesar cipher (a script where, given a message, the letters in the message are shifted x number of places; e.g. A => D, B => E, C => F, X => A, Y => B, Z => C, etc.). It works but it shifts all ASCII characters. I want to exclude characters that are not A-Z or a-z (outside the ASCII values of 65 - 90 and/or 97 - 122) so that spaces, punctuation, numbers all remain the same instead of shifting as well. Here's the form on index.html
<form class="form-inline" role="form" method="post">
<div class="table-responsive">
<table>
<tr class="tr_top">
<td class="td_top"><textarea class="form-control" rows="4" name="msg" placeholder="Your message here." onfocus='this.select()'><?php
require ('encode.php');
require ('decode.php');
if (isset($_POST['encode'])) {
echo $encstring;
} elseif (isset($_POST['decode'])) {
echo $decstring;
}
?></textarea></td>
</tr>
<tr class="tr_mid">
<td class="td_mid"><input type=text class="form-control input_mid" name="offset" value="<?php if (isset($_POST['encode']) || isset($_POST['decode'])) { echo htmlspecialchars($_POST['offset']);} ?>" placeholder="Enter a number." pattern="[0-9]{0,3}" oninvalid="setCustomValidity('Please enter a number between 1 and 999.')" oninput="setCustomValidity('')"></td>
</tr>
<tr class="tr_bottom">
<td class="td_bottom">
<input class="input_bottom btn btn-default submit" type="submit" name="encode" value="Encode">
<input class="input_bottom btn btn-default submit" type="submit" name="decode" value="Decode">
<input class="input_bottom btn btn-default" type="button" value="Clear"</td>
</tr>
</table>
</div><!-- close table-responsive -->
</form>
<?php
//encode
require ('encode.php');
if (isset($_POST['encode'])) {
echo "<p>Original message:</p>";
echo "<p class='string ital'>" . $string . "</p>";
echo "<p>Encoded message:</p>";
echo "<p class='string ital'>" . $newstring . "</p>";
}
//decode
require ('decode.php');
if (isset($_POST['decode'])) {
echo "<p>Encoded message:</p>";
echo "<p class='string ital'>" . $string . "</p>";
echo "<p>Decoded message:</p>";
echo "<p class='string ital'>" . $newstring . "</p>";
}
?>
and here is encode.php
<?php
$string = $_POST['msg'];
$newstring = $string;
$sp = $_POST['offset'];
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);
$encstring = $newstring;
}
?>
Any help would be appreciated as I can't figure it out. If you want to see it in action it's here.