#!/usr/bin/perl

# Author:
# Philippe Bourdin, 23.05.2013
# http://www.Bourdin.ch/Philippe/

# encrypted file and cipher
$file = "test.tgz.gpg";
$cipher = "TWOFISH";

# the following depends on your langugage settings, check the GPG error message for a wrong passphrase
$error = "Falscher Schl"; # German for "wrong k"[ey] (reduced to the minimum!)
$error_start = 167;
$error_length = 13;

# possible parts of the passphrase, this can be extended further, see 'g' and 'h'
@as = ("My prefix");
@bs = ("", " ", "-", "_", "/");
@cs = ("let", "me", "think");
@ds = ("", "!", "...");
@es = ("1", "2", "3");
@fs = ("", ".");
#@gs = ("");
#@hs = ("");

# HINT: if less parts are needed, just comment out the corresponding 'foreach' loops

# iterate over parts
$percent = 0.0;
$step = 1.0 / (@as * @bs * @cs * @ds);
foreach $a (@as) {
foreach $b (@bs) {
foreach $c (@cs) {
foreach $d (@ds) {
	print " Testing: ".sprintf ("% 3.1f", $percent)." %\r";
	$percent += $step;
foreach $e (@es) {
foreach $f (@fs) {
#foreach $g (@gs) {
#foreach $h (@hs) {
	# combine passphrase from parts
	$PASS = $a.$b.$c.$d.$e.$f; #.$g.$h;
	# check passphrase
	$out = `gpg --cipher-algo $cipher --passphrase "$PASS" -d $file 2>&1`;
	# you could also use a Regular Expression, but comparing substrings is much faster
	if (substr ($out, $error_start, $error_length) ne $error) {
		# you got lucky! ;-)
		print "\n=> $PASS <= YES!\n";
		exit (0);
	}
#}
#}
}
}
}
}
}
}

print "\nNo luck today...\n";
exit (1);
