-
Notifications
You must be signed in to change notification settings - Fork 34
/
cbc+xor_file_encrypter.pl
205 lines (147 loc) · 4.68 KB
/
cbc+xor_file_encrypter.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/perl
# Author: Trizen
# Date: 18 March 2022
# https://github.com/trizen
# A simple file encryption cihpher, using XOR with SHA-512 of the key and substring shuffling.
# WARNING: should NOT be used for encrypting real-world data.
# See also:
# https://en.wikipedia.org/wiki/Block_cipher
# https://en.wikipedia.org/wiki/XOR_cipher
use 5.020;
use strict;
use warnings;
use experimental qw(signatures);
binmode(STDOUT, ':raw');
package SimpleXORCipher {
require Digest::SHA;
sub new ($class, %opt) {
$opt{rounds} ||= 1;
if (!defined($opt{key})) {
die "Undefined key parameter";
}
if ($opt{rounds} <= 0) {
die "Number of rounds must be > 0";
}
$opt{key} = Digest::SHA::sha512($opt{key});
bless \%opt, $class;
}
sub encrypt ($self, $str) {
my $key = $self->{key};
$str ^= $key;
my $i = my $l = length($str);
for my $k (1 .. $self->{rounds}) {
$str =~ s/(.{$i})(.)/$2$1/sg while (--$i > 0);
$str ^= Digest::SHA::sha512($key . $k);
$str =~ s/(.{$i})(.)/$2$1/sg while (++$i < $l);
$str ^= Digest::SHA::sha512($k . $key);
}
return $str;
}
sub decrypt ($self, $str) {
my $key = $self->{key};
my $i = my $l = length($str);
for my $k (reverse(1 .. $self->{rounds})) {
$str ^= Digest::SHA::sha512($k . $key);
$str =~ s/(.)(.{$i})/$2$1/sg while (--$i > 0);
$str ^= Digest::SHA::sha512($key . $k);
$str =~ s/(.)(.{$i})/$2$1/sg while (++$i < $l);
}
$str ^= $key;
return $str;
}
sub cbc_encrypt ($crypt, $iv, $result, $blocks) {
my ($i, $r) = ($$iv, $$result);
foreach (@$blocks) {
$r .= $i = $crypt->encrypt($i ^ $_);
}
($$iv, $$result) = ($i, $r);
}
sub cbc_decrypt ($crypt, $iv, $result, $blocks) {
my ($i, $r) = ($$iv, $$result);
foreach (@$blocks) {
$r .= $i ^ $crypt->decrypt($_);
$i = $_;
}
($$iv, $$result) = ($i, $r);
}
sub generate_iv ($self) {
my $iv = Digest::SHA::sha512($self->{key});
foreach my $i (1 .. $self->{rounds}) {
$iv = Digest::SHA::sha512(($i % 2 == 0) ? $iv : scalar(reverse($iv)));
}
return $iv;
}
}
use constant {BUFFER_SIZE => 1024 * 10,};
sub encrypt_file ($file, $key) {
my $crypt = SimpleXORCipher->new(key => $key);
my $iv = $crypt->generate_iv;
open(my $fh, '<:raw', $file)
or die "can't open file <<$file>> for reading: $!";
my $size = -s $file;
$crypt->cbc_encrypt(\$iv, \(my $size_enc), [pack("N*", $size)]);
print $size_enc;
my $key_size = length($crypt->{key});
while (read($fh, (my $buffer), BUFFER_SIZE)) {
my @blocks = unpack("(a$key_size)*", $buffer);
$crypt->cbc_encrypt(\$iv, \(my $result), \@blocks);
print $result;
}
close $fh;
}
sub decrypt_file ($file, $key) {
my $crypt = SimpleXORCipher->new(key => $key);
my $iv = $crypt->generate_iv;
my $key_size = length($crypt->{key});
open(my $fh, '<:raw', $file)
or die "can't open file <<$file>> for reading: $!";
read($fh, (my $size), $key_size);
$crypt->cbc_decrypt(\$iv, \(my $size_dec), [$size]);
$size = unpack("N*", substr($size_dec, 0, 4));
my $dec_size = 0;
while (read($fh, (my $buffer), BUFFER_SIZE)) {
my @blocks = unpack("(a$key_size)*", $buffer);
$crypt->cbc_decrypt(\$iv, \(my $result), \@blocks);
$dec_size += $key_size * scalar(@blocks);
if ($dec_size > $size) {
print substr($result, 0, (scalar(@blocks) - 1) * $key_size, '');
print substr($result, 0, $size % $key_size);
last;
}
else {
print $result;
}
}
close $fh;
}
sub help ($exit_code = 0) {
print <<"EOT";
usage: $0 [options] [input file]
options:
-k --key=s : encryption/decryption symmetric key
-d --decrypt : decryption mode
-h --help : print this message
example:
# Encrypt file
perl $0 -k=foo msg.txt > msg.enc
# Decrypt file
perl $0 -d -k=foo msg.enc > msg.dec
EOT
exit($exit_code);
}
use Getopt::Long qw(GetOptions);
my $key = undef;
my $decrypt = 0;
GetOptions(
"d|decrypt" => \$decrypt,
"key=s" => \$key,
"h|help" => sub { help(0) },
)
or die("Error in command line arguments\n");
my $input_file = $ARGV[0] // help(2);
if ($decrypt) {
decrypt_file($input_file, $key);
}
else {
encrypt_file($input_file, $key);
}