Simple User/Pass Access

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Andrew Hopps
    New Member
    • Mar 2012
    • 2

    Simple User/Pass Access

    Code:
    import java.util.Scanner;
    
    public class Test {
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
    		String username, tempuser;
    		username = "testuser";
    		
    		{System.out.println("Please enter your username.");
    		tempuser = input.nextLine();
    		if (!tempuser.equals(username)){
    			System.out.println("Wrong username.");
    			Test.main(null);}
    		else 
    			Test.Access();
    		}
    	}
    	
    	public static void Access() {
    		Scanner input2 = new Scanner(System.in);
    		String password, temppassword;
    		password = "testpass";
    
    	System.out.println("Please enter your password.");
    		temppassword = input2.nextLine();
    
    		if (!temppassword.equals(password)){
    			System.out.println("Wrong password.");
    			Test.main(null);}
    		else
    			System.out.println("Access Granted!");}
    	
    	
    }
    Pastebin of JAVA Code for easier reading.

    I am a newbie to Java and have been learning and testing. My question is this, is the code above proper for a simple protected program in its current state?
    Last edited by Andrew Hopps; Mar 22 '12, 09:46 AM. Reason: Added Code and Link
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Typically password are stored in a database. Actually their digests are stored in the database. A digest is a fixed length string created by applying some algorithm to a string in such a way that it is practically impossible to deduce the password given it's digest.
    The authentication is then done by comparing digests rather than comparing the actual passwords.

    Comment

    • Andrew Hopps
      New Member
      • Mar 2012
      • 2

      #3
      Just for the sake of argument and to stay within my limits, this program is to be used by only me and only on my computer, and therefore I would have the only password. Would the above work?

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Your problem would be that anyone who has access to your program (even the compiled .class files) will be able to open the .class file and see both the user name and password in it.

        Comment

        Working...