Iggyroo2000 has asked for the wisdom of the Perl Monks concerning the following question:
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. I'm not quite sure where to find information on how to use the function or what is required to use the function. I know its a 10 shift to the right...can someone help me figure out where to start. Please know I'm very new, so please try to explain what the different steps are for. Thanks
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Caesar Cipher
by moritz (Cardinal) on Aug 04, 2013 at 19:43 UTC | |
a Caesar Cipher just substitutes each character for a fixed replacement. The easiest way to do that in Perl is with the tr operator:
| [reply] [d/l] |
Re: Caesar Cipher
by kcott (Archbishop) on Aug 05, 2013 at 07:01 UTC | |
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.
Documentation you may find useful:
Here's a few sample runs:
-- Ken | [reply] [d/l] [select] |
Re: Caesar Cipher
by ww (Archbishop) on Aug 04, 2013 at 20:48 UTC | |
In the computer world, it's often called ROT13 for rotate 13 positions. moritz' example above provides an example. I don't know about a "10 shift to the right" -- certainly, as indicated in para 1, that would be a valid way to implementing a Ceasar Cipher... and so would a 10 shift to the left but (IMO) both would make it easier to err if "encoding" manually. | [reply] |
by AnomalousMonk (Archbishop) on Aug 05, 2013 at 19:50 UTC | |
[reply] | |
Re: Caesar Cipher
by marinersk (Priest) on Aug 05, 2013 at 15:58 UTC | |
However, since this smells like like a homework assignment, and you are new to Perl, I would be inclined to suggest something a little more generic -- use an array. It would be slower, but since you would be building the underlying mechanisms, you would understand its workings more thoroughly. I know you are essentially asking for theoretical guidance, but have you done any work on this so far? Like, for example, the code which collects the text to be so ciphered? For example -- intentionally silly:
| [reply] [d/l] |