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

Description: Encryption is the process of converting ordinary text into a form t

ID: 3642118 • Letter: D

Question

Description:
Encryption is the process of converting ordinary text into a form that cannot be read. In assignment 1 you implemented Caesar cipher encryption algorithm. Decryption is the reverse of encryption and takes unreadable encrypted information and restores the original message using the original encryption algorithm.

Instructions:
For this assignment you will write a program to implement a variant of Caesar cipher encryption algorithm and the decryption algorithm. In assignment 1, the Caesar cipher works by shifting the letters of the alphabet forwards e.g., A become B, B becomes a C, C becomes a D...Y becomes a Z and Z becomes an A. The variant algorithm should be able to shift other characters forward by one as well e.g., a number sign # has an ASCII value of 35 and after being encrypted it will become a dollar sign $ with an ASCII value of 36.ASCII codes only run from 0 - 127 so if (in theory) a character with an ASCII value of 127 were encountered the formula should 'wrap' back to the first allowable ASCII value which is 0.
You need to implement the decryption algorithm for this variant algorithm.
Features to be implemented (you will also be graded on other criteria such as coding style and providing program documentation):

1. Your program should prompt the user to enter a phrase to be encrypted. Ideally your program should be able to encrypt more than a single word (words will be separated by spaces).
2. The program will quit when the user enter nothing.
3. The program should display both the message and the encrypted text, and the decrypted text on screen.

Explanation / Answer

import javax.crypto.Cipher; import javax.crypto.BadPaddingException; import javax.crypto.IllegalBlockSizeException; import javax.crypto.KeyGenerator; import java.security.Key; import java.security.InvalidKeyException; import java.util.Scanner; public class EncriptAndDecript { private static String algorithm = "DESede"; private static Key key = null; private static Cipher cipher = null; private static EncriptAndDecript obj = new EncriptAndDecript(); private EncriptAndDecript() { try { key = KeyGenerator.getInstance(algorithm).generateKey(); cipher = Cipher.getInstance(algorithm); } catch (Exception e) { } } public static EncriptAndDecript getInstance() { return obj; } public static byte[] encrypt(String input) throws InvalidKeyException, BadPaddingException, IllegalBlockSizeException { cipher.init(Cipher.ENCRYPT_MODE, key); byte[] inputBytes = input.getBytes(); return cipher.doFinal(inputBytes); } public static String getEncryptStringValue(String input) throws InvalidKeyException, BadPaddingException, IllegalBlockSizeException { return new String(encrypt(input)); } public static String getDecryptStringValue(String input) throws InvalidKeyException, BadPaddingException, IllegalBlockSizeException { byte[] inputArray = input.getBytes(); return new String(decrypt(inputArray)); } public static String decrypt(byte[] encryptionBytes) throws InvalidKeyException, BadPaddingException, IllegalBlockSizeException { cipher.init(Cipher.DECRYPT_MODE, key); byte[] recoveredBytes = cipher.doFinal(encryptionBytes); String recovered = new String(recoveredBytes); return recovered; } public static void main(String args[]) { String cipherText = null; System.out.println("Enter the string to encrypt"); Scanner scanner = new Scanner(System.in); String plainText = scanner.nextLine(); System.out.println("Given input string is : " + plainText); try { cipherText = EncriptAndDecript.getEncryptStringValue(plainText); System.out.println("cipherText(Encrypted text) is " + cipherText); plainText = EncriptAndDecript.getDecryptStringValue(cipherText); System.out.println("plainText (Decrypted text) is " + plainText); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } } }