In: Computer Science
import java.util.Scanner; // Import the Scanner class
import java.io.IOException; // Import the IOException class
import java.io.FileWriter; // Import the FileWriter class
import java.nio.file.*;
class Main {
//function to encode the message
public static void encode()
{
Scanner sc = new Scanner(System.in);
System.out.println(" Input the plaintext message : ");
String plaintext = sc.nextLine();
System.out.println(" Enter the value by which each character in the plaintext message gets shifted:"); int shift = sc.nextInt();
String ciphertext = "";
char alphabet;
for(int i=0; i < plaintext.length();i++)
{
// Shift one character at a time
alphabet = plaintext.charAt(i);
// if alphabet lies between a and z
if(alphabet >= 'a' && alphabet <= 'z')
{
// shift alphabet
alphabet = (char) (alphabet + shift);
// if shift alphabet greater than 'z'
if(alphabet > 'z')
{
// reshift to starting position
alphabet = (char) (alphabet+'a'-'z'-1);
}
ciphertext = ciphertext + alphabet;
}
// if alphabet lies between 'A'and 'Z'
else if(alphabet >= 'A' && alphabet <= 'Z') {
// shift alphabet
alphabet = (char) (alphabet + shift);
// if shift alphabet greater than 'Z'
if(alphabet > 'Z')
{
//reshift to starting position
alphabet = (char) (alphabet+'A'-'Z'-1);
}
ciphertext = ciphertext + alphabet;
}
else
{
ciphertext = ciphertext + alphabet;
}
}
//user input for filename to store encrypted message
System.out.println("Enter the file name where you want to store encrypted message:");
String filename = sc.next();
//writing encrypted message to the file
try
{
FileWriter fp = new FileWriter(filename);
fp.write(ciphertext);
fp.close();
}
catch(IOException e)
{
System.out.println("Error Occured");
}
}
//function to decode the message
public static void decode() throws Exception
{
Scanner sc = new Scanner(System.in);
//user input for filename
System.out.println("Enter the file name which have stored encrypted message:");
String filename = sc.next();
//shift
System.out.println(" Enter the value by which each character in the plaintext message gets shifted:"); int shift = sc.nextInt();
String ciphertext ;
String decryptMessage = "";
//reading the encrypted string from the file and store in ciphertext variable
ciphertext = new String(Files.readAllBytes(Paths.get(filename)));
for(int i=0; i < ciphertext.length();i++)
{
// Shift one character at a time
char alphabet = ciphertext.charAt(i);
// if alphabet lies between a and z
if(alphabet >= 'a' && alphabet <= 'z')
{
// shift alphabet
alphabet = (char) (alphabet - shift);
// shift alphabet lesser than 'a'
if(alphabet < 'a') {
//reshift to starting position
alphabet = (char) (alphabet-'a'+'z'+1);
}
decryptMessage = decryptMessage + alphabet;
}
// if alphabet lies between A and Z
else if(alphabet >= 'A' && alphabet <= 'Z')
{
// shift alphabet
alphabet = (char) (alphabet - shift);
//shift alphabet lesser than 'A'
if (alphabet < 'A') {
// reshift to starting position
alphabet = (char) (alphabet-'A'+'Z'+1);
}
decryptMessage = decryptMessage + alphabet;
}
else
{
decryptMessage = decryptMessage + alphabet;
}
}
System.out.println(" Decrypted message : " + decryptMessage);
}
public static void main(String[] args) throws Exception {
int ch;
Scanner sc = new Scanner(System.in);
//choose the operation
System.out.println("1.Encode Message");
System.out.println("1.Decode Message");
System.out.println("Enter your choice:");
ch = sc.nextInt();
sc.nextLine();
//if ch is 1 perform encode operation
if(ch==1)
{
encode();
}
//if ch is 2 perform decode operation
else if(ch==2)
{
decode();
}
else
{
System.out.println("Invalid choice");
}
}
}
Output::
Encode operation:
Decode operation::
The encrypt.txt which stores encrypted text::