Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: Caesar Cipher

by kcott (Canon)
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:

$ def 1 1 3 Transform: encrypt -- Shift: right -- Positions: 3 Plaintext: def Ciphertext: ghi
$ ghi 0 1 3 Transform: decrypt -- Shift: right -- Positions: 3 Ciphertext: ghi Plaintext: def
$ def 1 0 3 Transform: encrypt -- Shift: left -- Positions: 3 Plaintext: def Ciphertext: abc
$ abc 0 0 3 Transform: decrypt -- Shift: left -- Positions: 3 Ciphertext: abc Plaintext: def
$ '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:`
$ "';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.
$ Usage: ./ 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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1047870]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2017-03-26 19:47 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (315 votes). Check out past polls.