Hello I'm developing a program in Java for testing purposes. In the first section I create a hash code from a password using the MessageDigest method. In the second section I want to create hashes from a series of predefined strings (AAAAA to ABAAA). The program will create a hash for AAAAA and compare it with the used password. Then AAAAB, AAAAC ... AAABA etc. My problem is how to implement this in Java. I know that in C/C++ I would use pointers to do it. But how am I supposed to do this in Java? Is there a method for this kind of stuff? A way would be to use nested fors but that wouldn't be the best way to implement it. Any ideas or directions?
Thanks in advance.
10 replies to this topic
#1
Posted 30 October 2011 - 02:42 AM
|
|
|
#2
Posted 30 October 2011 - 09:50 AM
Hello again guys, I solved the problem, here's the code I used:
Cheers! :thumbup1:
String validChars="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
int validLength=validChars.length();
int C1,C2,C3,C4,C5;
String newHashcode;
for (C1=0;C1<1;C1++)
for (C2=0;C2<2;C2++)
for (C3=0;C3<validLength;C3++){
for (C4=0;C4<validLength;C4++){
for (C5=0;C5<validLength;C5++){
String temp=""+validChars.charAt(C1)+validChars.charAt(C2)+validChars.charAt(C3)+validChars.charAt(C4)+validChars.charAt(C5);
newHashcode = HashC.HashCreator(temp);
if (HashC.Compare(passHashcode, newHashcode)) {
System.out.println("=======================");
System.out.println("Password cracked!");
System.out.println("Password: " + temp);
System.out.println("Hashcode: " + newHashcode);
System.out.println("=======================");
break;
}
}
}
}
Cheers! :thumbup1:
#3
Posted 30 October 2011 - 10:35 AM
So all those loops are to create String from AAAAA to 99999 ?
Hm, there has to be a simpler way....
Hm, there has to be a simpler way....
#4
Posted 30 October 2011 - 10:42 AM
wim DC said:
So all those loops are to create String from AAAAA to 99999 ?
Hm, there has to be a simpler way....
Hm, there has to be a simpler way....
Well, that was my thought exactly and it also raises the question: What if I wanted to check 16 or 32 chars? So I guess that there must be a more clever way. If you stumble upon something, let me know. :-)
#5
Posted 30 October 2011 - 10:53 AM
There you go, kinda long line, should maybe split it up, but it works.
Edit:
Made it pwettier
public static void main(String[] args) {
String validChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
int length = validChars.length();
int max = (int) Math.pow(validChars.length(), 5.0);
for (int i = 0; i < max ; i++) {
String result = "" + validChars.charAt((int) (i/(Math.pow(length, 4.0))%length)) + validChars.charAt((int) (i/(Math.pow(length, 3.0))%length)) + validChars.charAt((int) (i/(Math.pow(length, 2.0))%length)) + validChars.charAt((int) (i/(Math.pow(length, 1.0))%length)) + validChars.charAt(i%length);
System.out.println(result);
}
}
Takes a while to run, maybe change the max to use pow( ..., 4.0) instead of 5.0 at first, then it goes to A9999.Edit:
Made it pwettier
public static void main(String[] args) {
String validChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
int length = validChars.length();
int max = (int) Math.pow(validChars.length(), 5.0);
int maxChars = 5;
for (int i = 0; i < max ; i++) {
StringBuilder sb = new StringBuilder(maxChars);
for(int j=maxChars-1 ; j>=0 ; j--){
sb.append(validChars.charAt((int) (i/(Math.pow(length, j))%length)));
}
System.out.println(sb.toString());
}
}
#6
Posted 30 October 2011 - 11:08 AM
Here's a quicker version of what DC posted:
I get the dreaded OutOfMemory error when I go beyond 4.
String validChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
StringBuffer result = new StringBuffer("");
int length = validChars.length();
int max = (int) Math.pow(validChars.length(), 4.0);
for (int i = 0; i < max; i++) {
result.append("")
.append(validChars.charAt((int) (i
/ (Math.pow(length, 4.0)) % length)))
.append(validChars.charAt((int) (i
/ (Math.pow(length, 3.0)) % length)))
.append(validChars.charAt((int) (i
/ (Math.pow(length, 2.0)) % length)))
.append(validChars.charAt((int) (i
/ (Math.pow(length, 1.0)) % length)))
.append(validChars.charAt(i % length)).append("\n");
}
System.out.println(result);
}
I get the dreaded OutOfMemory error when I go beyond 4.
#7
Posted 30 October 2011 - 11:13 AM
Aye that be faster, but know that you keep a String of
1679616 characters in memory in the end. That's quite a bit of RAM you're occupying.
1 char = 16 bit, so 3280,5 KiloByte.
Not too bad, for AAAAA to A9999 , if you go to "99999" however you end up with a 115MB String.
1679616 characters in memory in the end. That's quite a bit of RAM you're occupying.
1 char = 16 bit, so 3280,5 KiloByte.
Not too bad, for AAAAA to A9999 , if you go to "99999" however you end up with a 115MB String.
#8
Posted 30 October 2011 - 11:18 AM
wim DC said:
Aye that be faster, but know that you keep a String of
1679616 characters in memory in the end. That's quite a bit of RAM you're occupying.
1 char = 16 bit, so 3280,5 KiloByte.
Not too bad, for AAAAA to A9999 , if you go to "99999" however you end up with a 115MB String.
1679616 characters in memory in the end. That's quite a bit of RAM you're occupying.
1 char = 16 bit, so 3280,5 KiloByte.
Not too bad, for AAAAA to A9999 , if you go to "99999" however you end up with a 115MB String.
Yes that's correct. Does your second version run any faster? The reason it's slow is that the constant System.out.println()'s take up precious time.
#9
Posted 30 October 2011 - 11:19 AM
Well, I thank you both for your time but I have a question. Both your solutions require less lines of code but what about processing power? From that point of view, which solution is the best? Especially when considering adding more characters (16 for example).
#10
Posted 30 October 2011 - 11:26 AM
They're both the same as far as processing power.
The reason DC's version runs slower is because more usage of I/O.
The reason mine gets the OutOfMemory error is because I'm storing the entire Output in a String(in RAM), then printing at the end of the program.
I'm wondering, in my case, that perhaps you can print and dump the contents of the string buffer several times throughout the program to decrease RAM usage and keep some speed.
The reason DC's version runs slower is because more usage of I/O.
The reason mine gets the OutOfMemory error is because I'm storing the entire Output in a String(in RAM), then printing at the end of the program.
I'm wondering, in my case, that perhaps you can print and dump the contents of the string buffer several times throughout the program to decrease RAM usage and keep some speed.
#11
Posted 30 October 2011 - 11:36 AM
I think, looking purely in terms of processing power your initial code would / should be fastest.
As in ours, there's stuff the processor must compute like the Math.pow(...) , and the modulus operator "%", where you just have a bunch of loops.
The only thing you could enhance is, like lethalwire did, keep it all in a String before printing because System.out.println(..) is slow as f:w00t:
The downside of ur loops is that if you want a lenghtier string, you must change more code than ours. Meaning our code is easier to maintain.
As in ours, there's stuff the processor must compute like the Math.pow(...) , and the modulus operator "%", where you just have a bunch of loops.
The only thing you could enhance is, like lethalwire did, keep it all in a String before printing because System.out.println(..) is slow as f:w00t:
The downside of ur loops is that if you want a lenghtier string, you must change more code than ours. Meaning our code is easier to maintain.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









