Advertisement
akuji_1993

Vigenère cipher

Mar 6th, 2014
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.28 KB | None | 0 0
  1.  
  2. #!/usr/bin/perl
  3. use warnings;
  4.  
  5. #Input arguments
  6. our $key = $ARGV[0];    #REDDIT
  7. our $text = $ARGV[1];   #TODAYISMYBIRTHDAY
  8.  
  9. #Possible Values
  10. our %abc =
  11. (
  12.     A => 0,
  13.     B => 1,
  14.     C => 2,
  15.     D => 3,
  16.     E => 4,
  17.     F => 5,
  18.     G => 6,
  19.     H => 7,
  20.     I => 8,
  21.     J => 9,
  22.     K => 10,
  23.     L => 11,
  24.     M => 12,
  25.     N => 13,
  26.     O => 14,
  27.     P => 15,
  28.     Q => 16,
  29.     R => 17,
  30.     S => 18,
  31.     T => 19,
  32.     U => 20,
  33.     V => 21,
  34.     W => 22,
  35.     X => 23,
  36.     Y => 24,
  37.     Z => 25
  38. );
  39.  
  40. #Process arguments
  41. our @text_chars = ();
  42. our @key_chars = ();
  43. our @crypted_chars = ();
  44.  
  45. #Read arguments
  46. &process_arguments($key, $text);
  47.  
  48. sub process_arguments(){
  49.  
  50.     #Get Key Length
  51.     @key_chars = split(//, $_[0]);
  52.     my $key_length = scalar @key_chars;
  53.    
  54.     #Get Text Length
  55.     @text_chars = split(//, $_[1]);
  56.     my $text_length = scalar @text_chars;
  57.  
  58.    
  59.     #Extend Key to Text Length
  60.     while($key_length < $text_length){
  61.         push(@key_chars, @key_chars);
  62.         $key_length = scalar @key_chars;
  63.     }
  64.    
  65.     #Turn hash around
  66.     my %numbers = reverse %abc;
  67.    
  68.     #Get crypted chars
  69.     my $i = 0;
  70.     for ($i = 0; $i < $text_length; $i++) {
  71.         my $a = $abc{uc($key_chars[$i])};
  72.         my $b = $abc{uc($text_chars[$i])};
  73.        
  74.         my $sum_mod = ($a+$b)%26;
  75.         $crypted_chars[$i] = $numbers{$sum_mod};
  76.     }
  77.    
  78.     print @crypted_chars;
  79.  
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement