Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Rot13 Challenge

by PyrexKidd (Monk)
on Feb 10, 2011 at 01:01 UTC ( [id://887321]=perlquestion: print w/replies, xml ) Need Help??

PyrexKidd has asked for the wisdom of the Perl Monks concerning the following question:

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:

  • Create the most succinct version
  • Create the most obfuscated version.

Any takers?

Replies are listed 'Best First'.
Re: Rot13 Challenge
by eyepopslikeamosquito (Archbishop) on Feb 10, 2011 at 05:01 UTC
Re: Rot13 Challenge
by Anonymous Monk on Feb 10, 2011 at 01:51 UTC
    $ perl -pe'y/A-Za-z/N-ZA-Mn-za-m/' file
      my $input; print "Please Enter String to be encrypted or decrypted"; $input = <>; chomp $input; my @asciich; while($input=~ m/(.)/g) {push @asciich,ord ($1);} my $count=scalar(@asciich); print "Ascii values -----> @asciich\n"; for(my $i=0;$i<$count;$i++) { if (($asciich[$i] >= 65 && $asciich[$i] <= 77)|| ($asciich[$i] >= 97 & +& $asciich[$i] <= 109)) { $asciich[$i] = $asciich[$i]+13; } else {$asciich[$i] = $asciich[$i] - 13;} } print "Encrypted values -----> @asciich\n"; foreach(my $i=0;$i<$count;$i++) { $asciich[$i]=chr($asciich[$i]);} print "encrypted output ------> @asciich\t";
Re: Rot13 Challenge
by ELISHEVA (Prior) on Feb 10, 2011 at 07:52 UTC

    Run instructions: place in a script, e.g. "myscript.pl" and run as follows: perl myscript.pl  "Hello World!"

    use strict; use warnings; package l_A'MN'm_N'mn; our %sbernpu; our @NETI; our $z_="what in the world is a"; $_ = q{ $sbernpu{$#NETI}=[$l_A'MN'm_N'mn'z_,"cevag","?"] }; $! = y|=-},\"-'| N-ZA-M{ } / n-za-m( ); @ \-|; #cleanup eval; # add hash entry push @{$sbernpu{$#NETI}}, ''; print "@{$sbernpu{$#NETI}}\n";
Re: Rot13 Challenge
by sundialsvc4 (Abbot) on Feb 10, 2011 at 18:22 UTC

    The “most obfuscatory” version... hmmm...

    Option Explicit
    ...

    ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://887321]
Approved by mr_mischief
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-25 17:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found