PDA

View Full Version : Help writting a basic Java app




mgiddy911
Oct 14, 2004, 09:44 PM
Hey I need some help with a java application i have been playing with.
I was trying to write a basic console app that breaks a password that is conatined within the program itself. SO basicly I have an app that starts checking if one string equals the password one letter at a time, it starts with one letters then move son to two until it finds the right combination of letters. I got that much done and it worked, until I tried to have it start over again with a user input password once it cracks the first one. I want it to ask you to try again, then if you enetr y, then it asks u to enter a up to 10 letter string, ste that as the new password and begin process over again. Heres my orignal code:

import java.io.*;
import StringReader;
class passworder {

static String charIncrement(String pass, int index) {
String temp = "";

for(int i = 0; i < pass.length(); i++) {
temp += (char) (i == index ? pass.charAt(i) + 1: pass.charAt(i));
}

return temp;
}

static String reset(String pass, int index) {
String temp = "";

for(int i = 0; i < pass.length(); i++) {
temp += (char) (i == index ? '0': pass.charAt(i));
}

return temp;
}

public static void main(String args[]) {

System.out.println("Attempting to dechipher password");
int flager = 0;
StringReader ob = new StringReader("dad");
String guess = "0";
int flag = 0;
label1 : {
label : {



for(int a = 47; a <= 121; a++) {
if(flager == 1) guess = reset(guess, guess.length() - 9);

for(int b = 47; b <= 121; b++) {
if(flager == 1) guess = reset(guess, guess.length() - 8);

for(int c = 47; c <= 121; c++) {
if(flager == 1) guess = reset(guess, guess.length() - 7);

for(int d = 47; d <= 121; d++) {
if(flager == 1) guess = reset(guess, guess.length() - 6);

for(int e = 47; e <= 121; e++) {
if(flager == 1) guess = reset(guess, guess.length() - 5);

for(int f = 47; f <= 121; f++) {
if(flager == 1) guess = reset(guess, guess.length() - 4);

for(int g = 47; g <= 121; g++) {
if(flager == 1) guess = reset(guess, guess.length() - 3);

for(int h = 47; h <= 121; h++) {
if(flager == 1) guess = reset(guess, guess.length() - 2);

for(int i = 47; i <= 121; i++) {

if(flager == 1) guess = reset(guess, guess.length() - 1);

for(int j = 47; j <= 121; j++) {

if(flag == 0) {
guess = "0";
System.out.println("Trying One Letter Passwords");
System.out.println(guess);
flag = 1;
}

if(ob.password(guess)) break label;

guess = charIncrement(guess, (guess.length() - 1));
}
flager = 1;
if(flag == 1) {
guess = "00";
System.out.println("Trying Two Letter Passwords");
System.out.println(guess);
flag = 2;
if(ob.password(guess)) break label;
}

guess = charIncrement(guess, (guess.length() - 2));

}
if(flag == 2) {
guess = "000";
System.out.println("Trying Three Letter Passwords");
System.out.println(guess);
flag = 3;
if(ob.password(guess)) break label;
}

guess = charIncrement(guess, (guess.length() - 3));
}
if(flag == 3) {
guess = "0000";
System.out.println("Trying Four Letter Passwords");
System.out.println(guess);
flag = 4;
if(ob.password(guess)) break label;
}

guess = charIncrement(guess, (guess.length() - 4));
}
if(flag == 4) {
guess = "00000";
System.out.println("Trying Five Letter Passwords");
System.out.println(guess);
flag = 5;
if(ob.password(guess)) break label;
}

guess = charIncrement(guess, (guess.length() - 5));
}
if(flag == 5) {
guess = "000000";
System.out.println("Trying Sicks Letter Passwords");
System.out.println(guess);
flag = 6;
if(ob.password(guess)) break label;
}

guess = charIncrement(guess, (guess.length() - 6));
}
if(flag == 6) {
guess = "0000000";
System.out.println("Trying Seven Letter Passwords");
System.out.println(guess);
flag = 7;
if(ob.password(guess)) break label;
}

guess = charIncrement(guess, (guess.length() - 7));
}
if(flag == 7) {
guess = "00000000";
System.out.println("Trying Eight Letter Passwords");
System.out.println(guess);
flag = 8;
if(ob.password(guess)) break label;
}

guess = charIncrement(guess, (guess.length() - 8));
}
if(flag == 8) {
guess = "000000000";
System.out.println("Trying Nine Letter Passwords");
System.out.println(guess);
flag = 9;
if(ob.password(guess)) break label;
}

guess = charIncrement(guess, (guess.length() - 9));
}
if(flag == 9) {
guess = "0000000000";
System.out.println("Trying Ten Letter Passwords");
System.out.println(guess);
flag = 10;
if(ob.password(guess)) break label;
}

guess = charIncrement(guess, (guess.length() - 10));
}
}
System.out.println("Password is: " + guess);
System.out.println("Try Again?");
BufferedReader rb = new BufferedReader(new InputStreamReader(System.in));
char c=0;
try {
c = (char) rb.read();
}catch(java.io.IOException e){}
if(c == 71){
flager = 0;
guess = "0";
flag = 0;
BufferedReader br = new BufferedReader( new InputStreamReader(System.in));
String str="hi";
System.out.println("Enter new password");

try {
str = br.readLine();
}catch(java.io.IOException e){}
ob = new StringReader(str);
}break label1;
}
}
}

