Jump to content
Old threads will finally start getting archived ×

Recommended Posts

Hi, I'm currently in the midst of coding some simple cryptography applications for my portfolio in University. I've decided to create a simple Caesar cipher encrypter/decrypter, and I'm struggling with the underlying code. Of course, I just want to get it working on a predefined string and shift before I add in code to let the user decide.

 

The part I'm struggling with is converting each character in the string to it's ASCII value, then incrementing it. Can anyone offer any tips on how to do this? Here's my code so far:

 

<?php
$string = "Hello World";
$stringlength = strlen($string);

for ($counter = 0; $counter < $stringlength; $counter++)
{
$string[$counter]++;
echo $string[$counter];
}

?>

 

Thanks :)

Link to comment
https://forums.phpfreaks.com/topic/150753-caesar-cipher-using-php/
Share on other sites

I think I know it now.

 

You'll need to make an array containing all the offsets of the new characters.

$key=array('A' => 'Q', 'B' => 'W', 'C' => 'E'...);

 

Then a loop to read through your string replacing the characters.

$str='HELLO WORLD';
$length=strlen($str);
$newstr='';
for ($i=0;$i<$length;++$i) {
  if (in_array($str[$i],$key[$i])) {
    $newstr.=$key[$i];
  }
}

 

Something like that.

 

EDIT: Replaced count() with strlen()

I don't think so - you're always going to need an array defining what each letter will become. Plus to decrypt you'll need another array with the letters reversed...

$key=array('Q' => 'A', 'W' => 'B', 'E' => 'C'..);

 

You can't make it from random either because then you won't be able to decrypt anything unless you keep the key.

hey

 

I have been looking at the cipher with uni too.

 

do an array

 

$key=array('A' => '1', 'B' => '2', 'C' => '3'...);

 

Then split a string down into the characters and compare it to the array and that will give you the letters as numbers you can then add a N amount to each number for the shift.

 

 

 

this works, thought it might not be the most optimized.  currently it is designed for letters, though the concept can be used for any section of ascii values

 

<?php

$string = "hello";
$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 $newstring."<br>";
}

?>



This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.