Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!usr/bin/perl
- # Harrison Pincket
- # July 19, 2011
- # Sandorf Cipher
- #Variables
- # FUTURE UPDATES
- #######################
- # Random Key Generation
- # Input a particular key
- # Different Sized Square
- # Visualize
- #######################
- sub rotate_coordinate {
- #Will rotate one coordinate of the key clockwise 90'
- #Takes the current_x/y values and rotates them.
- $past_x=$current_x;
- $past_y=$current_y;
- $current_x=7-$past_y;
- $current_y=$past_x;
- }
- sub rotate_key {
- #Will rotate all the values in the Key arrays. (calls the rotate_coordinate function)
- #It then re-orders the coordinates for the new top to bottom order.
- print "Beginning Rotation\n";
- my @indexs=(0..$#key_x);
- foreach $indexv (@indexs) {
- $current_x=$key_x[$indexv];
- $current_y=$key_y[$indexv];
- rotate_coordinate;
- $key_x[$indexv]=$current_x;
- $key_y[$indexv]=$current_y;
- }
- my @sortedIndices= sort coord_sort 0..$#key_y;
- @key_x=@key_x[@sortedIndices];
- @key_y=@key_y[@sortedIndices];
- print "Key: Rotated\n"
- }
- sub coord_sort {
- #This is the sort algorithm for the coordinates. First y, then x.
- $key_y[$a] <=> $key_y[$b] || $key_x[$a] <=> $key_x[$b];
- }
- sub place_in_line ($$$$) {
- #Will convert the three coordinates into a number placement
- my $index=36*$_[2]+6*($_[1]-1)+$_[0]-1;
- $end_array[$index]= $_[3];
- }
- sub get_plain_text {
- print "Please enter your text to encode:\n";
- $start=lc(<STDIN>);
- $start=~ s/\s+//g;
- @alphabet=a..z;
- while ((length($start) % 36) != 0){
- $start= $start . $alphabet[rand @alphabet];
- }
- }
- get_plain_text
- $boxes=int(length($start)/36 +.5);
- #print $boxes;
- @start=unpack("(A36)*", $start);
- @key_x=(2,4,6,5,3,2,5,6,4);
- @key_y=(1,1,1,2,3,4,4,5,6);
- @end_array=();
- $current_z=0;
- foreach $start (@start) {
- @box_array=split(//,$start);
- $start_index=0;
- print "New Box!\n";
- until ($start_index>35) {
- if ($start_index==9 or $start_index ==18 or $start_index==27) {
- rotate_key;
- }
- foreach $value (0..$#key_x) {
- $current_x=$key_x[$value];
- $current_y=$key_y[$value];
- place_in_line($current_x,$current_y,$current_z,$box_array[$start_index]);
- print "@end_array\n";
- $start_index++;
- }
- }
- $current_z+=1;
- rotate_key;
- }
- foreach $char (@end_array) {
- print "$char";
- }
- print "\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement