Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Encrypt:
- #!/usr/bin/perl
- if(@ARGV == 0)
- {
- print "Usage: $0 <num recipients>\n";
- print "Creates a base 64 encoded set of encrypted data which when xor'ed together, produces the secret text\n";
- exit 1;
- }
- my $num_rec = shift @ARGV;
- print "Enter secret: \n";
- my $secret = <STDIN>;
- my @secret = map { ord($_) } (split(//,$secret));
- my @p1;
- my @p2 = @secret;
- while($num_rec > 1)
- {
- @p1 = @p2;
- @p2 = gen_random(scalar @p1);
- my @xor = xorit(\@p1,\@p2);
- print encode(@xor)."\n";
- $num_rec --;
- }
- print encode(@p2)."\n";
- sub gen_random
- {
- my $l = shift @_;
- my @a;
- while(--$l >= 0) { push @a, int(rand(256)); }
- @a;
- }
- sub xorit
- {
- my ($p1, $p2) = @_;
- my @a;
- for(my $i = 0; $i < @$p1; $i++)
- {
- push @a, $p1->[$i] ^ $p2->[$i];
- }
- return @a;
- }
- sub encode
- {
- join(" ",@_);
- }
- ------------
- Decrypt:
- #!/usr/bin/perl
- print "Enter code(s) and a blank line to stop\n";
- while(1)
- {
- my $d = <STDIN>;
- if($d =~ /^\s*$/s)
- {
- last;
- }
- $i = 0;
- while($d =~ /(\d+)/g)
- {
- $v[$i] = (int($v[$i]) ^ int($1));
- $i++;
- }
- }
- print "\nAns\n";
- print join(" ", @v);
- print "\n";
- print map { if($_ > ord(' ') && $_ < ord('~') || $_ == ord("\n")) {chr($_)} else { '.';} } @v;
- print "\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement