xor-base64.pl - encrypt/decrypt a string with XOR and base64
Here is a little tool written in perl, which XOR a string against a key and outputs the result base64 encoded and vice versa.
Encrypt
./xor-base64.pl -e foobar -k deadbeef
[+] Cleartext: foobar [+] Key: deadbeef [+] Hex: 020a0e060317 [+] Ciphertext: AgoOBgMX |
Decrypt
./xor-base64.pl -d AgoOBgMX -k deadbeef
[+] Ciphertext: AgoOBgMX [+] Hex: 020a0e060317 [+] Key: deadbeef [+] Cleartext: foobar |
And here is the code
#!/usr/bin/perl
use MIME::Base64; use Getopt::Long; use strict; ###################################### # variables ###################################### my $cstring; my $cipher; my @carray; my @key; ###################################### # process commandline argmuments ###################################### my ($help,$encrypt,$decrypt,$ikey); USAGE() if (@ARGV < 1 or ! GetOptions( 'help|h' => $help, 'version|v' => &VERSION, 'decrypt|d=s' => $decrypt, 'encrypt|e=s' => $encrypt, 'key|k=s' => $ikey) || ! defined $ikey || defined $help); DECRYPT() if defined ($decrypt && $ikey); ENCRYPT() if defined ($encrypt && $ikey); ###################################### # sub USAGE ###################################### sub USAGE { print "usage: ./xorbase64.pl [-h] [-k <key>] [-e <string>] [-d <string>]\n"; print "\n"; print "Options:\n"; print "\t-h, --help tprint this help\n"; print "\t-d, --decrypt tdecrypt xor/base64 encoded string, needs -k\n"; print "\t-e, --encrypt tencrypt string with xor/base64, needs -k\n"; print "\t-k, --key tkey for encrypting/decrypting, needs -e or -d\n"; print "\t-v, --version tversion\n"; print "\n"; exit 0; } ###################################### # sub VERSION ###################################### sub VERSION { print "Version 0.1 (25.12.2012)\n"; exit 0; } ###################################### # sub XOR ###################################### sub XOR { my $xor = $_[0]^$_[1]; } ###################################### # ENCRYPT ###################################### sub ENCRYPT { # split string as hex into array @carray for (my $h=0;$h<=length($encrypt)-1;$h++) { my $chex = substr($encrypt,$h,1); push (@carray,ord($chex)); } # split key into array @key for (my $k=0;$k<=length($ikey)-1;$k++) { my $key = substr($ikey,$k,1); push (@key,ord($key)); } # encrypting/decrypting string for (my $i=0;$i<=@carray-1;$i++) { $cstring .= chr(XOR($key[$i],$carray[$i])); } # convert to hex & base64 encode my $hex = join '', unpack "H*", $cstring; my $cipher = encode_base64($cstring); # Output print "[+] Cleartext: $encrypt\n"; print "[+] Key: $ikey\n"; print "[+] Hex: $hex\n"; print "[+] Ciphertext: $cipher"; exit 0; } ###################################### # DECRYPT ###################################### sub DECRYPT { # base64 decode & convert to hex my $cipher = decode_base64($decrypt); my $hex = join '', unpack "H*", $cipher; # split string as hex into array @carray for (my $h=0;$h<=length($hex)-1;$h+=2) { my $chex = substr($hex,$h,2); push (@carray,hex($chex)); } # split key into array @key for (my $k=0;$k<=length($ikey)-1;$k++) { my $key = substr($ikey,$k,1); push (@key,ord($key)); } # encrypting/decrypting string for (my $i=0;$i<=@carray-1;$i++) { $cstring .= chr(XOR($key[$i],$carray[$i])); } # Output print "[+] Ciphertext: $decrypt\n"; print "[+] Hex: $hex\n"; print "[+] Key: $ikey\n"; print "[+] Cleartext: $cstring\n"; exit 0; } #EOF |
Comments
Display comments as Linear | Threaded