Reputation: 11
This is an assignment for class, basically make a method that can "guess" the password then send it to another class which will de-crypt a file. I have the hard part done.
import sussex.edu.Cryptography;
public class Final {
public static void main(String[] args) throws Exception{
//decrypter();
Cryptography crypto = new Cryptography();
String encrypted = "SourceFile_encrypted.txt";
String decrypted = "SourceFile_decrypted.txt";
String password = new String();
crypto.setPassword(password);
if(crypto.isPasswordValid()){
System.out.println("Found password:" + password);
crypto.decryptFile(encrypted, decrypted);
}
else{
//Keep trying...
}
}
public static String decrypter(){
char array[] = new char[5];
Cryptography crypto = new Cryptography();
String password = new String();
for (char c0 = 'A'; c0 <= 'Z'; c0++) {
array[0] = c0;
for (char c1 = 'A'; c1 <= 'Z'; c1++) {
array[1] = c1;
for (char c2 = 'A'; c2 <= 'Z'; c2++) {
array[2] = c2;
for (char c3 = 'A'; c3 <= 'Z'; c3++) {
array[3] = c3;
for (char c4 = 'A'; c4 <= 'Z'; c4++){
array[4] = c4;
String s = new String(array);
password = s;
System.out.println(password);
crypto.setPassword(password);
if(crypto.isPasswordValid()){
break;
}
}
}
}
}
}
return password;
}
}
It iterates through all possible passwords, and goes past the actual password,(OLLEH) and does nothing. When I manually enter the password into the first crypto.setPassword(password); It works just fine. when I enter it, it looks like crypto.setPassword("OLLEH"); but if I manually enter "OLLEH" into the crypto.setPassword(password); inside the nested for loop it doesn't work. So my error is in there, i just can't see it.
Upvotes: 1
Views: 341
Reputation: 10717
Probably your isPasswordValid() method is comparing Strings with == instead of equals().
That could explain why this comparison returns true when it's against "OLLEH", but false when it's against a String with the same value, but created using new
.
Try substituting password = s;
by password = s.intern ();
Upvotes: 2