*IF YOU ARE GOING TO WRITE YOUR RESPONSE, PLEASE WRITE IT LEGIBLY, TYPED CODE PR
ID: 3830732 • Letter: #
Question
*IF YOU ARE GOING TO WRITE YOUR RESPONSE, PLEASE WRITE IT LEGIBLY, TYPED CODE PREFERRED*
Learn how to use public (asymmetric) key encryption in Java to authenticate the sender (digital signature).
Theory: Alice wants to assure Bob that the message comes from her, so she "signs" it by encrypting the message with her private key. Only her public key can decrypt the message, so Bob, knowing Alice's public key, can do it – but so can anyone else (public key is known to everybody).
Of course in a real life program Alice would send the message, encrypted with her private key and her public key to Bob, who would decrypt it with her public key to make sure that it was sent by Alice (we discussed that algorithm during the lecture).
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: Read a message entered on the keyboard or or use a static string, for example "I am Alice"
DEBUG: The message is: "I am Alice" (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 private key, creating the ciphertext
DEBUG: Message is encrypted with private key
Step 4: Decrypt the ciphertext using the public key, creating cleartext
DEBUG: Digital signature is decrypted with public 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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package solution;
/**
*
* @author SuNiL SiNgH
*/
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Arrays;
import javax.crypto.Cipher;
public class PubPrivKey {
public static void main(String args[]) throws NoSuchAlgorithmException{
String plainText="I am alice",cipherText="",decryptedText="";
byte[] cip;
System.out.println("Entered text is ="+plainText);
// Generate a 1024-bit Digital Signature Algorithm (DSA) key pair
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(1024);
KeyPair keypair = keyGen.genKeyPair();
PrivateKey privateKey = keypair.getPrivate();
System.out.println(privateKey);
PublicKey publicKey = keypair.getPublic();
System.out.println(publicKey);
System.out.println("Public and private key is generated");
//calling encrypt function with plaintext and private key
cip=encrypt(plainText,privateKey);
cipherText=cip.toString();
System.out.println("Ciphertext is ="+cipherText);
//calling decrypt function with ciphertext and public key
decryptedText=decrypt(cip,publicKey);
System.out.println("Decryptedtext is ="+decryptedText);
System.out.println("Digital signature decrypted");
if(plainText.equals(decryptedText)){
System.out.println("Message is the same, success!");
}
}
public static byte[] encrypt(String text, PrivateKey key) {
byte[] cipText = null;
try {
// get an RSA cipher object and print the provider
final Cipher cipher = Cipher.getInstance("RSA");
// encrypt the plain text using the private key
cipher.init(Cipher.ENCRYPT_MODE, key);
cipText = cipher.doFinal(text.getBytes());
} catch (Exception e) {
e.printStackTrace();
}
return cipText;
}
public static String decrypt(byte[] text, PublicKey key) {
byte[] decText = null;
try {
// get an RSA cipher object and print the provider
final Cipher cipher = Cipher.getInstance("RSA");
// decrypt the text using the public key
cipher.init(Cipher.DECRYPT_MODE, key);
decText = cipher.doFinal(text);
} catch (Exception ex) {
ex.printStackTrace();
}
return new String(decText);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.