Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.25 KB | None | 0 0
  1. #!usr/bin/perl
  2. # Harrison Pincket
  3. # July 19, 2011
  4. # Sandorf Cipher
  5.  
  6.  
  7. #Variables
  8. # FUTURE UPDATES
  9. #######################
  10. # Random Key Generation
  11. # Input a particular key
  12. # Different Sized Square
  13. # Visualize
  14. #######################
  15.  
  16.  
  17. sub rotate_coordinate {
  18.     #Will rotate one coordinate of the key clockwise 90'
  19.     #Takes the current_x/y values and rotates them.
  20.     $past_x=$current_x;
  21.     $past_y=$current_y;
  22.     $current_x=7-$past_y;
  23.     $current_y=$past_x;
  24. }
  25.  
  26. sub rotate_key {
  27.     #Will rotate all the values in the Key arrays. (calls the rotate_coordinate function)
  28.     #It then re-orders the coordinates for the new top to bottom order.
  29.     print "Beginning Rotation\n";
  30.     my @indexs=(0..$#key_x);
  31.     foreach $indexv (@indexs) {
  32.         $current_x=$key_x[$indexv];
  33.         $current_y=$key_y[$indexv];
  34.         rotate_coordinate;
  35.         $key_x[$indexv]=$current_x;
  36.         $key_y[$indexv]=$current_y;
  37.     }
  38.     my @sortedIndices= sort coord_sort 0..$#key_y;
  39.     @key_x=@key_x[@sortedIndices];
  40.     @key_y=@key_y[@sortedIndices];
  41.     print "Key: Rotated\n"
  42. }
  43.  
  44. sub coord_sort {
  45.     #This is the sort algorithm for the coordinates. First y, then x.
  46.     $key_y[$a] <=> $key_y[$b] || $key_x[$a] <=> $key_x[$b];
  47. }
  48. sub place_in_line ($$$$) {
  49.     #Will convert the three coordinates into a number placement
  50.     my $index=36*$_[2]+6*($_[1]-1)+$_[0]-1;
  51.     $end_array[$index]= $_[3];
  52. }
  53. sub get_plain_text {
  54.     print "Please enter your text to encode:\n";
  55.     $start=lc(<STDIN>);
  56.     $start=~ s/\s+//g;
  57.     @alphabet=a..z;
  58.     while ((length($start) % 36) != 0){
  59.         $start= $start . $alphabet[rand @alphabet];
  60.     }  
  61. }
  62.  
  63. get_plain_text
  64. $boxes=int(length($start)/36 +.5);
  65. #print $boxes;
  66. @start=unpack("(A36)*", $start);
  67. @key_x=(2,4,6,5,3,2,5,6,4);
  68. @key_y=(1,1,1,2,3,4,4,5,6);
  69. @end_array=();
  70. $current_z=0;
  71.  
  72. foreach $start (@start) {
  73.     @box_array=split(//,$start);
  74.     $start_index=0;
  75.     print "New Box!\n";
  76.     until ($start_index>35) {
  77.         if ($start_index==9 or $start_index ==18 or $start_index==27) {
  78.             rotate_key;
  79.         }
  80.        
  81.         foreach $value (0..$#key_x) {
  82.             $current_x=$key_x[$value];
  83.             $current_y=$key_y[$value];
  84.             place_in_line($current_x,$current_y,$current_z,$box_array[$start_index]);
  85.             print "@end_array\n";
  86.             $start_index++;
  87.         }
  88.     }
  89.     $current_z+=1;
  90.     rotate_key;
  91. }
  92. foreach $char (@end_array) {
  93.     print "$char";
  94. }
  95. print "\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement