Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Caesar Cipher

by kcott (Abbot)
on Aug 05, 2013 at 07:01 UTC ( #1047870=note: print w/ replies, xml ) Need Help??


in reply to Caesar Cipher

G'day Iggyroo2000,

Welcome to the monastery.

"I'm new to using perl, but I've been instructed to create a Caesar Cipher to decode a sentence. From what I've heard is that there is a function in perl that allows you to create a Caesar cipher."

That seems somewhat vague with respect to what you're actually trying to achieve. I'm unsure whether you have data you wish to encrypt, decrypt or both. Plugging "Caesar Cipher" into your favourite search engine should produce pages of results: perhaps a little (more) research will help clarify what you're really after.

There's no caesar_cipher() function, as such, in Perl. There are some CPAN modules that may do what you want. A quick search found Text::Cipher::KeywordAlphabet and Crypt::Caesar: I leave you to look for others.

"I know its a 10 shift to the right."

I agree with what ww said about this (above), i.e. the shift amount is arbitrary and can be either left or right. Perhaps you meant that in your specific case that's what you have to deal with.

Anyway, your post piqued my interest and I wrote a short script that both encrypts and decrypts, shifting an arbitrary amount either left or right (using all printable ASCII characters: ' ' [space: chr(32)] to '~' [tilde: chr(126)]). It's actually fairly straightforward but, as you've said how "very new" you are to Perl, I'll provide some documentation links at the end. The commandline interface is fairly clunky: if you use this script, you'll probably want to spend some time on improving that.

#!/usr/bin/env perl -l use strict; use warnings; if (@ARGV != 4) { print "Usage: $0 text encrypt/decrypt(1|0) ", 'right_shift/left_shift(1|0) positions_to_shift'; print "(Note: when decrypting, '*_shift' and 'positions_to_shift' +", 'is what was used for the original encryption!)'; exit; } my @chars = map { "\Q@{[chr]}\E" } 32 .. 126; my ($input, $encrypt, $right, $pos) = @ARGV; $pos %= @chars; my $plain_chars = join '' => @chars; my $cipher_chars = $right ? join('' => @chars[$pos .. $#chars, 0 .. $pos - 1]) : join('' => @chars[$#chars - $pos + 1 .. $#chars, 0 .. $#chars - +$pos]); print 'Transform: ', ($encrypt ? 'encrypt' : 'decrypt'), ' -- Shift: ', ($right ? 'right' : 'left'), ' -- Positions: ', $pos; if ($encrypt) { my $ciphertext; eval "(\$ciphertext = \$input) =~ y/$plain_chars/$cipher_chars/"; print "Plaintext: $input"; print "Ciphertext: $ciphertext"; } else { my $plaintext; eval "(\$plaintext = \$input) =~ y/$cipher_chars/$plain_chars/"; print "Ciphertext: $input"; print "Plaintext: $plaintext"; }

Documentation you may find useful:

Here's a few sample runs:

$ pm_caesar_cipher.pl def 1 1 3 Transform: encrypt -- Shift: right -- Positions: 3 Plaintext: def Ciphertext: ghi
$ pm_caesar_cipher.pl ghi 0 1 3 Transform: decrypt -- Shift: right -- Positions: 3 Ciphertext: ghi Plaintext: def
$ pm_caesar_cipher.pl def 1 0 3 Transform: encrypt -- Shift: left -- Positions: 3 Plaintext: def Ciphertext: abc
$ pm_caesar_cipher.pl abc 0 0 3 Transform: decrypt -- Shift: left -- Positions: 3 Ciphertext: abc Plaintext: def
$ pm_caesar_cipher.pl 'The quick brown fox jumped over the lazy dog.' +1 1 145 Transform: encrypt -- Shift: right -- Positions: 50 Plaintext: The quick brown fox jumped over the lazy dog. Ciphertext: ';8RDH<6>R5EBJAR9BKR=H@C87RBI8ERG;8R?4MLR7B:`
$ pm_caesar_cipher.pl "';8RDH<6>R5EBJAR9BKR=H@C87RBI8ERG;8R?4MLR7B:\`" + 0 1 145 Transform: decrypt -- Shift: right -- Positions: 50 Ciphertext: ';8RDH<6>R5EBJAR9BKR=H@C87RBI8ERG;8R?4MLR7B:` Plaintext: The quick brown fox jumped over the lazy dog.
$ pm_caesar_cipher.pl Usage: ./pm_caesar_cipher.pl text encrypt/decrypt(1|0) right_shift/lef +t_shift(1|0) positions_to_shift (Note: when decrypting, '*_shift' and 'positions_to_shift' is what was + used for the original encryption!)

-- Ken


Comment on Re: Caesar Cipher
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1047870]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2014-09-20 03:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (152 votes), past polls