Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program with the following two static methods: 1. + encrypt (oneTimePad

ID: 3529345 • Letter: W

Question

Write a program with the following two static methods: 1. + encrypt (oneTimePad : String, messageFile : String, outputFile : String) - The encrypt method should take three arguments: oneTimePad - which is the name of the file containing the one time pad (a sequence of characters that will be the key to encrypt and decrypt a message) messageFile - the name of the file containing the message. Note the message will need to contain the same number of characters or fewer than the one time pad file. outputFile - the name of the file that the encrypted message should be stored in. Encryption should be performed as follows: 2. + decrypt (oneTimePad : String, messageFile : String, outputFile : String) - The decrypt method should take three arguments: oneTimePad - which is the name of the file containing the one time pad (a sequence of characters that will be the key to encrypt and decrypt a message). Note to decrypt a file, you'll need to use the same one time pad that was used to encrypt the file. messageFile - the name of the file containing the encrypted message. Note the message will need to contain the same number of characters or fewer than the one time pad file. outputFile - the name of the file that the decrypted message should be stored in.

Explanation / Answer

Hey this is the exact code for your encryption and decryption problem..

You have not mentioned the encrytion algorithim so i used a simple algorithim like if " key=abcd" and "Message is : This " then encryption is : "abcdThisabcd"

Check it out..If you face any problem then plz comment..

Just give the exact file path for 3 files :It will work for sure..

Please rate it for my work..

Code Here:


import java.util.Scanner;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileWriter;

import java.io.OutputStreamWriter;

import java.io.IOException;

import java.io.BufferedWriter;

public class Security {


private static transient String decodeingKey;

private static transient String encodeingKey;

public static void encrypt(String oneTimePad,String messageFile,String outputFile)

{

try

{

System.out.println(oneTimePad);

decodeingKey="";

File f1=new File(oneTimePad);

Scanner sc=new Scanner(f1);

while(sc.hasNext())

{

decodeingKey += sc.nextLine();

}

String toBeEncryptedText="";

File f2=new File(messageFile);

Scanner sc1=new Scanner(f2);

while(sc1.hasNext())

{

toBeEncryptedText=sc1.nextLine();

}

System.out.println("The message to be encrypted is : "+toBeEncryptedText);

String[] messageArray=toBeEncryptedText.split(" ");

String encryptedText="";

for(String s:messageArray)

{

s = decodeingKey+s+decodeingKey;

encryptedText +=s+" ";

}

// System.out.println(encryptedText);

File f3=new File(outputFile);

Scanner sc2=new Scanner(f3);

FileWriter fw1=new FileWriter(f3);

BufferedWriter bw = new BufferedWriter(fw1);

bw.write(encryptedText);

bw.close();

// System.out.println(sc2.nextLine());

File f4=new File(outputFile);

Scanner sc4=new Scanner(f4);

System.out.println("The encrypted message is :");

while(sc4.hasNext())

{

System.out.println(sc4.nextLine());

}

}

catch(FileNotFoundException e)

{

System.out.println("The key file is not found");

}

catch(IOException e)

{

System.out.println("Error in writting the file");

}

}

public static void decrypt( String oneTimePad,String messageFile ,String outputFile )

{

try

{

encodeingKey="";

File f1=new File(oneTimePad);

Scanner sc=new Scanner(f1);

File f2=new File(messageFile);

Scanner sc1=new Scanner(f2);

//System.out.println(sc1.nextLine());

File f3=new File(outputFile);

Scanner sc2=new Scanner(f3);

while(sc.hasNext())

{

encodeingKey += sc.nextLine();

}

if(decodeingKey.equals(encodeingKey))

{

String message="";

while(sc2.hasNext())

{

message+=sc2.nextLine();

}

String[] messageArray=message.split(" ");

String decodeMessage="";

for(String s:messageArray)

{

//System.out.println(s);

int a=s.indexOf(encodeingKey);

int b=s.lastIndexOf(encodeingKey);

decodeMessage += s.substring(a+encodeingKey.length(),b)+" ";

}

File f4=new File(messageFile);

FileWriter fw1=new FileWriter(f4);

BufferedWriter bw = new BufferedWriter(fw1);

bw.write(decodeMessage);

bw.close();

System.out.println("Your decoded message is");

Scanner sc4=new Scanner(f4);

while(sc1.hasNext())

{

System.out.println(sc1.nextLine());

}

}

}

catch(FileNotFoundException e)

{

System.out.println("The key file is not found");

}

catch(IOException e)

{

System.out.println("Error in writting the file");

}

}

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

//please give valid file names..Else it will show file not found message.

encrypt("C:/key.txt", "C:/message.txt","C:/output.txt");

decrypt("C:/key.txt", "C:/message.txt","C:/output.txt");

}


}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote