Question: Using this source code below: - Generate an array of 100 different random strings (50 characters each) - Time how long it takes to encrypt

Using this source code below:

- Generate an array of 100 different random strings (50 characters each)

- Time how long it takes to encrypt all 100 strings using DES

-Time how long it takes to encrypt all 100 strings using AES

- Output the time difference between each crypto system

Source code:

package crypto;

//import java.util.logging.Level; //import java.util.logging.Logger; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.xml.bind.DatatypeConverter; import java.util.*;

public class Crypto { public static void main(String[] args) { //1. Read input plaintext from user as String Scanner input = new Scanner(System.in); // String plainText = "Hello World"; System.out.println("User's input:"); String plainText = input.nextLine(); System.out.println(); System.out.println("Bytes to Hex: "); System.out.println(bytesToHex(plainText.getBytes())); //2. Create SecretKey object SecretKey sharedKey; try { //3. Generate the secret shared key using the getSecretEncryptionKey method sharedKey = getSecretEncryptionKey(); //4. Encrypt the plaintext using aesEncryptText method byte[] encryptedPlainText = aesEncryptText(plainText, sharedKey); //5. Decrypt the generated cipher using the aesDecryptText method String decryptedPlainText = aesDecryptText(encryptedPlainText, sharedKey); //6. Print the cipher data in Hex Form using the bytexToHex method System.out.println(); System.out.println("Cipher data in Hex:"); System.out.println(bytesToHex(encryptedPlainText)); System.out.println(); //7. Print the generated key in Hex Form using the bytesToHex method System.out.println("Generated key in Hex: "); System.out.println(bytesToHex(sharedKey.getEncoded())); System.out.println(); //8. Print the recovered plaintext in Hex Form using the bytesToHex method System.out.println("Recovered plaintext in Hex Form:"); System.out.println(bytesToHex(decryptedPlainText.getBytes())); } catch (Exception ex) { System.out.println("Exception!"); } }

/** * gets the AES encryption key. In real applications, this key should be * safely stored. * @return * @throws Exception */ public static SecretKey getSecretEncryptionKey() throws Exception { //1. Instantiate an object of KeyGenerator class for AES keys KeyGenerator myGenerator = KeyGenerator.getInstance("AES"); //2. Initialize the key to generate 128 bit key SecretKey newKey; //3. Generate the key and store it in a SecretKey object newKey = myGenerator.generateKey(); //4. return the SecretKey object return newKey; }

/** * Encrypts plainText in AES using the secret key * @param plainText * @param secKey * @return * @throws Exception */ public static byte[] aesEncryptText(String plainText, SecretKey secKey) throws Exception { // AES defaults to AES/ECB/PKCS5Padding in Java 7 //1. Instantiate and object of Class Cipher for AES encryption Cipher cipher = Cipher.getInstance("AES"); //2. Initializes the object to ENCYRPT_MODE and the secret key cipher.init(Cipher.ENCRYPT_MODE, secKey); //3. Use the doFinal method of the cipher object to encyprt the plaintext byte[] encryptedText = cipher.doFinal(plainText.getBytes()); //4. Return the encrypted byte array. return encryptedText; }

/** * Decrypts encrypted byte array using the key used for encryption. * @param byteCipherText * @param secKey * @return * @throws Exception */ public static String aesDecryptText(byte[] byteCipherText, SecretKey secKey) throws Exception { // AES defaults to AES/ECB/PKCS5Padding in Java 7 //1. Instantiate and object of Class Cipher for AES encryption Cipher cipher = Cipher.getInstance("AES"); //2. Initializes the object to DECRYPT_MODE and the secret key cipher.init(Cipher.DECRYPT_MODE, secKey); //3. Use the doFinal method of the cipher object to Decrypt the plaintext byte[] decryptedPlainText = cipher.doFinal(byteCipherText); //4. Return the decrypted byte array. return new String(decryptedPlainText); }

/** * Convert a binary byte array into readable hex form * @param toConvert * @return */ private static String bytesToHex(byte[] toConvert) { return DatatypeConverter.printHexBinary(toConvert); }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!