-
Notifications
You must be signed in to change notification settings - Fork 34
/
one-time_pad.pl
executable file
·75 lines (55 loc) · 1.51 KB
/
one-time_pad.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/perl
# Daniel "Trizen" Șuteu
# License: GPLv3
# Date: 13 November 2016
# https://github.com/trizen
# One-time pad symmetric encryption, where the key is pseudo-randomly generated from a given seed.
# See also:
# https://en.wikipedia.org/wiki/One-time_pad
#---------------------------------------------------
# !!! WARNING !!!
#---------------------------------------------------
# This program is just a proof-of-concept.
# Do NOT use this program to encrypt sensitive data!
#---------------------------------------------------
use 5.010;
use strict;
use warnings;
use Getopt::Std qw(getopts);
my %opts;
getopts('s:h', \%opts);
use constant {
READ_SIZE => 2 * 1024**2, # 2 MB
};
sub usage {
warn "\n[ERROR]: ", @_, "\n\n" if @_;
print <<"USAGE";
usage: $0 [options] [<input] [>output]
options:
-s SEED : random seed
example:
$0 -s 42 < input.txt > output.dat
USAGE
exit 1;
}
$opts{h} && usage();
encode_file(
in_fh => \*STDIN,
out_fh => \*STDOUT,
seed => defined($opts{s}) ? $opts{s} : usage("No seed specified!"),
);
sub generate_key {
my ($length) = @_;
pack('C*', map { int(rand(256)) } 1 .. $length);
}
sub encode_file {
my %args = @_;
srand($args{seed});
while (1) {
my $len = read($args{in_fh}, my ($chunk), READ_SIZE);
my $key = generate_key($len);
print {$args{out_fh}} $chunk ^ $key;
last if $len != READ_SIZE;
}
return 1;
}