In: Computer Science
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;
}
}
need your help regarding attached for all the questions
Answered it from your mind Do not copying the Answered Especially "D"
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.