-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathencrypt.pl
47 lines (38 loc) · 1.36 KB
/
encrypt.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
use strict;
use Crypt::CBC;
use Crypt::Rijndael;
use MIME::Base64 qw(encode_base64);
print <<EOU and exit 0 unless @ARGV == 1;
$0 key
Reads STDIN, encrypts it with the given key, and writes the result to STDOUT
EOU
my ($key) = @ARGV;
my $line_length = 57; # Base64 encoding fills this out to 76
my $cipher = new Crypt::CBC( -key => $key, -cipher => "Crypt::Rijndael" );
$cipher->start("encrypting");
my $binary = undef;
my $out_line = "";
for my $in_line (<STDIN>) {
$binary = $cipher->crypt($in_line);
# crypt is bursty; we will not get output on every line
next unless defined $binary;
# Base64 encode for easy transport
$out_line .= $binary;
while (length($out_line) >= $line_length) {
print STDOUT encode_base64(substr($out_line, 0, $line_length));
$out_line = substr($out_line, $line_length);
}
}
# we're done with input; flush the output
$binary = $cipher->finish();
if (defined $binary) {
$out_line .= $binary;
while (length($out_line) >= $line_length) {
print STDOUT encode_base64(substr($out_line, 0, $line_length));
$out_line = substr($out_line, $line_length);
}
}
# there might be a half line left over
if (length($out_line) > 0) {
print STDOUT encode_base64($out_line);
}