Question

In: Computer Science

Exp1: import java.util.Scanner; public class User_Authentication { public static void main(String args[]) { String username, password;...

Exp1:

import java.util.Scanner;

public class User_Authentication

{

public static void main(String args[])

{

String username, password;

Scanner s = new Scanner(System.in);

System.out.print("Enter username:");//username:user

username = s.nextLine();

System.out.print("Enter password:");//password:user

password = s.nextLine();

if(username.equals("Bisha") && password.equals("Computer"))

{

System.out.println("Authentication Successful");

}

else

{

    System.out.println("Authentication Failed");

   }

}

}

Exp3:

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.Scanner;

public class Main {

static Scanner sc=new Scanner(System.in);

static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

public static void main(String[] args) throws IOException {

// TODO code application logic here

System.out.print("Enter any String: ");

String str = br.readLine();

System.out.print("\nEnter the Key: ");

int key = sc.nextInt();

String encrypted = encrypt(str, key);

System.out.println("\nEncrypted String is: " +encrypted);

String decrypted = decrypt(encrypted, key);

System.out.println("\nDecrypted String is: "

+decrypted); System.out.println("\n");

}

public static String encrypt(String str, int key)

{ String encrypted = "";

for(int i = 0; i < str.length(); i++) {

int c = str.charAt(i);

if (Character.isUpperCase(c)) {

c = c + (key % 26);

if (c > 'Z')

c = c - 26;

}

else if (Character.isLowerCase(c)) {

c = c + (key % 26);

if (c > 'z')

c = c - 26;

}

encrypted += (char) c;

}

return encrypted;

}

public static String decrypt(String str, int key)

{ String decrypted = "";

for(int i = 0; i < str.length(); i++) {

int c = str.charAt(i);

if (Character.isUpperCase(c)) {

c = c - (key % 26);

if (c < 'A')

c = c + 26;

}

else if (Character.isLowerCase(c)) {

c = c - (key % 26);

if (c < 'a')

c = c + 26;

}

decrypted += (char) c;

}

return decrypted;

}

}

  1. Merge Exp. 1 and Exp. 3 to do the following: -
    1. Enter the username and password
    2. Verify the username and password
    3. If it is valid
      1. apply the Caesar cipher
      2. display the plain username and password
      3. display the encrypted username and password
    4. Write your conclusion about this encryption method and the authentication process.

      need your help regarding attached for all the questions

      Answered it from your mind Do not copying the Answered Especially "D"

Solutions

Expert Solution

The modified java program to implement the a,b, c parts of the question as below:

import java.util.Scanner;

public class Authentication {
        //authenticating 
        public static void authenticate(String username, String password){
                Scanner sc = new Scanner(System.in);
                if(username.equals("Bisha") && password.equals("Computer")){
                        System.out.println("Authentication is Successful \nPlease enter the key :");
                        int key = sc.nextInt();
                        String encUserName = encrypt(username, key);
                        String decUserName = decrypt(encUserName, key);
                        String encPassword = encrypt(password, key);
                        String decPassword = decrypt(encPassword, key);
                        
                        System.out.println("Before encryption : "+username+"  "+password);
                        System.out.println("After encryption : "+encUserName+"  "+encPassword);
                        System.out.println("After decryption : "+decUserName+"  "+decPassword);
                        
                        
                }else {
                    System.out.println("Authentication Failed");
           }            
        }
        //encrypting
        public static String encrypt(String str, int key)
        { 
                String encrypted = "";
                for(int i = 0; i < str.length(); i++) {
                        int c = str.charAt(i);
                        if (Character.isUpperCase(c)) {
                                c = c + (key % 26);
                                if (c > 'Z')
                                        c = c - 26;
                        }else if (Character.isLowerCase(c)) {
                                c = c + (key % 26);
                                if (c > 'z')
                                        c = c - 26;
                        }
                        encrypted += (char) c;
                }
                return encrypted;
        }
    //decrypting
        public static String decrypt(String str, int key){ 
                String decrypted = "";
                for(int i = 0; i < str.length(); i++) {
                        int c = str.charAt(i);
                        if (Character.isUpperCase(c)) {
                                c = c - (key % 26);
                                if (c < 'A')
                                        c = c + 26;
                        }
                        else if (Character.isLowerCase(c)) {
                                c = c - (key % 26);
                                if (c < 'a')
                                        c = c + 26;
                        }
                        decrypted += (char) c;
                }
                return decrypted;
        }
        
        public static void main(String[] args){
                Scanner sc = new Scanner(System.in);
                System.out.println("Enter user name");
                String username = sc.nextLine();
                System.out.println("Enter your password");
                String password = sc.nextLine();
                authenticate(username, password);
        }
}

The output screenshots of the above program is :

D) The above authentication process is directly comparing the userName and password with the original values. Instead of comparing with original values we should compare the encrypted format of the username and password, so that it will be secured.

The above encryption algorithm is substitution cipher algorithm using the key value for replacing the each character with next key'th letter in the alphabet. If if the next Key'th letter is more than 'Z', then counting the next letter in round robin fashion. This will helps in securing the data while transferring the data as it is in encrypted format which is not understandable.

The above cipher can only deals with the text formed with [a-z A-Z] . But in real life program, there are many speacial characters also present. So, we should be able to deal with them also.

If you have any queries regarding this answer, please reach out through the comment section.


Related Solutions

import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        } System.out.println(""+getAvg(new_stack));    }     public static int getAvg(Stack s) {        //TODO: Find the average of the elements in the...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        }        int new_k = scan.nextInt(); System.out.println(""+smallerK(new_stack, new_k));    }     public static int smallerK(Stack s, int k) {       ...
------------------------------------------------------------------------------------ import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input =...
------------------------------------------------------------------------------------ import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int result = 0; System.out.print("Enter the first number: "); int x = input.nextInt(); System.out.print("Enter the second number: "); int y = input.nextInt(); System.out.println("operation type for + = 0"); System.out.println("operation type for - = 1"); System.out.println("operation type for * = 2"); System.out.print("Enter the operation type: "); int z = input.nextInt(); if(z==0){ result = x + y; System.out.println("The result is " + result); }else...
//This is an ArrayReverser Problem.. import java.util.Scanner; public class Main { public static void main(String[] args)...
//This is an ArrayReverser Problem.. import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in);    // Read in the list of numbers int[] numbers; String input = sc.nextLine(); if (input.equals("")) { numbers = new int[0]; } else { String[] numberStrings = input.split(" "); numbers = new int[numberStrings.length]; for (int i = 0; i < numberStrings.length; i++) { numbers[i] = Integer.parseInt(numberStrings[i]); } }    // Reverse the list int[] resultArray = reverseArray(numbers);   ...
import java.lang.UnsupportedOperationException; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc...
import java.lang.UnsupportedOperationException; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in);    // parse the number of strings int numStrings = Integer.parseInt(sc.nextLine());    // parse each string String[] stringsArray = new String[numStrings]; for (int i = 0; i < numStrings; i++) { stringsArray[i] = sc.nextLine(); }    // print whether there are duplicates System.out.println(hasDuplicates(stringsArray)); }    private static boolean hasDuplicates(String[] stringsArray) { // TODO fill this in and remove the below line...
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Exercise { public static void main(String[] args) {...
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Exercise { public static void main(String[] args) { Scanner input=new Scanner(System.in); int[] WordsCharsLetters = {0,1,2}; while(input.hasNext()) { String sentence=input.nextLine(); if(sentence!=null&&sentence.length()>0){ WordsCharsLetters[0] += calculateAndPrintChars(sentence)[0]; WordsCharsLetters[1] += calculateAndPrintChars(sentence)[1]; WordsCharsLetters[2] += calculateAndPrintChars(sentence)[2]; } else break; } input.close(); System.out.println("Words: " + WordsCharsLetters[0]); System.out.println("Characters: " + WordsCharsLetters[1]); System.out.println("Letters: " + WordsCharsLetters[2]); } static int[] calculateAndPrintChars(String sentence) { int[] WCL = new int[3]; String[] sentenceArray=sentence.split(" "); WCL[0] = sentenceArray.length; int letterCount=0; for(int i=0;i<sentence.length();i++) { if(Character.isLetter(sentence.charAt(i))) letterCount++; } WCL[1]...
Consider the following code: import java.util.Scanner; public class Main {    public static void main(String[] args)...
Consider the following code: import java.util.Scanner; public class Main {    public static void main(String[] args) {    Scanner in = new Scanner(System.in); String input = ""; System.out.println("Enter the first number: ");    input = in.nextLine(); int a = 0; int b = 0;    a = Integer.parseInt(input);    System.out.println("Enter the second number: "); input = in.nextLine();    b = Integer.parseInt(input);    int result = 0; result = a/b;    System.out.println("a/b : " + result);    } Copy the code...
Correct the code: import java.util.Scanner; public class Ch7_PrExercise5 { public static void main(String[] args) {   ...
Correct the code: import java.util.Scanner; public class Ch7_PrExercise5 { public static void main(String[] args) {    Scanner console = new Scanner(System.in);    double radius; double height; System.out.println("This program can calculate "+ "the area of a rectangle, the area "+ "of a circle, or volume of a cylinder."); System.out.println("To run the program enter: "); System.out.println("1: To find the area of rectangle."); System.out.println("2: To find the area of a circle."); System.out.println("3: To find the volume of a cylinder."); System.out.println("-1: To terminate the...
import java.util.Scanner; public class test {    public static void main(String args[]){        char letter;...
import java.util.Scanner; public class test {    public static void main(String args[]){        char letter;        int number = 0;        Scanner in = new Scanner(System.in);        System.out.print("Enter a letter: ");        letter = in.next().charAt(0);        if(letter == 'A' || letter == 'B' || letter == 'C') number = 2;        if(letter == 'D' || letter == 'E' || letter == 'F') number = 3;        if(letter == 'G' || letter ==...
My code: import java.util.Random; import java.util.Scanner; public class RollDice { public static void main(String[] args) {...
My code: import java.util.Random; import java.util.Scanner; public class RollDice { public static void main(String[] args) { int N; Scanner keybd = new Scanner(System.in); int[] counts = new int[12];    System.out.print("Enter the number of trials: "); N = keybd.nextInt();    Random die1 = new Random(); Random die2 = new Random(); int value1, value2, sum; for(int i = 1; i <= N; i++) { value1 = die1.nextInt(6) + 1; value2 = die2.nextInt(6) + 1; sum = value1 + value2; counts[sum-1]++; }   ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT