I have the following strings at left with encoded values at right:
123456789012 ,\#8%-SM+"Q$[2C4?_\n
1234567890123 -\#8%-SM+"Q$[2C4?_SP \n
12345678901234 .\#8%-SM+"Q$[2C4?_S], \n
123456789012345 /\#8%-SM+"Q$[2C4?_S],,\n
1234567890123456 0\#8%-SM+"Q$[2C4?_S],,* \n
12345678901234567 1\#8%-SM+"Q$[2C4?_S],,**D \n
123456789012345678 2\#8%-SM+"Q$[2C4?_S],,**GA\n
1234567890123456789 3\#8%-SM+"Q$[2C4?_S],,**GA]P \n
12345678901234567890 4\#8%-SM+"Q$[2C4?_S],,**GA]]0 \n
123456789012345678901 5\#8%-SM+"Q$[2C4?_S],,**GA]]3^\n
1234567890123456789012 6\#8%-SM+"Q$[2C4?_S],,**GA]]3^_ \n
12345678901234567890123 7\#8%-SM+"Q$[2C4?_S],,**GA]]3^_(< \n
123456789012345678901234 8\#8%-SM+"Q$[2C4?_S],,**GA]]3^_(>'\n
I tried to replicate the decoding procedure (uudecode -> XOR with key) like this:
#!/usr/bin/perl
$key = pack("H*","3cb37efae7f4f376ebbd76cdfc");
print "Enter string to decode: ";
$str=<STDIN>;chomp $str; $str =~s/\\(.)/$1/g;
$dec = decode($str);
print "Decoded string value: $dec\n";
sub decode{ #Sub to decode
my ($sqlstr) = @_;
$cipher = unpack("u", $sqlstr);
$plain = $cipher^$key;
return substr($plain, 0, length($cipher));
}
All works well until I get to a string made of 13 characters:
# perl d.pl
Enter string to decode: -\#8%-SM+"Q$[2C4?_SP \n
Decoded string value: 1234567890123
# perl d.pl
Enter string to decode: .\#8%-SM+"Q$[2C4?_S], \n
Decoded string value: 1234567890123Ó
Any idea on how can I decode all the encoded datas? Thanks!
Ok, figured it out myself bruteforcing the key in HEX. This key 3cb37efae7f4f376ebbd76cdfce7391e9ed9cee4cfceb4b3
decodes all my encrypted data.
SOLUTION, cleaner code thanks to ikegami:
#!/usr/bin/perl
use strict;
use warnings;
sub deliteral {
my ($s) = @_;
$s =~ s/\\n/\n/g;
die "Unrecognised escape \\$1\n"
if $s =~ /(?<!\\)(?:\\{2})*\\([a-zA-Z0-9])/;
$s =~ s/\\(.)/$1/sg;
return $s;
}
sub uudecode {
return unpack 'u', $_[0];
}
sub decode {
my ($key, $cipher) = @_;
return substr($cipher^$key, 0, length($cipher));
}
my $key = pack('H*', '3cb37efae7f4f376ebbd76cdfce7391e9ed9cee4cfceb4b3');
print "Enter string to decode: ";
chomp( my $coded = <STDIN> );
my $cipher = uudecode(deliteral($coded));
my $plain = decode($key, $cipher);
print("Plain text: $plain\n");
Output:
$ perl deXOR.pl
Enter string to decode: ,\#8%-SM+"Q$[2C4?_\n
Plain text: 123456789012
$ perl deXOR.pl
Enter string to decode: 8\#8%-SM+"Q$[2C4?_S],,**GA]]3^_(>'\n
Plain text: 123456789012345678901234
Here is the solution to my question. I bruteforced the key HEX byte by byte.
#!/usr/bin/perl
use strict;
use warnings;
sub deliteral {
my ($s) = @_;
$s =~ s/\\n/\n/g;
die "Unrecognised escape \\$1\n"
if $s =~ /(?<!\\)(?:\\{2})*\\([a-zA-Z0-9])/;
$s =~ s/\\(.)/$1/sg;
return $s;
}
sub uudecode {
return unpack 'u', $_[0];
}
sub decode {
my ($key, $cipher) = @_;
return substr($cipher^$key, 0, length($cipher));
}
my $key = pack('H*', '3cb37efae7f4f376ebbd76cdfce7391e9ed9cee4cfceb4b3');
print "Enter string to decode: ";
chomp( my $coded = <STDIN> );
my $cipher = uudecode(deliteral($coded));
my $plain = decode($key, $cipher);
print("Plain text: $plain\n");