To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > PHP Help > Code Critique

Code Critique Having someone critique your code is always a great way to hone the skills. Stop in and post your code to see what your peers may have done differently.

Reply
 
Thread Tools Rate Thread Display Modes
Old 10-19-2003, 01:03 PM   #1
drawmack
Computers can do that?
 
drawmack's Avatar
 
Join Date: Apr 2003
Location: Pocono Mtns PA
Posts: 3,268
Ceaser Cipher

I'm still bored so I made up a ceaser cipher in php.

Here is the function
PHP Code:
function ceaser($clicks,$input) {
    
//set output to stop errors from showing
    
$output = "";
    
    
//get the length of the string once to aid in efficency
    
$str_len = strlen($input);
    
    
//encryption loop
    
for($i=0;$i<$str_len;$i++) {
        
//get the ascii value of the current character
        
$cur_char = ord(substr($input,$i,1));
        
        
//if this is an upper case letter
        
if($cur_char > 64 && $cur_char < 91) {
            
//turn the letter into its position in the alphebet
            
$cur_char = $cur_char - 65;
            
//move the position according to clicks and turn the position
            //back into the value of the ascii letter
            
$cur_char = ($cur_char + $clicks)%26 + 65;
            
        
//if this is a lower case letter
        
} elseif ($cur_char > 96 && $cur_char < 123) {
            
//turn the letter into its position in the alphebet
            
$cur_char = $cur_char - 97;
            
//move the position according to clicks and turn the position
            //back into the value of the ascii letter
            
$cur_char = ($cur_char + $clicks)%26 + 97;
        }
//end if
        
        
$output .= chr($cur_char);
    }
//end for
    
return $output;
}
//end rot13
the attachment is a complete working example
Attached Files
File Type: zip ceaser.zip (1.6 KB, 209 views)
drawmack is offline   Reply With Quote
Old 10-20-2003, 07:06 AM   #2
marque
Senior Member
 
marque's Avatar
 
Join Date: Aug 2002
Location: Brisbane, Australia
Posts: 169
yhub fxwh gudzpdfn!
marque is offline   Reply With Quote
Old 10-20-2003, 10:45 AM   #3
Moonglobe
Better fan than rebelo!
 
Moonglobe's Avatar
 
Join Date: Apr 2003
Location: brain://localhost:left-side
Posts: 2,381
so is this just a rot13 implentation? 'cuz there is str_rot13()....
__________________
there's no place i can be, since i found serenity.
Moonglobe is offline   Reply With Quote
Old 10-20-2003, 11:09 AM   #4
laserlight
PHP Witch
 
laserlight's Avatar
 
Join Date: Apr 2003
Location: Singapore
Posts: 12,388
Not quite.

str_rot13() is a special case of a Caesar cipher.
__________________
Use Bazaar for your version control system
Read the PHP Spellbook
Learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 10-20-2003, 05:13 PM   #5
drawmack
Computers can do that?
 
drawmack's Avatar
 
Join Date: Apr 2003
Location: Pocono Mtns PA
Posts: 3,268
rot13 is a ceaser cipher with a key of 13, this is a generic implementation of the cesear cipher where you can use any number of clicks.
drawmack is offline   Reply With Quote
Old 10-20-2003, 10:06 PM   #6
Moonglobe
Better fan than rebelo!
 
Moonglobe's Avatar
 
Join Date: Apr 2003
Location: brain://localhost:left-side
Posts: 2,381
o of course lol, ya wasn't thinkin straight. thanks for clearing that up
__________________
there's no place i can be, since i found serenity.
Moonglobe is offline   Reply With Quote
Old 11-07-2003, 01:49 AM   #7
drawmack
Computers can do that?
 
drawmack's Avatar
 
Join Date: Apr 2003
Location: Pocono Mtns PA
Posts: 3,268
Quote:
Originally posted by marque
yhub fxwh gudzpdfn!
I finally got around to programming the chi square attack on the ceaser cipher.

Tanslation: very cute drawmack!

with a key of 3, which is the classic ceaser cipher, very cute marque
drawmack is offline   Reply With Quote
Old 11-09-2003, 09:29 PM   #8
procoder
Freelance Programmer
 
Join Date: Dec 2002
Posts: 102
Well done drawmack

One suggestion

change

PHP Code:
function ceaser($clicks,$input) {
to

PHP Code:
function ceaser($input, $clicks = 13) {
Just incase no clicks specified, it uses rot13 actully.

so people can just do ceaser('bla') for defautt cypher
procoder is offline   Reply With Quote
Old 11-09-2003, 11:13 PM   #9
drawmack
Computers can do that?
 
drawmack's Avatar
 
Join Date: Apr 2003
Location: Pocono Mtns PA
Posts: 3,268
Quote:
Originally posted by procoder
PHP Code:
function ceaser($input, $clicks = 13) {
Just incase no clicks specified, it uses rot13 actully.
so people can just do ceaser('bla') for defautt cypher [/b]
Actually since the original Ceaser cipher used 3 clicks then shouldn't I set the clicks to 3 if I'm going to default it?

The reason I didn't default it is because it's more flexible this way. I can define the default clicks in the calling code without changing the function at all.
drawmack is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 05:55 PM.






Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.