Question: 1) Explain the code with comments wherever I have indicated that you do so. 2) Fill in the decryption code fill-in-the-blanks using my comments above
1) Explain the code with comments wherever I have indicated that you do so.
2) Fill in the decryption code fill-in-the-blanks using my comments above each line. 1) Explain the code with comments wherever I have indicated that you do so. 2) Fill in the decryption code fill-in-the-blanks using my comments above each line.
A) Run the code as is first. It should compile. Note that to run it, youll need to provide the two filenames youd like to use for your encryption and decryption files at the command line as follows: java CryptoTest file1 file2. If you dont, youre receive an error. If youre using DrJava, you may use the Interactions Pane to run this command
B)Figure out the functionality of the encryption and decryption methods by looking up documentation of the packages that are used
C)Verify that the encryption method is working youll see the output in the first filename youve sent in as an argument.
D)Fill in the decryption code by mirroring the encryption code as indicated. After youve got it running, test the program again. The second filename you send it in an argument will contain the decrypted text.
E)Change the plaintext to something of your choosing and test again. Does it still work
F)Include any citations for any code or comments/explanations that you use or adapt off the Internet or from another textor from a classmateor anywhere else!
// Why are these packages needed in this program? // Comment above each one with your answer. import java.util.*; import java.nio.file.Files; import java.nio.file.Paths; import javax.crypto.*; import java.io.FileNotFoundException; import java.io.IOException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; public class CryptoEngine { // What is the function of these four variables? // Comment above each one with your answer. KeyGenerator keyGen; String encFile, decFile; SecretKey secKey; Cipher aesCipher; CryptoEngine(String inFile, String outFile) throws NoSuchAlgorithmException, NoSuchPaddingException { encFile = inFile; decFile = outFile; keyGen = KeyGenerator.getInstance("AES"); keyGen.init(128); secKey = keyGen.generateKey(); aesCipher = Cipher.getInstance("AES"); } public void encrypt() throws InvalidKeyException, IOException { byte[] byteText = "Include your plaintext to be encrypted here".getBytes(); aesCipher.init(Cipher.ENCRYPT_MODE, secKey); byte[] byteCipherText = null; try { byteCipherText = aesCipher.doFinal(byteText); } catch (IllegalBlockSizeException | BadPaddingException e) { e.printStackTrace(); } Files.write(Paths.get(encFile), byteCipherText); } // You will decrypt the encrypted file using the same general principles // For each line that you supply, leave a detailed comment of what that line is doing, including the functionality of the methods and variables public void decrypt() throws IOException, InvalidKeyException { // assign "Files.readAllBytes(Paths.get(encFile))" to the cipherText array (fill in the blank) // byte[] cipherText = ____________ // call aesCipher.init as in the encrypt method, but this time, you will use DECRYPT_MODE! // _______________________________________________ // declare and initialize a byte array just like in the encrypt method, but this time, call it bytePlainText // _______________________________________________ // I have commented out this try-catch block to make your code compilable, but you'll need to decomment it after filling in the code // try { // Decrypt the cipherText byte array with the same aesCipher.doFinal method as in encrypt method // But this time, the byte arrays will be reversed! // ___________________________________________ // } catch (IllegalBlockSizeException | BadPaddingException e) { // e.printStackTrace(); // } // Write your output to a file, similar to the Files.write method in the encrypt method (but be careful to use the correct byteArray and file) // _________________________________________________ } }
public class CryptoTest { public static void main(String[] args) throws Exception { // What's going on in the two lines below? // Explain with a comment. // I've not handled the error in which the args[] array is null. // So, if you don't call the program correctly at the command line, you'll get an error. // Try to handle this in some way. String inFile = args[0]; String outFile = args[1]; CryptoEngine cryptoEngine = new CryptoEngine(inFile, outFile); cryptoEngine.encrypt(); cryptoEngine.decrypt(); } } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
