Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
Keep It Simple, Stupid
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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


In reply to Re: Caesar Cipher by kcott
in thread Caesar Cipher by Iggyroo2000

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (16)
    As of 2014-04-16 16:31 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      April first is:







      Results (432 votes), past polls