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

This needs to be in Java. I have the rest of the program I just need help with t

ID: 3889310 • Letter: T

Question

This needs to be in Java. I have the rest of the program I just need help with the encryption and decryption. The password to use is provided at the bottom of the instructions. After encrypting it should make an encrypted file and leave the original intact, then decrypt and receive another new file identical to the original.

6 – Encrypt file (XOR with password)
This option prompts the user for a password (max 256 bytes long, may contain letters, digits,
other characters) and then prompts the user for a filename and encrypts the content of the
selected file using that password. The encryption method is very simple: just XOR the
password with the file content byte after byte; the password being shorter than the file content,
you must repeat the password as needed.
Example:
passwordpasswordpasswordpasswordpasswordpasswordpass
this is the file content that we need to encrypt now
chiphertext is obtained here by XORing byte to byte
Note1: the user must be prompted for the filename of the encrypted file (containing the
ciphertext) as well, otherwise we would need to overwrite the initial file.
Note2: If no directory was selected an error message must be displayed. If the directory does
not contain either of the files specified by the user, an error message must be displayed. The
filenames do not include any path.
7 – Decrypt file (XOR with password)
This option prompts the user for a password (max 256 bytes long, may contain letters, digits,
other characters) and then prompts the user for a filename and decrypts the content of the
selected file using that password. The decryption method is very simple: just XOR the
password with the file content byte after byte; the password being shorter than the file content,
you must repeat the password as needed.
Example:
passwordpasswordpasswordpasswordpasswordpasswordpass
chiphertext is here …
this is the file content that we had initially
Note1: the user must be prompted for the filename of the decrypted file as well, otherwise we
would need to overwrite the initial file.
Note2: it may seem strange that we are using the same operation (XOR) both for encryption
and for decryption, but this is how XOR (exclusive OR) works.
Note3: If no directory was selected an error message must be displayed. If the directory does
not contain either of the files specified by the user, an error message must be displayed. The
filenames do not include any path.
Testing:
1. You should use this file as the test file for Display, Encryption and Decryption.
2. For encryption, use the following password “Qwertyuiop[123$4$567]”
3. After encrypting this file with the above password, you obtain an encrypted file of the
same length; after decrypting that file with the same password you should obtain an
exact replica of this file, and this must be reflected in the Report document by
appropriate screenshots showing the content of the files involved in these operations.

Explanation / Answer

public static String encrypt(String text) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeySpecException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidParameterSpecException, InvalidAlgorithmParameterException { Cipher cipher = Cipher.getInstance(ENC_ALG); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(KEY_ALG); KeySpec keySpec = new PBEKeySpec(PASSWORD.toCharArray(), SALT, 65536, 256); SecretKey key = keyFactory.generateSecret(keySpec); IvParameterSpec ivSpec = new IvParameterSpec(IV); PBEParameterSpec paramSpec = new PBEParameterSpec(SALT, 0,ivSpec); cipher.init(Cipher.ENCRYPT_MODE, key, paramSpec); byte[] encrypted = cipher.doFinal(text.getBytes()); return Base64.getEncoder().encodeToString(encrypted); } public static String decrypt(String encrypted) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeySpecException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException { Cipher cipher = Cipher.getInstance(ENC_ALG); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(KEY_ALG); KeySpec keySpec = new PBEKeySpec(PASSWORD.toCharArray(), SALT, 65536, 256); SecretKey key = keyFactory.generateSecret(keySpec); IvParameterSpec ivSpec = new IvParameterSpec(IV); PBEParameterSpec paramSpec = new PBEParameterSpec(SALT, 0, ivSpec); cipher.init(Cipher.DECRYPT_MODE, key, paramSpec); byte[] decoded = Base64.getDecoder().decode(encrypted); byte[] decrypted = cipher.doFinal(decoded); return new String(decrypted); }

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