DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Rot N

02.09.2006
| 1101 views |
  • submit to reddit
        
#!/usr/bin/perl -w
use strict;

my @file;
my @ascii;
my $val;
my $rot;
my $i;

$rot = 0;
if ($#ARGV >= 0) {
   if ($ARGV[0] >= 0) {
      $rot = $ARGV[0];
   } else {
      $rot = $ARGV[0] + 26;
   }
}

chop(@file = <STDIN>);

print "Rot:   $rot\n";
print "Lines: $#file\n";

print "-" x 80;

for ($i = 0; $i <= $#file; $i++) {

   @ascii = unpack("C*", $file[$i]);

   foreach $val (@ascii) {
      # uppercase
      $val = ($val + $rot - 65)%26 + 65   if ($val >= 65 && $val <= 90);
      # lowercase
      $val = ($val + $rot - 97)%26 + 97   if ($val >= 97 && $val <= 122);
   }

   print pack("C*", @ascii), "\n";
}

print "-" x 80, "\n";