Advertisement
Guest User

Untitled

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