heres the stringreader class code:

import java.io.*;
class StringReader{
static private String key;



StringReader(String pass){
key = pass;
}

static boolean password(String string) {
boolean i;

i =string.equals(key);
if(i) {
System.out.println("Access Granted, Password correct");
return i;
}
else {
return i;
}
}
}

Thanks, please feel free to suggest anything, such as ways to do this better, optimizatiosn of my code, any critiques are appreciated



G4scott
Oct 14, 2004, 10:08 PM
aah!!!

I just got out of a CS test right now... If my brain wasn't fried, I'd help you...

Have you tried recursion, by the way?

mgiddy911
Oct 14, 2004, 10:13 PM
yeah I vaguely remember that recursion thing from my one semester java class soph yr of highschool ( im only a jr now by the way).
I dont remeber enough to write recursive methods for this program right now but suggestions would help.
yeah my brain is also fried form exam week here at school (St. Louis University Highschool), so this app is harder to work with right now, and also I dont know as much as I wish I did in java, my course was short and didn't cover as much as I woudl have liked, so now due to stupid scheduling regulations, I couldn't take the AP comp class which is really java 2 for us. SO i have to wait to next yr to do a follow up course in java.

iBert
Oct 14, 2004, 10:18 PM
I've never used Java. But I have done this quite often in C++. Use a do while condition. Enter to the program as if its the first time, then when is time to actually leave the program add the while. make sure you ask the user if they want to go again and then compare answers.

something like this is what I do in C++,

main ()
{
//variables or whatever
do
{
//all you need to do here
//ask user if they want to quit
}while(answer is no keep working)
//exit here
}

mgiddy911
Oct 14, 2004, 10:22 PM
Thanks for the tip, I have considered this But am not sure where to begin with the do-while loop, I don't knwo how to implement it directly into this source code I have, I don't want to start over either

MacNeXT
Oct 15, 2004, 04:43 AM
I'm sorry but it looks like you're not used to OO programming and you're starting out with java. You obviously don't understand how to use OO because you use static functions and labels. That's BAD!

Anyway, here is a class in pseudo code you could use to get consecutive strings. It sort of simulates decimal counting. If digit is '9', it resets to '0' and a new digit '1' is inserted at the beginning, you get '10'. Replace 0 with char 47 and 9 with char 121.


class Guess {
String guess = new String("0");

void increase() {
increaseDigit(guess.length()); // index of last character
}

void increaseDigit (index) {
if (index == -1) {
insert char 47 at begin
} else {
if (char at index > 121 ) {
char at index = 47;
increaseDigit (index - 1);
} else {
increase char at index;
}
}
}
string getGuess() {
return guess;
}
}


you could use it like this:


Guess g = new Guess();
while (g.getGuess() does not equal whatever) {
g.increase();
}
answer is g.getGues()


I hope this helps.

SilentPanda
Oct 15, 2004, 07:20 AM
Just figured now would be a good time to mention the "code" tag...


