Advertisement
vanbestglitch

Encryption Algorithm Based On Enigma Machine (Using Java)

Jan 26th, 2020
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.35 KB | None | 0 0
  1. public class ActualEnigma {
  2.    
  3. private String newmessage = "This Was Made By Vladimir Alekseev";
  4. /*
  5.     Sorry for any spaghetti code this was made in my sophomore year of high school, so feel free to modify it. Also feel free to view any other projects I made. Look at bottom to see what the methods do.
  6. */
  7. private String code;
  8. private boolean usePlug = false;
  9. private char[] letter;
  10.  
  11. private int[] storedrotors = new int[3];
  12. private int[] rotors = new int[] {10,10,10};
  13. private char[] alphabet = new char[] {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
  14. private char[] plugboard = new char[]{'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
  15.    
  16. public ActualEnigma() {
  17.    
  18. }
  19.  
  20. //helper methods for plugboard
  21. private char replaceLetters(char c) {
  22. for(int i = 0; i < plugboard.length; i++) {
  23.     if(c == alphabet[i]) {
  24.     c = plugboard[i];
  25.     return c;
  26.     }
  27. }
  28. return c;
  29. }
  30.  
  31. private char reversereplaceLetters(char c) {
  32. for(int i = 0; i < plugboard.length; i++) {
  33.     if(c == plugboard[i]) {
  34.     c = alphabet[i];
  35.     return c;
  36.     }
  37. }
  38. return c;
  39. }
  40.  
  41. //encrypts message
  42. public String encrypt(String message) {
  43.     newmessage = "";
  44.     letter = new char[message.length()];
  45.     rotors = new int[] {storedrotors[0], storedrotors[1], storedrotors[2]};
  46.    
  47.     for(int j =0; j < message.length(); j++) {
  48.         if(rotors[2] - 1 >= 1) {
  49.             rotors[2]--;   
  50.         }else if (rotors[1] - 1 >= 1) {
  51.             rotors[1]--;
  52.             rotors[2] = 26;
  53.         }else if(rotors[0] - 1 >= 1) {
  54.             rotors[2] = 26;
  55.             rotors[1] = 26;
  56.             rotors[0]--;
  57.         }else {
  58.             rotors[0] = 26;
  59.             rotors[1] = 26;
  60.             rotors[2] = 26;
  61.         }  
  62.     }
  63.    
  64.    
  65.    
  66.     for(int i = 0; i < letter.length; i ++) {
  67.        
  68.     letter[i] = message.substring(i, i+1).charAt(0);
  69.    
  70.     if(letter[i] == ' ') {
  71.        
  72.         if(rotors[2] + 1 != 27) {
  73.             rotors[2]++;   
  74.         }else if (rotors[1] + 1 != 27) {
  75.             rotors[1]++;
  76.             rotors[2] = 1;
  77.         }else if(rotors[0] + 1 != 27) {
  78.             rotors[2] = 1;
  79.             rotors[1] = 1;
  80.             rotors[0]++;
  81.         }else {
  82.             rotors[0] = 1;
  83.             rotors[1] = 1;
  84.             rotors[2] = 1;
  85.         }
  86.        
  87.     }else {
  88.     letter[i] = replaceLetters(letter[i]);
  89.     letter[i] += (rotors[0] + rotors[1] + rotors[2]);
  90.     if(letter[i] > 90) {
  91.     while(letter[i] > 90) {
  92.     letter[i] -= 26;   
  93.     }
  94.     }
  95.    
  96.     if(rotors[2] + 1 != 27) {
  97.         rotors[2]++;   
  98.     }else if (rotors[1] + 1 != 27) {
  99.         rotors[1]++;
  100.         rotors[2] = 1;
  101.     }else if(rotors[0] + 1 != 27) {
  102.         rotors[2] = 1;
  103.         rotors[1] = 1;
  104.         rotors[0]++;
  105.     }else {
  106.         rotors[0] = 1;
  107.         rotors[1] = 1;
  108.         rotors[2] = 1;
  109.     }
  110.     }
  111.    
  112.     letter[i] = replaceLetters(letter[i]);
  113.     newmessage += letter[i];
  114.        
  115.     }
  116.    
  117.     String strg = "none";
  118.    
  119.     if(usePlug) {
  120.         strg = "";
  121.     for(int p = 0; p < plugboard.length; p++) {
  122.         strg += plugboard[p];  
  123.     }
  124.     }
  125.    
  126.     code = rotors [0] + " " + rotors [1] + " " + rotors [2];
  127.    
  128.     return "Encrypted Message: " + newmessage +  "; Code: " + code + "; Plugboard: " + strg;
  129. }
  130.  
  131. //decrypts message
  132. public String decrypt(String message, String key) {
  133.     newmessage = "";
  134.     letter = new char[message.length()];
  135.     rotors = new int[] {(Integer.parseInt(key.substring(0, 2))),(Integer.parseInt(key.substring(3, 5))),(Integer.parseInt(key.substring(6, 8)))};
  136.    
  137.     for(int j =0; j < message.length(); j++) {
  138.         if(rotors[2] - 1 >= 1) {
  139.             rotors[2]--;   
  140.         }else if (rotors[1] - 1 >= 1) {
  141.             rotors[1]--;
  142.             rotors[2] = 26;
  143.         }else if(rotors[0] - 1 >= 1) {
  144.             rotors[2] = 26;
  145.             rotors[1] = 26;
  146.             rotors[0]--;
  147.         }else {
  148.             rotors[0] = 26;
  149.             rotors[1] = 26;
  150.             rotors[2] = 26;
  151.         }  
  152.     }
  153.    
  154.    
  155.     for(int i = 0; i < letter.length; i ++) {
  156.         letter[i] = message.substring(i, i+1).charAt(0);
  157.         letter[i] = reversereplaceLetters(letter[i]);
  158.        
  159.         if(letter[i] == ' ') {
  160.            
  161.             if(rotors[2] + 1 != 27) {
  162.                 rotors[2]++;   
  163.             }else if (rotors[1] + 1 != 27) {
  164.                 rotors[1]++;
  165.                 rotors[2] = 1;
  166.             }else if(rotors[0] + 1 != 27) {
  167.                 rotors[2] = 1;
  168.                 rotors[1] = 1;
  169.                 rotors[0]++;
  170.             }else {
  171.                 rotors[0] = 1;
  172.                 rotors[1] = 1;
  173.                 rotors[2] = 1;
  174.             }
  175.            
  176.        
  177.         }else {
  178.        
  179.         letter[i] -= (rotors[0] + rotors[1] + rotors[2]);
  180.  
  181.        
  182.         if(letter[i] < 65) {
  183.             while(letter[i] < 65) {
  184.                 letter[i] += 26;   
  185.             }
  186.         }
  187.        
  188.  
  189.         if(rotors[2] + 1 != 27) {
  190.             rotors[2]++;   
  191.         }else if (rotors[1] + 1 != 27) {
  192.             rotors[1]++;
  193.             rotors[2] = 1;
  194.         }else if(rotors[0] + 1 != 27) {
  195.             rotors[2] = 1;
  196.             rotors[1] = 1;
  197.             rotors[0]++;
  198.         }else {
  199.             rotors[0] = 1;
  200.             rotors[1] = 1;
  201.             rotors[2] = 1;
  202.         }
  203.         }
  204.        
  205.         letter[i] = reversereplaceLetters(letter[i]);
  206.         newmessage += letter[i];
  207.            
  208.         }
  209.         code = rotors [0] + " " + rotors [1] + " " + rotors [2];
  210.        
  211.         return "Decrypted Message: " + newmessage;
  212.        
  213. }
  214.  
  215. //sets rotors to random numbers
  216. public void randomizeRotors() {
  217.     rotors = new int[] {(int)(Math.random()*26) + 1,(int)(Math.random()*26) + 1,(int)(Math.random()*26) + 1};  
  218.     storedrotors = new int[] {rotors[0], rotors[1], rotors[2]};
  219. }
  220.  
  221. //sets rotors
  222. public void setRotors(int i, int j, int k) {
  223.     rotors = new int[] {i,j,k};
  224.     storedrotors = new int[] {rotors[0], rotors[1], rotors[2]};
  225.    
  226. }
  227.  
  228. //sets plugboard either with (letter, letter to be replaced, true) or (scrambled alphabet, normal alphabet, false) 
  229. public void setPlugboard(String c, String st, boolean ind) {
  230.     usePlug = true;
  231.     if(ind) {
  232.        
  233.     char lop = c.charAt(0);
  234.     int i = lop -=65;
  235.     System.out.println(i);
  236.    
  237.     char g = st.charAt(0);
  238.     int l = g -= 65;
  239.     System.out.println(l);
  240.    
  241.     plugboard[l] = plugboard[i];
  242.     plugboard[i] = st.charAt(0);
  243.  
  244.    
  245.     }else {
  246.         for(int j = 0; j < st.length(); j++) {
  247.        
  248.             plugboard[j] = c.substring(j,j+1).charAt(0);
  249.        
  250.  
  251.         }
  252.     }
  253.    
  254. }
  255.  
  256.  
  257.  
  258. //prints possible codes based on portions of message (need to set plugboard beforehand
  259.  
  260. public String guessCode(String message, String partofmessage) {
  261. newmessage = "";
  262. letter = new char[message.length()];
  263. rotors = new int[] {1,1,1};
  264. boolean b = false;
  265. int times = 0;
  266. int messages = 0;
  267.  
  268. if(b) {
  269.     return newmessage; 
  270. }else {
  271.     while(!b) {
  272.         newmessage = "";
  273.        
  274.         if(times != 0) {
  275.             if(rotors[2] + 1 != 27) {
  276.                 rotors[2]++;   
  277.             }else if (rotors[1] + 1 != 27) {
  278.                 rotors[1]++;
  279.                 rotors[2] = 1;
  280.             }else if(rotors[0] + 1 != 27) {
  281.                 rotors[2] = 1;
  282.                 rotors[1] = 1;
  283.                 rotors[0]++;
  284.             }else {
  285.                 rotors[0] = 1;
  286.                 rotors[1] = 1;
  287.                 rotors[2] = 1;
  288.     }
  289.     }
  290.    
  291.     for(int i = 0; i < letter.length; i++) {
  292.         letter[i] = message.substring(i, i+1).charAt(0);
  293.         letter[i] = reversereplaceLetters(letter[i]);
  294.        
  295.         if(letter[i] == ' ') {
  296.            
  297.             if(rotors[2] + 1 != 27) {
  298.                 rotors[2]++;   
  299.             }else if (rotors[1] + 1 != 27) {
  300.                 rotors[1]++;
  301.                 rotors[2] = 1;
  302.             }else if(rotors[0] + 1 != 27) {
  303.                 rotors[2] = 1;
  304.                 rotors[1] = 1;
  305.                 rotors[0]++;
  306.             }else {
  307.                 rotors[0] = 1;
  308.                 rotors[1] = 1;
  309.                 rotors[2] = 1;
  310.             }
  311.            
  312.        
  313.         }else {
  314.        
  315.         letter[i] -= (rotors[0] + rotors[1] + rotors[2]);
  316.    
  317.        
  318.         if(letter[i] < 65) {
  319.         while(letter[i] < 65) {
  320.             letter[i] += 26;   
  321.         }
  322.         }
  323.        
  324.         if(rotors[2] + 1 != 27) {
  325.             rotors[2]++;   
  326.         }else if (rotors[1] + 1 != 27) {
  327.             rotors[1]++;
  328.             rotors[2] = 1;
  329.         }else if(rotors[0] + 1 != 27) {
  330.             rotors[2] = 1;
  331.             rotors[1] = 1;
  332.             rotors[0]++;
  333.         }else {
  334.             rotors[0] = 1;
  335.             rotors[1] = 1;
  336.             rotors[2] = 1;
  337.         }
  338.         }
  339.            
  340.         letter[i] = reversereplaceLetters(letter[i]);
  341.         newmessage += letter[i];
  342.        
  343.     }
  344.    
  345.     if(newmessage.contains(partofmessage)) {
  346.         System.out.print("Code: " + rotors [0] + " " + rotors [1] + " " + rotors [2]);
  347.         System.out.println("; Message: " + newmessage);
  348.         messages++;
  349.         times++;
  350.     }else {
  351.         times++;   
  352.     }
  353.    
  354.     if(rotors[0] == 26 & rotors[1] == 26 & rotors[2] == 26) {
  355.         b = true;  
  356.     }
  357.    
  358.     for(int j =0; j < message.length(); j++) {
  359.         if(rotors[2] - 1 >= 1) {
  360.             rotors[2]--;   
  361.         }else if (rotors[1] - 1 >= 1) {
  362.             rotors[1]--;
  363.             rotors[2] = 26;
  364.         }else if(rotors[0] - 1 >= 1) {
  365.             rotors[2] = 26;
  366.             rotors[1] = 26;
  367.             rotors[0]--;
  368.         }else {
  369.             rotors[0] = 26;
  370.             rotors[1] = 26;
  371.             rotors[2] = 26;
  372.         }  
  373.     }
  374.    
  375. }
  376. }
  377. if(messages == 0) {
  378.     return "No Possible Messages or Codes Detected On This Plugboard Setting (either change plugboard or change possible part of message)";
  379. }else {
  380. System.out.println("");
  381.     return messages + " Possible Messages and Codes Detected!";
  382. }
  383. }
  384.  
  385. //End of Enigma Code
  386.  
  387. /*
  388. To Encrypt message:
  389.  
  390. First setup plugboard with .setPlugboard(String original_letter, String letter_to_be_replaced, true) or with
  391. .setPlugboard(String original_alphabet, String scrambled_alphabet, false)
  392.  
  393. Then setup rotors with .setRotors(int num1, int num2, int num3) or .randomizeRotors()
  394.  
  395. Then encrypt message with encrypt(String message)
  396.  
  397. [messages can only be allcaps and without any special characters, unless you want the message to not be decryptable]
  398. [EX. "THIS MESSAGE WITH WORK WITH THE ENIGMA MACHINE"]
  399. [EX. "THIS MeSSAGE will Not WORK WITH THE ENIGMA MACHINE!1!!!!"]
  400.  
  401. To Decrypt message:
  402.  
  403. First setup plugboard with .setPlugboard(String original_letter, String letter_to_be_replaced, true) or with
  404. .setPlugboard(String original_alphabet, String scrambled_alphabet, false)
  405.  
  406. Then decrypt message with encrypt(String message, String key)
  407.  
  408. [key is string out rotors) (EX. "01 01 01"]
  409.  
  410. To Guess Code:
  411.  
  412. First setup you want to test plugboard with .setPlugboard(String original_letter, String letter_to_be_replaced, true) or with .setPlugboard(String original_alphabet, String scrambled_alphabet, false)
  413.  
  414. Then use .guessCode(String message, String part_of_message)
  415.  
  416. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement