-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathaes
executable file
·57 lines (47 loc) · 1.17 KB
/
aes
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
#!/usr/bin/perl
#
# quick aes encryption/decryption wih hex strings
# -samy kamkar
use strict;
use Crypt::OpenSSL::AES;
die "usage: $0 [-a (no \\n)] [-d | -e] <0xkey> <0xmessage>\n" unless @ARGV >= 2;
my $crypt;
my $nolf = grep { splice(@ARGV, $_, 1) if $ARGV[$_] eq '-a' } 0 .. $#ARGV;
if ($ARGV[0] eq "-d" || $ARGV[0] eq "-e")
{
$crypt = shift;
}
my ($key, $msg) = @ARGV;
$key =~ s/^0x//;
$msg =~ s/^0x//;
$key =~ s/(..)/pack("H2", $1)/eg;
$msg =~ s/(..)/pack("H2", $1)/eg;
if (length($msg) % 16 != 0)
{
print STDERR "padding with " . (16 - (length($msg) % 16)) . " zeros\n\n";
$msg .= "\x00" x (16 - (length($msg) % 16));
}
#print length($key).$/;
#print "$key\n";
my $cipher = new Crypt::OpenSSL::AES($key);
my $encrypted = $cipher->encrypt($msg);
if ($crypt eq "-e") {
print unpack("H*", $encrypted);
print "\n" unless $nolf;
}
elsif (!$crypt)
{
print "Encrypted: $encrypted\n";
print "Encrypted: " . unpack("H*", $encrypted) . "\n";
}
my $decrypted = unpack("H*", $cipher->decrypt($msg));
if ($crypt eq "-d")
{
print $decrypted;
print "\n" unless $nolf;
}
elsif (!$crypt)
{
print "Decrypted: " . pack("H*", $decrypted) . "\n";
print "Decrypted: $decrypted\n";
}