public static void main(String args[]) {
System.out.println("Some string");
}

Linz
Oct 15, 2004, 08:55 AM
I took a few minutes and hacked up a simple, possible solution. It depends how you're planning this to work. This code just tests each character in the password until the whole password is tested:

import java.io.*;

public class PasswordCheck {
public static void main(String[]args) throws IOException {
String password = "thisIsThePassword";
String choice = "";
BufferedReader inuser = new BufferedReader(new InputStreamReader(System.in));

String temp = decipher(password);

System.out.println("Password is: " + temp);
System.out.print("Try again? (y/n) : ");
choice = inuser.readLine();
while(true) {
switch(choice.charAt(0)) {
case 'y':
System.out.print("Enter new password: ");
password = inuser.readLine();
temp = decipher(password);
System.out.println("Password is: " + temp);
System.out.print("Try again? (y/n) : ");
choice = inuser.readLine();
case 'n':
System.exit(0);
}
}
}

public static String decipher(String password) {
String temp = "";
System.out.println("Attempting to decipher password");
for (int i = 0; i < password.length(); i++) {
int character = 0;
while(true) {
if (password.charAt(i) == (char) character) {
temp += (char) character;
break;
}
character++;
}
}
return temp;
}
}

FritzTheWonderM
Oct 15, 2004, 09:41 AM
Here is a slightly faster decipher method. Sure, we're talking 1 millisecond difference for password size strings, but you might use this in something bigger.


public static String fastdecipher(String password) {
StringBuffer temp = new StringBuffer(36);
System.out.println("Attempting to fast decipher password");

long start = System.currentTimeMillis();
byte[] passwordints = password.getBytes();
for (int i = 0; i < password.length(); i++) {
int character = 0;
while (true) {
if (passwordints[i] == character) {
temp.append((char) character) ;
break;
}
character++;
}
}

long end = System.currentTimeMillis();
System.out.println("Fast Decipher took: "+(end-start)+" milliseconds");
return temp.toString();
}

King Cobra
Oct 15, 2004, 09:50 AM
yeah I vaguely remember that recursion thing from my one semester java class soph yr of highschool
The really super-simplified way to describe recursion in Java is: Calling the method you are already in. But often recursion leads to algorithms, which is your second Java course, and they deal with repetition patterns (like the Koch snowflake).

Now, with all that being said, if your program still doesn't work, try this: Take at least 5 days off from working on the program, then come back to it. Don't even touch the Java files for another 5 days. Additionally, don't even revisit this thread anymore until you have finished reading this paragraph and have let 5 days pass. If you want, you can spend time thinking about why it's still not working, but don't touch the files or the program for a while. Even if you are not sure if spending (at minimum) 5 days will seem to help in any way possible, try it anyways. Sometimes, the most powerful trick to programming is to remove your mindset from the very specifics you want to change so that you can revisit the program with more of a focus on the entire project and work your way back into the code. Often, you'll find the differences in your new view of the project in terms of the way you assemble the code versus the code you have now, and in recognizing those differences, you'll usually recognize why your program originally didn't work. So take the 5 days off if your program isn't working, then come back to it. Finally, if you have time constraints and can't do all of the 5 days, then plan so that you have two days left before a deadline, then revisit the code.

SilentPanda
Oct 15, 2004, 09:56 AM
King Cobra gives very good advice (although usually a few hours will do the trick for me)... another good thing to do is at some point... admit defeat. Admit that you went about the problem the wrong way and that salvaging what you have is not a good solution as it will just create more complications. Save the code just in case it is indeed your only route, take a few days off from it to clear your own memory buffer, then start again. You might come off with a much better and cleaner idea. And don't hesitate to look into the Java API to find out if something to do what you want to do already exists! There often times is and it will save you a lot of time and agony.

mgiddy911
Oct 15, 2004, 11:52 AM
Thanks for all the help, the dechipher and fast dechiper worked amazingly
and for the comment about OO programming, I am used to it, but had not learned OO programming yet when I had written this program. In my school java class we did nothing object oriented unitl the very end, I wrote this program befroe we really got into pplaying around with many many classes and objects.