Hey everyone,
I wrote a Caesar cipher (a script where, given a message, the letters in the message are shifter x number of places; e.g. A => D, B => E, C => F, X => A, Y => B, Z => C, etc.). It works but it shifts [I]all
The script is here:
[code=php]<?php
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 [URL=”http://avondaleriverside.com”]here
[code=php]
I changed the last else to else if as follows:
else if ($ascii < 65 || ($ascii > 90 || $ascii < 97) || $ascii > 122 ) { //uppercase bound
$ascii++;
}
[/code]
[code=php]
esleif ( ($ascii >= 65 and $ascii <= 90) or ($ascii >= 97 or $ascii <= 122 ) ) {
[/code]
[code=php]
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 if ( ($ascii >= 65 && $ascii <= 90) || ($ascii >= 97 || $ascii <= 122 ) ) {
$ascii++;
}
}
$newstring[$i] = chr($ascii);
$encstring = $newstring;
}
[/code]
[code=php]
<?php
$sp = 5;
$string = "Testing 1, 2, 3, and X, y, Z. All done!";
$newstring = array();
for ($i=0; $i < strlen($string); $i++) {
$ascii = ord($string[$i]);
if ($ascii < 65 || ($ascii > 90 && $ascii < 97) || $ascii > 122) {
$newstring[$i] = $string[$i];
}
else {
for ($j=0; $j < $sp; $j++) {
$ascii += 1;
if ($ascii == 91 or $ascii == 123) {
$ascii -= 26;
}
}
$newstring[$i] = chr($ascii);
}
}
$encstring = implode('', $newstring);
echo $encstring;
[/code]
<form class="form-inline" method="post" role="form">
lm fmpp
<p>Original message:</p>
[code=html]</form>
lm fmpp
<p>Original message:</p>[/code]
<i>
</i><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');
<i> </i> if (isset($_POST['encode'])) {
<i> </i> echo $encstring;
<i> </i> } elseif (isset($_POST['decode'])) {
<i> </i> echo $decstring;
<i> </i> }
<i> </i> ?></textarea></td>
<i> </i> </tr>
<i> </i> <tr class="tr_mid">
<i> </i> <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>
<i> </i> </tr>
<i> </i> <tr class="tr_bottom">
<i> </i> <td class="td_bottom">
<i> </i> <input class="input_bottom btn btn-default submit" type="submit" name="encode" value="Encode">
<i> </i> <input class="input_bottom btn btn-default submit" type="submit" name="decode" value="Decode">
<i> </i> <input class="input_bottom btn btn-default" type="button" value="Clear"</td>
<i> </i> </tr>
<i> </i> </table>
<i> </i> </div><!-- close table-responsive -->
<i> </i> </form>
<i> </i> <?php
<i> </i> //encode
<i> </i> require ('encode.php');
<i> </i> if (isset($_POST['encode'])) {
<i> </i> echo "<p>Original message:</p>";
<i> </i> echo "<p class='string ital'>" . $string . "</p>";
<i> </i> echo "<p>Encoded message:</p>";
<i> </i> echo "<p class='string ital'>" . $newstring . "</p>";
<i> </i> }
<i> </i> //decode
<i> </i> require ('decode.php');
<i> </i> if (isset($_POST['decode'])) {
<i> </i> echo "<p>Encoded message:</p>";
<i> </i> echo "<p class='string ital'>" . $string . "</p>";
<i> </i> echo "<p>Decoded message:</p>";
<i> </i> echo "<p class='string ital'>" . $newstring . "</p>";
<i> </i> }
<i> </i> ?>
[code=php]<?php
$sp = $_POST['offset'];
$string = $_POST['msg'];
$newstring = array();
for ($i=0; $i < strlen($string); $i++) {
$ascii = ord($string[$i]);
if ($ascii < 65 || ($ascii > 90 && $ascii < 97) || $ascii > 122) {
$newstring[$i] = $string[$i];
}
else {
for ($j=0; $j < $sp; $j++) {
$ascii += 1;
if ($ascii == 91 || $ascii == 123) {
$ascii -= 26;
}
}
$newstring[$i] = chr($ascii);
}
}
$encstring = implode('', $newstring);
echo $encstring;
?>[/code]
[code=php]
<?php
/**
* pseudo encrypt/decrypt a string
* @return string
* @param string $string text to be encoded/decoded
* @param int $offset number of positions to offset letters (only)
*/
function caesar_cipher($string, $offset)
{
if(!ctype_digit($offset)) {
throw new Exception("offset must be an integer");
}
$newstring = array();
for ($i=0; $i < strlen($string); $i++) {
$ascii = ord($string[$i]);
if ($ascii < 65 || ($ascii > 90 && $ascii < 97) || $ascii > 122) {
$newstring[$i] = $string[$i];
}
else {
for ($j=0; $j < abs($offset); $j++) {
$ascii += 1;
if($offset > 0 and ($ascii == 91 or $ascii == 123)) {
$ascii -= 26;
}
elseif($ascii == 65 or $ascii == 97) { // negative offset
$ascii += 26;
}
}
$newstring[$i] = chr($ascii);
}
}
$encstring = implode('', $newstring);
return $encstring;
}
[/code]
[code=php]
$string = "whatever...";
$sp = 5;
$encrypted = caesar_encrypt($string, $sp);
$decrypted = caesar_encrypt($string, $sp * -1); // just negate the original offset
[/code]
[code=php]
<?php
/**
* Provide a simple pseudo encryption of text only affecting [A-Za-z]
*/
class CaesarCipher
{
/**
* Encrypt it
* @return string
* @param string $string
* @param int $offset
*/
public function encrypt($string, $offset)
{
return $this->execute($string, $offset);
}
/**
* Decrypt it
* @return string
* @param string $string
* @param int $offset
*/
public function decrypt($string, $offset)
{
return $this->execute($string, $offset * -1);
}
/**
* pseudo encrypt/decrypt a string
* @return string
* @param string $string text to be encoded/decoded
* @param int $offset number of positions to offset letters (only)
*/
private function execute($string, $offset)
{
if(!is_int($offset)) {
throw new Exception("offset must be an integer");
}
// need to use this to fix bug:
$inc = 1;
if($offset < 0) {
$inc = -1;
}
$newstring = array();
for ($i=0; $i < strlen($string); $i++) {
$ascii = ord($string[$i]);
if ($ascii < 65 || ($ascii > 90 && $ascii < 97) || $ascii > 122) {
$newstring[$i] = $string[$i];
}
else {
for ($j=0; $j < abs($offset); $j++) {
$ascii += $inc;
if($offset > 0 and ($ascii == 91 or $ascii == 123)) {
$ascii -= 26;
}
elseif($ascii == 64 or $ascii == 96) { // negative offset
$ascii += 26;
}
}
$newstring[$i] = chr($ascii);
}
}
return implode('', $newstring);
}
}
[/code]
[code=php]
$text = "ABCDE, abcde, UVWXYZ, uvwxyz, 123.";
$sp = 4;
$crypt = new CaesarCipher();
echo "<p>Start:<br />$text</p>n";
$text = $crypt->encrypt($text, $sp);
echo "<p>Encrypted:<br />$text</p>n";
$text = $crypt->decrypt($text, $sp);
echo "<p>Decrypted:<br />$text</p>n";
[/code]
Start:
ABCDE, abcde, UVWXYZ, uvwxyz, 123.
Encrypted:
EFGHI, efghi, YZABCD, yzabcd, 123.
Decrypted:
ABCDE, abcde, UVWXYZ, uvwxyz, 123.
[/quote]
[code=php]<?php
$sp = $_POST['offset'];
$string = $_POST['msg'];
$newstring = array();
for ($i=0; $i < strlen($string); $i++) {
$ascii = ord($string[$i]);
if ($ascii < 65 || ($ascii > 90 && $ascii < 97) || $ascii > 122) {
$newstring[$i] = $string[$i];
}
else {
for ($j=0; $j < $sp; $j++) {
$ascii += 1;
if ($ascii == 91 || $ascii == 123) {
$ascii -= 26;
}
}
$newstring[$i] = chr($ascii);
}
}
$decstring = implode('', $newstring);
?>[/code]
[code=php]
$asciiArray = array();
$keysArray = array();
// make ascii array
for($ascii=65; $ascii<=122; $ascii++)
if($ascii<=90 or $ascii >=97){
// we're in range
$asciiArray[] = chr( $ascii );
// make keys
$keysArray[] = chr( $ascii);
}
shuffle( &$keysArray);
$code = array_combine($keysArray,$asciiArray);
[/code]
[CODE]YTo1Mjp7czoxOiJ3IjtzOjE6IkEiO3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gE hc cRqeEcE AkFgc 123456789 FV ReW...[/CODE]
0.1.9 — BETA 5.19