Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Random;
- import java.util.Scanner;
- public class Main {
- private static HashMap<Integer, Character> letters = new HashMap<Integer, Character>();
- private static String key;
- private static ArrayList<Integer> encryptedMessage;
- public static void main(String... args) {
- // Text to Encrypt
- System.out.println("Input Message to encrypt: ");
- // This is were put the new scanner for the console
- Scanner s = new Scanner(System.in);
- String input = s.nextLine();
- String plainText = input;
- // Generate random key
- String key = keyGenerator(plainText.length());
- /**
- * Add all characters to their specific value into a hashmap
- */
- for(int i = 0; i < 26; i++) {
- letters.put(i, (char)(65+i));
- }
- System.out.println("Plain-Text: " + plainText);
- System.out.println("Key: " + key);
- System.out.println("Message encrypted to: " + encrypter(plainText, key));
- System.out.println("Decrypted Message: " + decrypter(encryptedMessage, key) + " - Using Key: " + key);
- }
- /**
- * Generate random key using the size of the plaintext.
- */
- public static String keyGenerator(int size) {
- String randomKey = "";
- Random r = new Random();
- for (int i = 0; i < size; i++) {
- randomKey += (char)(r.nextInt(26) + 'A');
- }
- return randomKey;
- }
- public static String encrypter(String plainText, String key) {
- ArrayList<Integer> splitPlainValue = new ArrayList<>();
- ArrayList<Integer> splitKeyValue = new ArrayList<>();
- ArrayList<Integer> combinedEncryptionValue = new ArrayList<>();
- /**
- * Split the characters and convert them into their respective value then add it to an arrayList.
- */
- int textLenght = plainText.length();
- for (int i = 0; i < textLenght; i++) {
- for (int l = 0; l < letters.size(); l++) {
- if (letters.get(l).equals(plainText.toUpperCase().charAt(i))) {
- splitPlainValue.add(l);
- }
- if (letters.get(l).equals(key.toUpperCase().charAt(i))) {
- splitKeyValue.add(l);
- }
- }
- }
- /**
- * Checks if value goes over 26, if so subtract 26 from integer
- */
- for (int i = 0; i < textLenght; i++) {
- if (splitPlainValue.get(i) + splitKeyValue.get(i) > 26) {
- combinedEncryptionValue.add((splitPlainValue.get(i) + splitKeyValue.get(i)) - 26);
- } else {
- combinedEncryptionValue.add(splitPlainValue.get(i) + splitKeyValue.get(i));
- }
- }
- /**
- * sets the encrypted message to the encryption value then returns the encrypted message as an arrayList.
- */
- encryptedMessage = combinedEncryptionValue;
- return combinedEncryptionValue.toString();
- }
- public static String decrypter(ArrayList<Integer> message, String key) {
- //ArrayList<Character> decryptedMessage = new ArrayList<>();
- String decryptedMessage = "";
- ArrayList<Integer> splitKeyValue = new ArrayList<>();
- /**
- * Converting the key "Text" to corresponding integers.
- */
- int textLenght = key.length();
- for (int i = 0; i < textLenght; i++) {
- for (int l = 0; l < letters.size(); l++) {
- if (letters.get(l).equals(key.toUpperCase().charAt(i))) {
- splitKeyValue.add(l);
- }
- }
- }
- /**
- * Reversing the encryption using the key
- */
- ArrayList<Integer> decryptedMessageValue = new ArrayList<>();
- for (int i = 0; i < textLenght; i++) {
- if (message.get(i) - splitKeyValue.get(i) < 0) {
- decryptedMessageValue.add((message.get(i) - splitKeyValue.get(i)) + 26);
- } else {
- decryptedMessageValue.add(message.get(i) - splitKeyValue.get(i));
- }
- }
- /**
- * Gets the correct letters for the specific values inside the arraylist
- */
- if (!decryptedMessageValue.isEmpty()) {
- for (int i = 0; i < decryptedMessageValue.size(); i++) {
- decryptedMessage += letters.get(decryptedMessageValue.get(i));
- }
- return decryptedMessage.toLowerCase();
- } else {
- return "Error Empty Message";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement