Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Encryption Write a program that encrypts (or decrypts) a file using an XOR cipher. An XOR cipher encrypts a byte by performing an XOR

Java Encryption

Write a program that encrypts (or decrypts) a file using an XOR cipher. An XOR cipher encrypts a byte by performing an XOR bitwise operation with the byte to be encrypted and a key. In Java, you can perform bitwise XOR using the ^ operator.

First, write a class XorEncryptor that encrypts or decrypts a file using an XOR cipher. Part of the code of the class has been provided for you:

import . . .

/**

An encryptor that encrypts files using a "XOR" cipher.

*/

public class XorCipher

{

private int key;

/**

Constructs an encryptor.

@param aKey the encryption key

*/

public XorCipher(int aKey)

{

key = aKey;

}

/**

Encrypts the contents of a file.

@param inFile the input file

@param outFile the output file

*/

public void encryptFile(String inFile, String outFile)

throws . . .

{

InputStream in = null;

OutputStream out = null;

try

{

in = . . .;

out = . . .;

encryptStream(in, out);

}

finally

{

/** Close the files */

}

}

/**

Encrypts the contents of a stream.

@param in the input stream

@param out the output stream

*/

public void encryptStream(InputStream in, OutputStream out)

throws . . .

{

boolean done = false;

while (!done)

{

int next = in.read();

if (next == -1) { done = true; }

else

{

int c = encrypt(next);

out.write(c);

}

}

}

/**

Encrypts a value.

@param b the value to encrypt

@return the encrypted value

*/

public int encrypt(int b)

{

return . . .;

}

}

1.2) Write a program that asks the user for the name of a file to encrypt (or decrypt) and the name of the output file, and encrypts (or decrypts) the file using an XOR cipher. Note that both operations are performed in exactly the same way. Your program should also ask the user for the encryption key to use.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Conceptual Database Design An Entity Relationship Approach

Authors: Carol Batini, Stefano Ceri, Shamkant B. Navathe

1st Edition

0805302441, 978-0805302448

More Books

Students also viewed these Databases questions

Question

=+ Is secondary industrial action common and/or legal?

Answered: 1 week ago

Question

=+What sanctions are available to employers

Answered: 1 week ago

Question

=+ If strikes occur, are they legally regulated?

Answered: 1 week ago