HFD Posted March 23, 2009 Share Posted March 23, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/150753-caesar-cipher-using-php/ Share on other sites More sharing options...
Yesideez Posted March 23, 2009 Share Posted March 23, 2009 Might be an idea to explain what a string looks like before and after so we have more of an idea - never heard of Caesar encryption before. Quote Link to comment https://forums.phpfreaks.com/topic/150753-caesar-cipher-using-php/#findComment-791998 Share on other sites More sharing options...
HFD Posted March 23, 2009 Author Share Posted March 23, 2009 Thanks for your reply. What a Caesar cipher does is increment every letter by a certain amount, so if by incrementing by two HELLO becomes JGNNQ Quote Link to comment https://forums.phpfreaks.com/topic/150753-caesar-cipher-using-php/#findComment-792000 Share on other sites More sharing options...
Yesideez Posted March 23, 2009 Share Posted March 23, 2009 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() Quote Link to comment https://forums.phpfreaks.com/topic/150753-caesar-cipher-using-php/#findComment-792010 Share on other sites More sharing options...
HFD Posted March 23, 2009 Author Share Posted March 23, 2009 Thanks only problem is in that scenario I'd have to make 25 arrays with the offsets for the different shifts - that's of course entirely possible and fine but just wondering if there's a more efficient way? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/150753-caesar-cipher-using-php/#findComment-792020 Share on other sites More sharing options...
Yesideez Posted March 23, 2009 Share Posted March 23, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/150753-caesar-cipher-using-php/#findComment-792022 Share on other sites More sharing options...
HFD Posted March 23, 2009 Author Share Posted March 23, 2009 Ah ok thanks, the arrays should do fine Quote Link to comment https://forums.phpfreaks.com/topic/150753-caesar-cipher-using-php/#findComment-792025 Share on other sites More sharing options...
adam291086 Posted March 23, 2009 Share Posted March 23, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/150753-caesar-cipher-using-php/#findComment-792026 Share on other sites More sharing options...
lonewolf217 Posted March 23, 2009 Share Posted March 23, 2009 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>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/150753-caesar-cipher-using-php/#findComment-792031 Share on other sites More sharing options...
Yesideez Posted March 23, 2009 Share Posted March 23, 2009 Just what I have said but adam is only going to be able to use 10 letters otherwise the encrypted string will be much longer and decrypting it will be a nightmare! Quote Link to comment https://forums.phpfreaks.com/topic/150753-caesar-cipher-using-php/#findComment-792032 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.