Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.62 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.HashMap;
  3. import java.util.Random;
  4. import java.util.Scanner;
  5.  
  6. public class Main {
  7. private static HashMap<Integer, Character> letters = new HashMap<Integer, Character>();
  8. private static String key;
  9. private static ArrayList<Integer> encryptedMessage;
  10.  
  11. public static void main(String... args) {
  12. // Text to Encrypt
  13. System.out.println("Input Message to encrypt: ");
  14.  
  15. // This is were put the new scanner for the console
  16. Scanner s = new Scanner(System.in);
  17. String input = s.nextLine();
  18. String plainText = input;
  19. // Generate random key
  20. String key = keyGenerator(plainText.length());
  21.  
  22. /**
  23. * Add all characters to their specific value into a hashmap
  24. */
  25. for(int i = 0; i < 26; i++) {
  26. letters.put(i, (char)(65+i));
  27. }
  28.  
  29. System.out.println("Plain-Text: " + plainText);
  30. System.out.println("Key: " + key);
  31. System.out.println("Message encrypted to: " + encrypter(plainText, key));
  32. System.out.println("Decrypted Message: " + decrypter(encryptedMessage, key) + " - Using Key: " + key);
  33. }
  34.  
  35. /**
  36. * Generate random key using the size of the plaintext.
  37. */
  38. public static String keyGenerator(int size) {
  39. String randomKey = "";
  40. Random r = new Random();
  41. for (int i = 0; i < size; i++) {
  42. randomKey += (char)(r.nextInt(26) + 'A');
  43. }
  44. return randomKey;
  45. }
  46.  
  47. public static String encrypter(String plainText, String key) {
  48. ArrayList<Integer> splitPlainValue = new ArrayList<>();
  49. ArrayList<Integer> splitKeyValue = new ArrayList<>();
  50. ArrayList<Integer> combinedEncryptionValue = new ArrayList<>();
  51.  
  52. /**
  53. * Split the characters and convert them into their respective value then add it to an arrayList.
  54. */
  55. int textLenght = plainText.length();
  56. for (int i = 0; i < textLenght; i++) {
  57. for (int l = 0; l < letters.size(); l++) {
  58. if (letters.get(l).equals(plainText.toUpperCase().charAt(i))) {
  59. splitPlainValue.add(l);
  60. }
  61. if (letters.get(l).equals(key.toUpperCase().charAt(i))) {
  62. splitKeyValue.add(l);
  63. }
  64. }
  65. }
  66.  
  67. /**
  68. * Checks if value goes over 26, if so subtract 26 from integer
  69. */
  70. for (int i = 0; i < textLenght; i++) {
  71. if (splitPlainValue.get(i) + splitKeyValue.get(i) > 26) {
  72. combinedEncryptionValue.add((splitPlainValue.get(i) + splitKeyValue.get(i)) - 26);
  73. } else {
  74. combinedEncryptionValue.add(splitPlainValue.get(i) + splitKeyValue.get(i));
  75. }
  76. }
  77.  
  78. /**
  79. * sets the encrypted message to the encryption value then returns the encrypted message as an arrayList.
  80. */
  81. encryptedMessage = combinedEncryptionValue;
  82. return combinedEncryptionValue.toString();
  83. }
  84.  
  85. public static String decrypter(ArrayList<Integer> message, String key) {
  86. //ArrayList<Character> decryptedMessage = new ArrayList<>();
  87. String decryptedMessage = "";
  88. ArrayList<Integer> splitKeyValue = new ArrayList<>();
  89.  
  90. /**
  91. * Converting the key "Text" to corresponding integers.
  92. */
  93. int textLenght = key.length();
  94. for (int i = 0; i < textLenght; i++) {
  95. for (int l = 0; l < letters.size(); l++) {
  96. if (letters.get(l).equals(key.toUpperCase().charAt(i))) {
  97. splitKeyValue.add(l);
  98. }
  99. }
  100. }
  101.  
  102. /**
  103. * Reversing the encryption using the key
  104. */
  105. ArrayList<Integer> decryptedMessageValue = new ArrayList<>();
  106. for (int i = 0; i < textLenght; i++) {
  107. if (message.get(i) - splitKeyValue.get(i) < 0) {
  108. decryptedMessageValue.add((message.get(i) - splitKeyValue.get(i)) + 26);
  109. } else {
  110. decryptedMessageValue.add(message.get(i) - splitKeyValue.get(i));
  111. }
  112. }
  113.  
  114. /**
  115. * Gets the correct letters for the specific values inside the arraylist
  116. */
  117. if (!decryptedMessageValue.isEmpty()) {
  118. for (int i = 0; i < decryptedMessageValue.size(); i++) {
  119. decryptedMessage += letters.get(decryptedMessageValue.get(i));
  120. }
  121. return decryptedMessage.toLowerCase();
  122. } else {
  123. return "Error Empty Message";
  124. }
  125. }
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement