import { } } } else {

import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.nio.charset.StandardCharsets;import java.security.InvalidKeyException;import java.security.Key;import java.security.NoSuchAlgorithmException;import java.util.Date;import java.util.Scanner;import javax.crypto.spec.SecretKeySpec;public class Server { private static final String PASSWORDS_TXT = “passwords.txt”; public static void main(String args) throws NumberFormatException, InvalidKeyException, NoSuchAlgorithmException { String password = null; String username = null; if (args == null || args.length < 2) { System.out .println("Server should run as: Server username new or Server username password pin"); System.exit(0); } username = args0; if (args.length == 2) { int attempts = 0; if ("new".equalsIgnoreCase(args1)) { Scanner scanner = new Scanner(System.in); password: { System.out.print("Enter Password : "); password = scanner.nextLine(); boolean isValid = PasswordValidator.validate(password); if (attempts == 3) { System.out .println("Reached maximum attempts. System is exiting"); System.exit(0); } if (!isValid) { attempts++; System.out .println("Failed to meet criteria Length (6 - 20), Upper and Lower case letter, digits, Special characters"); break password; } else { System.out.print("Confirm Password : "); if (password.equals(scanner.nextLine())) { System.out.println("Account Created"); BufferedWriter bw = null; try { bw = new BufferedWriter(new FileWriter( PASSWORDS_TXT, true)); bw.write(username + ":" + password); bw.newLine(); bw.flush(); } catch (IOException ioe) { ioe.printStackTrace(); } finally { if (bw != null) try { bw.close(); } catch (IOException ioe2) { } } } else { System.out.println("Passwords didnt match"); break password; } } } scanner.close(); } } else if (args.length == 3) { String line = null; BufferedReader bufferedReader = null; FileReader fileReader = null; try { fileReader = new FileReader(PASSWORDS_TXT); bufferedReader = new BufferedReader(fileReader); while ((line = bufferedReader.readLine()) != null) { if (line.equals(args0 + ":" + args1)) { final Key key = new SecretKeySpec( "12345678901234567890" .getBytes(StandardCharsets.US_ASCII), "RAW"); if (args2.equals((new OTPGenerator()) .generateOneTimePassword(key, new Date()))) { System.out.println("Login Succesful"); } } else { System.out.println("Incorrect Username/Password"); } } } catch (FileNotFoundException ex) { System.out.println("Unable to open file '" + PASSWORDS_TXT + "'"); } catch (IOException ex) { System.out .println("Error reading file '" + PASSWORDS_TXT + "'"); } finally { if (bufferedReader != null) try { bufferedReader.close(); } catch (IOException e) { } if (fileReader != null) { try { fileReader.close(); } catch (IOException e) { } } } } else { System.out .println("Server should run as: Server username new or Server username password pin"); System.exit(0); } }}

x

Hi!
I'm Johnny!

Would you like to get a custom essay? How about receiving a customized one?

Check it out