左に次の文字列があり、右にエンコードされた値があります。
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
次のように、デコード手順(uudecode -> キーを使用した XOR)を複製しようとしました。
#!/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));
}
13 文字で構成される文字列に到達するまで、すべてがうまく機能します。
# 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Ó
エンコードされたすべてのデータをデコードする方法について何か考えはありますか? ありがとう!
わかりました、HEXでキーをブルートフォースすることを自分で考え出しました。このキー3cb37efae7f4f376ebbd76cdfce7391e9ed9cee4cfceb4b3
は、暗号化されたすべてのデータをデコードします。
ソリューション、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");
出力:
$ 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