0% found this document useful (0 votes)
33 views

Cs508 Assignment 3 Solution Code

This Java code defines methods for encrypting and decrypting messages. The encryption method prompts the user to enter a student ID and name, then uses the ID and name length to calculate an encryption key. It encrypts the user's message by XORing each character with the key and displays the encrypted text. The decryption method prompts for the encrypted message and key, then decrypts the message by XORing each character with the key again and displays the original text. The main method runs a menu loop allowing the user to choose encryption or decryption until exiting.

Uploaded by

Zee
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Cs508 Assignment 3 Solution Code

This Java code defines methods for encrypting and decrypting messages. The encryption method prompts the user to enter a student ID and name, then uses the ID and name length to calculate an encryption key. It encrypts the user's message by XORing each character with the key and displays the encrypted text. The decryption method prompts for the encrypted message and key, then decrypts the message by XORing each character with the key again and displays the original text. The main method runs a menu loop allowing the user to choose encryption or decryption until exiting.

Uploaded by

Zee
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.io.

BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
class Main {
public static BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
private static void doEncryption() throws IOException
{ String id = "";
String name = "";
String message ="";
String encryption_message="";
int vuid = 0;
int name_length=0;
System.out.println("Please Enter your Student id:" );
id = input.readLine()
; if(id.length() < 11 || id.length() > 11){
System.out.println("Please Enter Correct Student id:" );
id = input.readLine();
}else{
id = id.replaceAll("[^\\d]","");
id= id.trim();
vuid = Integer.parseInt(id);
}
System.out.println("Please Enter your Full Name:" );
name = input.readLine();
if(name.length() < 6){
System.out.println("Please Enter your full name having atleast 6 characters:" );
name = input.readLine();
}else{
name_length = name.replace(" ","").length();
}
System.out.println("Please Enter the message that you want to encrypt:" );
message = input.readLine();
if(message.length()< 10){
System.out.println("Please Enter your message having atleast 10 characters:" );
message = input.readLine();
}
int key = vuid % name_length;
for(int i = 0; i < message.length(); i++){
encryption_message += Character.toString((char) (message.charAt(i) ^ key));
}
System.out.println("Encrypted message:" + encryption_message);
System.out.println("key : " +key);
}
private static void doDecryption()throws IOException
{
String message = ""; String encrypt_message="";
System.out.println("Please Enter your message: for decrypt" );
message = input.readLine();
if(message.length()< 10){
System.out.println("Please Enter your message having atleast 10 characters:" );
message = input.readLine();
}
System.out.println("Please Enter the key" );
int key=Integer.parseInt(input.readLine());
for(int i = 0; i <message.length(); i++){
encrypt_message +=Character.toString((char) (message.charAt(i) ^ key));
}
System.out.println("Encrypted message:" +encrypt_message);
}
private static void developedBy()
{
System.out.println("Thanks for Using Encryption System!");
System.out.println("Developed By : BC123456789");
}
public static void main(String[] args) throws IOException{
boolean check = true ; int choice =0;
do{
System.out.println("Please Press '1' to encrypt a Message , '2' to decrypt a Message or '3' to exit a
Program. ");
choice = Integer.parseInt(input.readLine());
switch(choice){
case 1:
doEncryption();
break;
case 2:
doDecryption();
break;
case 3:
developedBy();
check = false;
break;
default:
System.out.println("Pleae Enter Correct Choice");
}
}while(check);
}
}

You might also like