Programming and Scripting :: C question



I'm not in a hurry, a custom solution is what I feel like testing anyway..
One of the Monks on Perlmonks said we might like to try Algorithm::Combinatorics

This is XScode and so is supposed to be fast, and it does permutations with repetition so it doesn't generate redundant perms.
So you only put the desired ranges in once in the array @set.

It will generate all the perms with replacement, so I've put a regex filter in to skip perms containing 3 adjacent identical chars as Curaga wished.  Those perms still get generated but won't get tested on openssl.  Not sure which generation cost (redundant perms or perms with more than 3 adjacent chars) is more inefficient - a bit of math would work it it out I suppose.

EDIT:  this generates perms in the more or less expected alphabetic order so you can see just how far you are into a particular subset when you send it a SIGHUP to print.

I'd be interested in hearing how this seems to compare speed-wise.


Code Sample

#!/usr/bin/perl

# curagacrack2.pl
# Demonstration brute force password cracker #2 using use Algorithm::Combinatorics

use strict;
use warnings;
use Algorithm::Combinatorics qw(variations_with_repetition);

#==========================// SETTINGS // =========================


my @set = ( 'a'..'z', 'A'..'Z', '0'..'9' );

my $skip = 1; # filter: set to 1 to skip any perm containing 3 adjacent identical chars
my $printall = 0;  # set to 1 to print every perm
my $startlen;
my $endlen;

# Leave the following  commented out to do all length perms within @set

#~ $startlen = 3;  # begin processing with strings of length $startlen

#~ $endlen = 5;   # end processing with strings of length $endlen

#================================================================

my $perm = "";
my $k;
my $i;

sub print_perm {
print "Perm no. $k = $perm\n";
}



#================================// MAIN // =======================

$SIG{'HUP'} = 'print_perm';  # print current perm on receipt of sighup


$k = 0;
my $setlen = $#set + 1;

# defaults
unless (defined($startlen)) { $startlen = 1 }
unless (defined($endlen)) { $endlen = $setlen }

unless ($startlen >= 1) { die "Error: startlen must be >= 1" }
unless ($endlen >= $startlen) { die "Error: startlen must be <= endlen" }
unless ($endlen <= $setlen) { die "Error: endlen must be <= $setlen" }



foreach $i ($startlen..$endlen) {
my $p = variations_with_repetition(\@set, $i);
my $c;
while ( $c = $p->next) {
$k++;
$perm = join("", @$c);
if ($printall == 1) {
print "$k $perm\n";
}
if (($skip == 1) && ($perm =~ /(.)\1\1/)){ next }
system("echo $perm | 2>/dev/null openssl bf -pass stdin -d -in encryptedfile -out out.txt && file -b out.txt | grep -qE '^ASCII.+text'") == 0 && die "Found passphrase \"$perm\"";

}

}


FYI, custom solutions notwithstanding: I do like fooling around with Perl on this - nonetheless I just compiled a C prog called "combina".

This not surprisingly does appear to be faster than the Perl, but I was pleasantly surprised to see not as much faster as I might have expected.  Rough "tests" suggest something like 20-50% faster, but that's just trying a few short passwords and it will depend on what password and the order im which the perms are generated, so it might average out to not much difference (or not).  XScode is,  in fact, interfacing with C libaries via Perl as I understand(?)  it so, indirecly, it is a way of using C.

Code Sample
time combina -a -A -n -p 1 -k 5  |  ( while read PW; do if 2>/dev/null openssl bf -pass pass:$PW  -d -in encryptedfile -out out.txt; then if file -b out.txt | grep -qE '^ASCII.+text'; then echo "Found passphrase $PW"; exit; else continue; fi; fi; done )


This found the highly challenging (not)  password "zz" in 20 sec.

The Perl above took 25 sec over the same name space.  There was a greater difference with other passwords.

(I'll post combina.uci to the repo anyway.)

After some months of intense hacking away, and still not gotten even close, I decided to give up ;)

I was hoping I could find out the time needed to crack it, but it's way too good for that. I did prove it's really good though, and that it would take years even with a top notch comp.

Got me looking at some Perl modules I'd never looked at anyway, which was interesting.
Next Page...
original here.