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

In Java Language To begin, visit https://docs.oracle.com/javase/7/docs/api/javax

ID: 3799378 • Letter: I

Question

 In Java Language To begin, visit https://docs.oracle.com/javase/7/docs/api/javax/crypto/package-summary.html for a full documentation of the javax.crypto API classes. The classes that you will need to complete this project are: KeyGenerator and Cipher classes. Provided with this project description a Crypto Java Class Template that has all the methods you need to implement predefined and well documented. You need to complete the implementation of the four methods, run and test your program.   Extra Credit (Up to 10 points)  - 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  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;  public class Crypto {          public static void main(String[] args) {         //1. Read input plaintext from user as String         //2. Create SecretKey object                  try {             //3. Generate the secret shared key using the getSecretEncryptionKey method             //4. Encrypt the plaintext using  aesEncryptText method             //5. Decrypt the generated cipher using the aesDecryptText method             //6. Print the cipher data in Hex Form using the bytexToHex method             //7. Print the generated key in Hex Form using the bytesToHex method             //8. Print the recovered plaintext in Hex Form using the bytesToHex method         } 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         //2. Initialize the key to generate 128 bit key         //3. Generate the key and stopre it in a SecretKey object         //4. return the SecretKey object     }      /**      * 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         //2. Initializes the object to ENCYRPT_MODE and the secret key         //3. Use the doFinal method of the cipher object to encyprt the plaintext         //4. Return the encrypted byte array.      }      /**      * 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         //2. Initializes the object to DECRYPT_MODE and the secret key         //3. Use the doFinal method of the cipher object to Decrypt the plaintext         //4. Return the decrypted byte array.      }      /**      * Convert a binary byte array into readable hex form      * @param toConvert      * @return      */     private static String bytesToHex(byte[] toConvert) {         return DatatypeConverter.printHexBinary(toConvert);     } } 

Explanation / Answer

// Crypto.java

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) {
Scanner scan = new Scanner(System.in);

//1. Read input plaintext from user as String
System.out.print("Input Plain text: ");
String text = scan.nextLine();
//2. Create SecretKey object
SecretKey key;
  
try {
//3. Generate the secret shared key using the getSecretEncryptionKey method
key = getSecretEncryptionKey();

//4. Encrypt the plaintext using aesEncryptText method
byte[] textCipher = aesEncryptText(text, key);
//5. Decrypt the generated cipher using the aesDecryptText method
String decryptedText = aesDecryptText(textCipher, key);
//6. Print the cipher data in Hex Form using the bytexToHex method
System.out.println("Encrypted Text (Hex Form):"+bytesToHex(textCipher));
//7. Print the generated key in Hex Form using the bytesToHex method
System.out.println("AES Key (Hex Form):"+bytesToHex(key.getEncoded()));
//8. Print the recovered plaintext in Hex Form using the bytesToHex method
System.out.println("Descrypted Text:"+decryptedText);
} 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 g = KeyGenerator.getInstance("AES");
//2. Initialize the key to generate 128 bit key
g.init(128);
//3. Generate the key and stopre it in a SecretKey object
SecretKey key = g.generateKey();
//4. return the SecretKey object
return key;
}

/**
* 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 asc = Cipher.getInstance("AES");
//2. Initializes the object to ENCYRPT_MODE and the secret key
asc.init(Cipher.ENCRYPT_MODE, secKey);
//3. Use the doFinal method of the cipher object to encyprt the plaintext
byte[] bcText = asc.doFinal(plainText.getBytes());
//4. Return the encrypted byte array.
return bcText;
}

/**
* 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 asCipher = Cipher.getInstance("AES");
//2. Initializes the object to DECRYPT_MODE and the secret key
asCipher.init(Cipher.DECRYPT_MODE, secKey);
//3. Use the doFinal method of the cipher object to Decrypt the plaintext
byte[] bytePlainText = asCipher.doFinal(byteCipherText);
//4. Return the decrypted byte array.
return new String(bytePlainText);
}

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


/*
output:

Input Plain text: Hello World
Encrypted Text (Hex Form):A4B4571D83BCCAD4F5F87C0A60776E14
AES Key (Hex Form):707866CC245572CE8A64A19ED8E81F8D
Descrypted Text:Hello World


*/

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