I was browsing here and found this Rot13 Challenge.

Rot13 Encryption is performed by shifting the letters 13 characters to the right. A becomes N, B becomes O. The cool thing about this is an encrypted string can be decrypted by using the same algorithm. IOW, the A becomes N, N becomes A.

my first thought was to convert the characters to their equivalent ascii number, shift it, then return the ascii character.

#!/usr/bin/perl use strict; use warnings; use 5.010001; my $input; say "Please Enter String a string to be encrypted or decrypted"; $input = <>; chomp $input; my @ascii_char; my $char; while($input =~ m/(.)/g) { push @ascii_char, ord $1; } say @ascii_char; open my $ENCRYPTED_STRING, '>', \my $encrypted_string; foreach( @ascii_char ) { if ($_ >= 97 and $_ <= 122) { if ((122 - $_) <13 ){ $_ = 97 + (13 - (123 - $_)); } else { $_ += 13; } } elsif ($_ >= 65 and $_ <= 90) { if ((90 - $_) < 13 ){ $_ = 65 + (13 - (91 - $_)); } else { $_ += 13; } } print $ENCRYPTED_STRING chr; } close $ENCRYPTED_STRING; say $encrypted_string;

Then I figured why not use the translate function...

#!/usr/bin/perl use strict; use warnings; use 5.010001; say 'Please Enter a String to be Encrypted or Decrypted'; my $input = <>; chomp $input; $input =~ tr/abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/nopq +rstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM/; say $input;

Considerably shorter.

So the challenge is this:

Any takers?


In reply to Rot13 Challenge by PyrexKidd

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.