*IF YOU ARE GOING TO HANDWRITE YOUR RESPONSE, PLEASE WRITE IT LEGIBLY, TYPED COD
ID: 3830734 • Letter: #
Question
*IF YOU ARE GOING TO HANDWRITE YOUR RESPONSE, PLEASE WRITE IT LEGIBLY, TYPED CODE PREFERRED*
Learn how to use public (asymmetric) key encryption in Java to encrypt text of a message.
Theory: Alice wants to securely send a message to Bob. To do that she must obtain Bob's public key (receiving it from Bob or from a 3-d party as was described during the lecture) and encrypt a message with that key. Then she would send encrypted message to Bob who is the only one who can decrypt it with his private key. Bob communicates back to Alice in a symmetric fashion (encrypting a message with her public key).
This assignment is much simpler and does not involve actual sending of a message. You must learn how to use public encryption method in Java (see provided examples). Include in your program debugging statements as specified below so you and I could see how your program works (use "print on screen" method). Here is what should happen:
Step 1: Entered a text from the keyboard or or use a static string, for example "My message is ..."
DEBUG: The message is: "My message is ..." (or print on screen whatever you entered)
Step 2: Use Java method to generate public and private key pair
DEBUG: Public and private keys are generated
Step 3: Encrypt the message with public key, creating the ciphertext of the message
DEBUG: Message is encrypted with public key
Step 4: Decrypt the ciphertext using the private key, creating cleartext
DEBUG: The message text is decrypted with private key
Step 5: Compare the cleartext with the original message
DEBUG: Message is the same, success! (or message is not the same, failure!)
EXIT
Explanation / Answer
Below is the required code
package Chegg;
import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.util.Scanner;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
public class PublicEncryption {
private static void publicEncryption(String message) throws Exception {
// Step 1
System.out.println("The message is :" + message);
// Step 2
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA", "SUN");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");
keyGen.initialize(1024, random);
KeyPair pair = keyGen.generateKeyPair();
PrivateKey priv = pair.getPrivate();
PublicKey pub = pair.getPublic();
System.out.println("Public and private keys are generated");
// step 3
byte[] input = message.getBytes("UTF8");
byte[] secret = encrypt(pub, input);
System.out.println("Message is encrypted with public key");
// step 4
byte[] recoveredMessage = decrypt(priv, secret);
String clearText = recoveredMessage.toString();
System.out.println("The message is decrypted with private key");
// step 5
boolean success = message.equals(clearText);
if (success) {
System.out.println("Success !!!!");
} else {
System.out.println("Failure !!!!");
}
}
public static byte[] encrypt(PublicKey key, byte[] plaintext) throws NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
// Cipher cipher =
// Cipher.getInstance("RSA/ECB/OAEPWithSHA1AndMGF1Padding");
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, key);
return cipher.doFinal(plaintext);
}
public static byte[] decrypt(PrivateKey key, byte[] ciphertext) throws NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA1AndMGF1Padding");
// Cipher cipher =
// Cipher.getInstance("RSA/ECB/OAEPWithSHA1AndMGF1Padding");
cipher.init(Cipher.DECRYPT_MODE, key);
return cipher.doFinal(ciphertext);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("***************Enter message below*************** ");
String message = sc.nextLine();
try {
publicEncryption(message);
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getCause().toString());
}
sc.close();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.