Warning: opendir(/var/www/html/web1/serendipity/plugins/serendipity_event_dpsyntaxhighlighter/sh/3.0.83.2/scripts/): failed to open dir: No such file or directory in /var/www/html/web1/serendipity/plugins/serendipity_event_dpsyntaxhighlighter/serendipity_event_dpsyntaxhighlighter.php on line 26

Warning: Invalid argument supplied for foreach() in /var/www/html/web1/serendipity/plugins/serendipity_event_dpsyntaxhighlighter/serendipity_event_dpsyntaxhighlighter.php on line 170
Skip to content

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
 

Trackbacks

No Trackbacks

Comments

Display comments as Linear | Threaded

No comments

Add Comment


To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA 1CAPTCHA 2CAPTCHA 3CAPTCHA 4CAPTCHA 5


Textile-formatting allowed
You can use [geshi lang=lang_name [,ln={y|n}]][/geshi] tags to embed source code snippets.
Form options
Imprint | Contact | Privacy Statement

Warning: opendir(/var/www/html/web1/serendipity/plugins/serendipity_event_dpsyntaxhighlighter/sh/3.0.83.2/scripts/): failed to open dir: No such file or directory in /var/www/html/web1/serendipity/plugins/serendipity_event_dpsyntaxhighlighter/serendipity_event_dpsyntaxhighlighter.php on line 26

Warning: Invalid argument supplied for foreach() in /var/www/html/web1/serendipity/plugins/serendipity_event_dpsyntaxhighlighter/serendipity_event_dpsyntaxhighlighter.php on line 170