Xigbar Posted April 15, 2009 Share Posted April 15, 2009 I'm doing a Caesar cipher code for a class and I need help getting the letters to shift, and I need help. Quote Link to comment https://forums.phpfreaks.com/topic/154225-caesar-cipher/ Share on other sites More sharing options...
jackpf Posted April 15, 2009 Share Posted April 15, 2009 I've got a good idea... 1. Explain what a Caesar cipher is, for those of us who are unlightened. 2. Post your current code 3. Define whatyou're having trouble with. That might incurr some more helpful responses. Quote Link to comment https://forums.phpfreaks.com/topic/154225-caesar-cipher/#findComment-810796 Share on other sites More sharing options...
Mchl Posted April 15, 2009 Share Posted April 15, 2009 Isn't that ROT13? Quote Link to comment https://forums.phpfreaks.com/topic/154225-caesar-cipher/#findComment-810798 Share on other sites More sharing options...
Carth Posted April 15, 2009 Share Posted April 15, 2009 What do you have so far (code)? To shift letters, have a look at ord(). You need to wrap around when you reach z, and ignore non-alphabetical characters. Then convert back with chr(). You can loop through doing that, or you can create an array of the letters and use it to translate each character. Quote Link to comment https://forums.phpfreaks.com/topic/154225-caesar-cipher/#findComment-810800 Share on other sites More sharing options...
Daniel0 Posted April 15, 2009 Share Posted April 15, 2009 1. Explain what a Caesar cipher is, for those of us who are unlightened. A shifting algorithm. Let the key k ∈ Z, let P be a set containing alphabetic characters (a-z) forming the plain text value, let S be a set for the cipher text. |P|=|S|. Case-insensitively, the letter 'a'=0, 'b'=1, 'c'=2, etc. The function f maps a letter to its numerical value, the function g does the opposite. ∀ pi∈P, si∈S : si = g((f(pi) + k) % 25) Good enough? Coincidentally, the last line should explain exactly how to implement it. Before anyone asks, the algorithm is named like that because Caesar supposedly used that algorithm to encrypt messages to his generals. Whether that's true or not we'll let historians figure out. Isn't that ROT13? ROT13 is the above algorithm with k=13. Quote Link to comment https://forums.phpfreaks.com/topic/154225-caesar-cipher/#findComment-810814 Share on other sites More sharing options...
jackpf Posted April 16, 2009 Share Posted April 16, 2009 Aha. So a = 0, b = 1, c = 2...etc. Hmm...doesn't sound like a very secure algorithm. If I happened to figure out that a site was using this algorithm, I could probably work out what the hashes were without a pc Quote Link to comment https://forums.phpfreaks.com/topic/154225-caesar-cipher/#findComment-811299 Share on other sites More sharing options...
Mchl Posted April 16, 2009 Share Posted April 16, 2009 Vg fgvyy jbexf svar vs lbh jnag gb cbfg fcbvyref, be na nafjre gb n dhvm... be jungrire... Quote Link to comment https://forums.phpfreaks.com/topic/154225-caesar-cipher/#findComment-811302 Share on other sites More sharing options...
MasterACE14 Posted April 16, 2009 Share Posted April 16, 2009 Aha. So a = 0, b = 1, c = 2...etc. Hmm...doesn't sound like a very secure algorithm. If I happened to figure out that a site was using this algorithm, I could probably work out what the hashes were without a pc would a website with that algorithm be worth working out? lol Quote Link to comment https://forums.phpfreaks.com/topic/154225-caesar-cipher/#findComment-811303 Share on other sites More sharing options...
Daniel0 Posted April 16, 2009 Share Posted April 16, 2009 Aha. So a = 0, b = 1, c = 2...etc. Hmm...doesn't sound like a very secure algorithm. If I happened to figure out that a site was using this algorithm, I could probably work out what the hashes were without a pc Well, yeah you need some way to numerically represent the characters. You cannot say 't'+'c'. That doesn't make sense unless you've defined how these should numerically be represented. Anyway, no, this is not a secure cipher, but OP said this is "for class". It would be an excellent introductory cipher in a cryptology class. You could probably then move on to to Vigenère cipher, which is a bit more advanced, but still relatively simple to understand. Of course eventually you would learn about ciphers that are applicable today such as RSA. Quote Link to comment https://forums.phpfreaks.com/topic/154225-caesar-cipher/#findComment-811311 Share on other sites More sharing options...
Xigbar Posted April 17, 2009 Author Share Posted April 17, 2009 <?php $alphabet_count=array(); for($i=0; $i<26; $i++){ $alphabet_count[$i]='0';} $message="LZWJW OSK FGLZAFY KG NWJQ JWESJCSTDW AF LZASL"; $number_message=array(); for($x=0; $x<80; $x++){ if($message[$x]=="A"){ $alphabet_count[0]++; $number_mesaage[$x]="0"; } elseif($message[$x]=="B"){ $alphabet_count[1]++; $number_mesaage[$x]="1"; } elseif($message[$x]=="C"){ $alphabet_count[2]++; $number_mesaage[$x]="2"; } elseif($message[$x]=="D"){ $alphabet_count[3]++; $number_mesaage[$x]="3"; } elseif($message[$x]=="E"){ $alphabet_count[4]++; $number_mesaage[$x]="4"; } elseif($message[$x]=="F"){ $alphabet_count[5]++; $number_mesaage[$x]="5"; } elseif($message[$x]=="G"){ $alphabet_count[6]++; $number_mesaage[$x]="6"; } elseif($message[$x]=="H"){ $alphabet_count[7]++; $number_mesaage[$x]="7"; } elseif($message[$x]=="I"){ $alphabet_count[8]++; $number_mesaage[$x]="8"; } elseif($message[$x]=="J"){ $alphabet_count[9]++; $number_mesaage[$x]="9"; } elseif($message[$x]=="K"){ $alphabet_count[10]++; $number_mesaage[$x]="10"; } elseif($message[$x]=="L"){ $alphabet_count[11]++; $number_mesaage[$x]="11"; } elseif($message[$x]=="M"){ $alphabet_count[12]++; $number_mesaage[$x]="12"; } elseif($message[$x]=="N"){ $alphabet_count[13]++; $number_mesaage[$x]="13"; } elseif($message[$x]=="O"){ $alphabet_count[14]++; $number_mesaage[$x]="14"; } elseif($message[$x]=="P"){ $alphabet_count[15]++; $number_mesaage[$x]="15"; } elseif($message[$x]=="Q"){ $alphabet_count[16]++; $number_mesaage[$x]="16"; } elseif($message[$x]=="R"){ $alphabet_count[17]++; $number_mesaage[$x]="17"; } elseif($message[$x]=="S"){ $alphabet_count[18]++; $number_mesaage[$x]="18"; } elseif($message[$x]=="T"){ $alphabet_count[19]++; $number_mesaage[$x]="19"; } elseif($message[$x]=="U"){ $alphabet_count[20]++; $number_mesaage[$x]="20"; } elseif($message[$x]=="V"){ $alphabet_count[21]++; $number_mesaage[$x]="21"; } elseif($message[$x]=="W"){ $alphabet_count[22]++; $number_mesaage[$x]="22"; } elseif($message[$x]=="X"){ $alphabet_count[23]++; $number_mesaage[$x]="23"; } elseif($message[$x]=="Y"){ $alphabet_count[24]++; $number_mesaage[$x]="24"; } elseif($message[$x]=="Z"){ $alphabet_count[25]++; $number_mesaage[$x]="25"; } } $max_alphabet=$alphabet_count[0]; $max_letter=0; for($count=1; $count<26;$count++){ if($alphabet_count[$count]>$max_alphabet) { $max_alphabet=$alphabet_count[$count]; $max_letter=$count; } } $shift=$max_letter-4; $change=4-$max_letter; for($x=0; $x<80; $x++){ if($number_message[$x]-$shift<0){ //print $max_letter+$change $actual_letter=$max_letter+$change; } else{ //print $max_letter-$shift; $acutal_letter=$max_letter-$shift; $number_message[$x]=$number_message[$x]+$shift; } if($actual_letter==0){ print"A"; } if($actual_letter==1){ print"B"; } if($actual_letter==2){ print"C"; } if($actual_letter==3){ print"D"; } if($actual_letter==4){ print"E"; } if($actual_letter==5){ print"F"; } if($actual_letter==6){ print"G"; } if($actual_letter==7){ print"H"; } if($actual_letter=={ print"I"; } if($actual_letter==9){ print"J"; } if($actual_letter==10){ print"K"; } if($actual_letter==11){ print"L"; } if($actual_letter==12){ print"M"; } if($actual_letter==13){ print"N"; } if($actual_letter==14){ print"O"; } if($actual_letter==15){ print"P"; } if($actual_letter==16){ print"Q"; } if($actual_letter==17){ print"R"; } if($actual_letter==18){ print"S"; } if($actual_letter==19){ print"T"; } if($actual_letter==20){ print"U"; } if($actual_letter==21){ print"V"; } if($actual_letter==22){ print"W"; } if($actual_letter==23){ print"X"; } if($actual_letter==24){ print"Y"; } if($actual_letter==25){ print"Z"; } } ?> That's my current code (Don't laugh at me I just started programming this year) so yeah... any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/154225-caesar-cipher/#findComment-812440 Share on other sites More sharing options...
Daniel0 Posted April 17, 2009 Share Posted April 17, 2009 I was hoping my post describing the algorithm would have suggested an implementation at the same time. function caesarEncode($plaintext, $key) { for ($i = 0, $length = strlen($plaintext); $i < $length; $i++) { if (!ctype_alpha($plaintext[$i])) { continue; } $first = ord($plaintext[$i]) >= ord('a') ? ord('a') : ord('A'); $offset = ord($plaintext[$i]) - $first + $key; $plaintext[$i] = chr($first + $offset % 26); } return $plaintext; } function caesarDecode($ciphertext, $key) { return caesarEncode($ciphertext, -$key); } echo caesarEncode('This is a test', 3); // Wklv lv d whvw echo caesarDecode('Wklv lv d whvw', 3); // This is a test Quote Link to comment https://forums.phpfreaks.com/topic/154225-caesar-cipher/#findComment-812457 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.