// can someone explain the code line by line shortly.thanks
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import javax.crypto.Cipher;
// Java 8 example for RSA encryption/decryption.
// Uses strong encryption with 2048 key size.
public class GlobalMembers {
public static void main(String[] args) throws Exception {
String plainText = "Hello World!";
// Generate public and private keys using RSA
Map<String, Object> keys = getRSAKeys();
PrivateKey privateKey = (PrivateKey) keys.get("private");
PublicKey publicKey = (PublicKey) keys.get("public");
String encryptedText = encryptMessage(plainText,
privateKey);
String descryptedText = decryptMessage(encryptedText,
publicKey);
System.out.println("input:" + plainText);
System.out.println("encrypted:" + encryptedText);
System.out.println("decrypted:" + descryptedText);
}
// Get RSA keys. Uses key size of 2048.
private static Map<String,Object> getRSAKeys() throws
Exception {
KeyPairGenerator keyPairGenerator =
KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(2048);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
PrivateKey privateKey = keyPair.getPrivate();
PublicKey publicKey = keyPair.getPublic();
Map<String, Object> keys = new
HashMap<String,Object>();
keys.put("private", privateKey);
keys.put("public", publicKey);
return keys;
}
// Decrypt using RSA public key
private static String decryptMessage(String encryptedText,
PublicKey publicKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, publicKey);
return new
String(cipher.doFinal(Base64.getDecoder().decode(encryptedText)));
}
// Encrypt using RSA private key
private static String encryptMessage(String plainText, PrivateKey
privateKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
return
Base64.getEncoder().encodeToString(cipher.doFinal(plainText.getBytes()));
}
}
I have explained each line in the program please follow program
/*******************************GlobalMembers.java**********************************/
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import javax.crypto.Cipher;
// Java 8 example for RSA encryption/decryption.
// Uses strong encryption with 2048 key size.
public class GlobalMembers {
public static void main(String[] args) throws
Exception {
//declare and save the plain text
to string
String plainText = "Hello
World!";
// Generate public and private
keys using RSA
Map<String, Object> keys =
getRSAKeys();//get the keys by calling getRSAKeys method
//get the private key from
map
PrivateKey privateKey =
(PrivateKey) keys.get("private");
//get the public key from map
PublicKey publicKey = (PublicKey)
keys.get("public");
//get the encyptedText by
calling method encyptedMessage by passing plainText and private
key
String encryptedText =
encryptMessage(plainText, privateKey);
//get decryptedText by calling
method decryptedMessage by passing encryptedText and public
key
String descryptedText =
decryptMessage(encryptedText, publicKey);
//print input plain text
System.out.println("input:" +
plainText);
//print encrypted text
System.out.println("encrypted:" +
encryptedText);
//print decrypted text
System.out.println("decrypted:" +
descryptedText);
}
// Get RSA keys. Uses key size of 2048.
private static Map<String, Object> getRSAKeys()
throws Exception {
/**
* Creating KeyPair generator
object
* getInstance() method which
accepts a String variable representing the required key-generating
algorithm
* and returns a KeyPairGenerator
object that generates keys.
*/
KeyPairGenerator keyPairGenerator =
KeyPairGenerator.getInstance("RSA");
/*
* initialize() method is used to
initialize the key pair generator.
* This method accepts an integer
value representing the key size.
*/
keyPairGenerator.initialize(2048);
/*
* Generate the pair of keys
* using the generateKeyPair()
method
*/
KeyPair keyPair =
keyPairGenerator.generateKeyPair();
/*
* Getting the private key from the
key pair
*/
PrivateKey privateKey =
keyPair.getPrivate();
/*
* Getting the public key from the
key pair
*/
PublicKey publicKey =
keyPair.getPublic();
/*
* A Map is used to store the keys
String is the information which is String type
* And The keys are Object
type
*/
Map<String, Object> keys =
new HashMap<String, Object>(); //initialize the map
keys.put("private",
privateKey);//store private key in the map by put method
keys.put("public",
publicKey);//store public key in the map
return keys;
}
// Decrypt using RSA public key
private static String decryptMessage(String
encryptedText, PublicKey publicKey) throws Exception {
//Creating a Cipher by
getInstance() method
Cipher cipher =
Cipher.getInstance("RSA");
//Cipher Mode
cipher.init(Cipher.DECRYPT_MODE,
publicKey);
//return the encrypted text
return new
String(cipher.doFinal(Base64.getDecoder().decode(encryptedText)));
}
// Encrypt using RSA private key
private static String encryptMessage(String plainText,
PrivateKey privateKey) throws Exception {
//Creating a Cipher by
getInstance() method
Cipher cipher =
Cipher.getInstance("RSA");
//Cipher Mode
cipher.init(Cipher.ENCRYPT_MODE,
privateKey);
//return the decrypted text
return
Base64.getEncoder().encodeToString(cipher.doFinal(plainText.getBytes()));
}
}
/*******************output*******************************/
input:Hello World!
encrypted:w3wMlGbKMpVRG01ZdlaQ6FIA+DQBeGcL2f2WYYT/GFBFI/a29/+fRVnf5RjWXmWtyuJsgDSG3arXSW58XnqLcvfZ0uxR9RsyD9s/Itj6bYE8L+3e5vxnZu5Ldp0dOtQ/gsZ6lUyJqfT1iYiVTcBv6gAAv0wx8ctUDvKy9fb8dGLj0ONqiTwO5yiIYXSSSzOzGyM4bQBop40HBsX7UNHAngwuN6z1Wc+/TU3bk8tnxXrrReYM1SJTRyU4HeUFvvPi2UHGVcg/XCMnGAQHuS/kr1MfcP+e8aWpMrTvg2F/Njoy8ykIdGyYY5BIbhZNKUt360SUrqjpbH+DNHAddX14bQ==
decrypted:Hello World!
Thanks a lot, Please let me know if you have any problem..............
// can someone explain the code line by line shortly.thanks import java.security.KeyPair; import ...
Change the following Shift Cipher program so that it uses OOP(constructor, ect.) import java.util.*; import java.lang.*; /** * * @author STEP */ public class ShiftCipher { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here Scanner input = new Scanner(System.in); String plainText; System.out.print("Please enter your string: "); // get message plainText = input.nextLine(); System.out.print("Please enter your shift cipher key: "); // get s int s = input.nextInt(); int...
Hey Guys , I need help with this assignment: This component of the final exam will have two data structures. The two data structures will be a stack and the other will be a hashmap. The hashmap object will be added to the stack every time the user picks an answer. The hashmap will have a key and value. The key will be the timestamp (system time when the user selected a choice) and the value will be the choice...
How to complete this methods: import java.io.FileInputStream; import java.io.FileNotFoundException; import java.time.LocalDate; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Scanner; import java.util.Set; /** * Utility class that deals with all the other classes. * * @author EECS2030 Summer 2019 * */ public final class Registrar { public static final String COURSES_FILE = "Courses.csv"; public static final String STUDENTS_FILE = "Students.csv"; public static final String PATH = System.getProperty("java.class.path"); /** * Hash table to store the list of students using their...
Can someone help me with this code, I not really understanding how to do this? import java.util.ArrayList; import java.util.HashMap; import java.util.Map; /** * @version Spring 2019 * @author Kyle */ public class MapProblems { /** * Modify and return the given map as follows: if the key "a" has a value, set the key "b" to * have that value, and set the key "a" to have the value "". Basically "b" is confiscating the * value and replacing it...
Convert Code from Java to C++ Convert : import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Template { public static void main(String args[]) { Scanner sc1 = new Scanner(System.in); //Standard input data String[] line1 = sc1.nextLine().split(","); //getting input and spliting on basis of "," String[] line2 = sc1.nextLine().split(" "); //getting input and spiliting on basis of " " Map<String, String> mapping = new HashMap<String, String>(); for(int i=0;i<line1.length;i++) { mapping.put(line1[i].split("=")[0], line1[i].split("=")[1]); //string in map where in [] as key and...
Consider the following client class: import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Set; public class PresidentsMain { public static void main(String[] args) { Map<String, String> PresidentsOfTheUnitedStates = new HashMap<String, String>(); PresidentsOfTheUnitedStates.put("George Washington", "Unaffiliated"); PresidentsOfTheUnitedStates.put("John Adams", "Federalist"); PresidentsOfTheUnitedStates.put("Thomas Jefferson", "Democratic-Republican"); PresidentsOfTheUnitedStates.put("James Madison", "Democratic-Republican"); PresidentsOfTheUnitedStates.put("James Monroe", "Democratic-Republican"); PresidentsOfTheUnitedStates.put("John Quincy Adams", "Democratic-Republican"); PresidentsOfTheUnitedStates.put("Andrew Jackson", "Democratic"); PresidentsOfTheUnitedStates.put("Martin Van Buren", "Democratic"); PresidentsOfTheUnitedStates.put("William Henry Harrison", "Whig"); PresidentsOfTheUnitedStates.put("John Tyler", "Whig"); } } } Extend given client class: Implement a static method called FilterMapByValue, that takes...
Must comment on the code at every "/* Comment Here */" section about what the code is doing. import java.io.*; import org.xml.sax.*; import org.xml.sax.helpers.DefaultHandler; import javax.xml.parsers.SAXParserFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import java.util.Map; import java.util.HashMap; public class Configuration extends DefaultHandler { private Map map; private String configurationFile; /* Comment Here */ public Configuration(String configurationFile) throws ConfigurationException { this.configurationFile = configurationFile; map = new HashMap(); try { // Use the default (non-validating) parser SAXParserFactory factory = SAXParserFactory.newInstance();...
Write code for RSA encryption package rsa; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class RSA { private BigInteger phi; private BigInteger e; private BigInteger d; private BigInteger num; public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); System.out.println("Enter the message you would like to encode, using any ASCII characters: "); String input = keyboard.nextLine(); int[] ASCIIvalues = new int[input.length()]; for (int i = 0; i < input.length(); i++) { ASCIIvalues[i] = input.charAt(i); } String ASCIInumbers...
Modify the below Java program to use exceptions if the password is wrong (WrongCredentials excpetion). import java.util.HashMap; import java.util.Map; import java.util.Scanner; class Role { String user, password, role; public Role(String user, String password, String role) { super(); this.user = user; this.password = password; this.role = role; } /** * @return the user */ public String getUser() { return user; } /** * @param user the user to set */ public void setUser(String user) { this.user = user; } /** *...
Here is my assignment: --------------------------------------------------- Consider the following client class: import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Set; public class PresidentsMain { public static void main(String[] args) { Map<String, String> PresidentsOfTheUnitedStates = new HashMap<String, String>(); PresidentsOfTheUnitedStates.put("George Washington", "Unaffiliated"); PresidentsOfTheUnitedStates.put("John Adams", "Federalist"); PresidentsOfTheUnitedStates.put("Thomas Jefferson", "Democratic-Republican"); PresidentsOfTheUnitedStates.put("James Madison", "Democratic-Republican"); PresidentsOfTheUnitedStates.put("James Monroe", "Democratic-Republican"); PresidentsOfTheUnitedStates.put("John Quincy Adams", "Democratic-Republican"); PresidentsOfTheUnitedStates.put("Andrew Jackson", "Democratic"); PresidentsOfTheUnitedStates.put("Martin Van Buren", "Democratic"); PresidentsOfTheUnitedStates.put("William Henry Harrison", "Whig"); PresidentsOfTheUnitedStates.put("John Tyler", "Whig"); } } } Extend given client class: Implement a static...