无法一次发送一个密码尝试(Java)

时间:2016-05-07 18:36:26

标签: java

我正在尝试为我的Java类做额外的信用分配,我们试图入侵服务器。我现在遇到的问题是一次只发送一个密码:

import java.io.*;
import java.net.*;
import java.util.Scanner;

public class Client 
{
    private Socket socket;
    private PrintWriter out;
    private BufferedReader in;
    private static int passwordLength;
    private static String attempt;
    private static int counter = 0;
    private static String acceptable = "ABCDEFGHIJKLMNOPQRSTUVWXYXZabcdefghijklmnopqrstuvwxyz0123456789";
    public Client()
    {
        try 
        {
            System.out.println("Connecting to server...");
            socket = new Socket("localhost", 58999);
            out = new PrintWriter(socket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));                                                                          
        } 
        catch (Exception e)
        {
            System.out.println("Run the server first.");
        }
    }

    public void close()
    {
        try 
        {
            socket.close();
        } 
        catch (IOException e) 
        {
            System.out.println(e.getMessage());
        }
    }

    public String sendPassword(String pass)
    {                        
        if (!HUSH) System.out.print("Sending: " + pass);
        out.println(pass);
        String result = null;
        try 
        {
            result = in.readLine();
            if (!HUSH)
            {
                if (result.equals("no"))
                    System.out.println(" (wrong password)");
                else if (result.equals("yes"))
                    System.out.println(" (CORRECT!)");
            }
        } 
        catch (IOException e) 
        {
            System.out.println(e.getMessage());
        }
        return result;
    }

    public static boolean HUSH = false;
    public static void main(String[] args) 
    {                                  
        Client me = new Client();

        //BEGIN YOUR WORK
        int length;
        HUSH = false; //change this to false for testing
        System.out.println("Input character length");
        Scanner ui = new Scanner(System.in);
        length = ui.nextInt();
        Client(length);//set the max length of the password
        generate();//pull into the first generate method
        me.sendPassword("1234");
        me.sendPassword(attempt); //the first password i am trying to break
        me.sendPassword("letmein");        
        me.sendPassword("willthiswork");        
        // END YOUR WORK

        me.close();
    }

    public static void Client(int max)
    {
        passwordLength = max;
    }
    public static void generate()
    {
        generate(""); //enters generate(String password)
    }

    static void generate(String password)
    {
        //base case
        if(password.length() == passwordLength)//if password is long enough
            System.out.println(++counter + " " + password);
        else
            for(int x = 0; x < acceptable.length(); x++)
                generate(attempt = password + acceptable.charAt(x));
    }
}

当我运行代码(使用提供的服务器)时,它运行每个可能的密码组合,但返回9(passwordLength次数)而不是发送说..... A(密码错误)B(错误密码)等等。我知道我需要在for循环中添加一些内容以将其调回main,但我不确定如何。

0 个答案:

没有答案