Advertisement
Guest User

elvis java 2

a guest
Nov 16th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.30 KB | None | 0 0
  1. package railway;
  2.  
  3. import java.util.regex.Pattern;
  4.  
  5. import com.sun.org.apache.xml.internal.security.encryption.EncryptedKey;
  6.  
  7. import sun.applet.resources.MsgAppletViewer;
  8.  
  9. //import com.sun.java_cup.internal.runtime.Scanner;
  10. import java.util.*;
  11.  
  12. public class Railwayproject {
  13.     String msg = new String();
  14.     //initalzing and making key and choice variables global
  15.     int key = 0;
  16.     int choice = 0;
  17.  
  18.     static Railwayproject r = new Railwayproject();
  19.     //initializing array list varraribles
  20.     //initializing top
  21.     ArrayList<Character> top = new ArrayList<>();
  22.     //initializing bot
  23.     ArrayList<Character> bot = new ArrayList<>();
  24.     //initializing encrypt
  25.     ArrayList<Character> encrypt = new ArrayList<>();
  26.     ArrayList<Character> encrypted = new ArrayList<>();
  27.     ArrayList<Character> decrypted = new ArrayList<>();
  28.    
  29.  
  30.     public static void main(String[] args) {       
  31.         //creating the title
  32.         System.out.printf("%50s", "Welcome to Rail Fence Cipher!");
  33.         //showing the users the options
  34.         System.out.println("\n\nPlease select one of the following options:");
  35.         System.out.println("");
  36.         //1 is for encryption option
  37.         System.out.println("1 - Encrypt a message");
  38.         //2 is for decryption option
  39.         System.out.println("2 - Decrypt a message");
  40.         //3 is for brute force option
  41.         System.out.println("3 - Brute force");
  42.        
  43.            
  44.         //starts
  45.         while (1 == 1) {
  46.             if (r.messageControl(1))break;
  47.         }      
  48.  
  49.    
  50.    
  51.     }
  52.    
  53.     public boolean messageControl(int state) {
  54.         //initializing scanner
  55.         Scanner scan = new Scanner(System.in);
  56.         //initializing boolean
  57.         boolean bol = false;
  58.         //initializing pattern
  59.         Pattern p = Pattern.compile("[$&+,:;=\\\\?@#|/'<>.^*()%-]");
  60.        
  61.         if (state == 1) {
  62.             if (choice == 1 ||choice == 2) {
  63.                 bol = false;
  64.             }
  65.            
  66.             do{
  67.                
  68.                 System.out.println("\nEnter your choice: ");
  69.                 try {  
  70.                 choice = scan.nextInt();
  71.                 bol = (choice >= 1 && choice <= 3);
  72.                
  73.                 //decrypt
  74.                 if(choice == 2 ) {
  75.                     while (1 == 1) {
  76.                         if (r.messageControl(2))break;
  77.                     }
  78.                     while (1 == 1) {
  79.                         if (r.messageControl(3))break;
  80.                     }
  81.                     r.formatEncrypted(msg);
  82.                     decrypted = r.decrypt(encrypted);
  83.                 }
  84.                
  85.                 //encrypt
  86.                 if(choice == 1) {
  87.                     while (1 == 1) {
  88.                         if (r.messageControl(2))break;
  89.                     }
  90.                     while (1 == 1) {
  91.                         if (r.messageControl(3))break;
  92.                     }
  93.                    
  94.                     //calling method zig zag
  95.                     r.twoArray(top, bot);
  96.                     //outputting results
  97.                     System.out.println(top);
  98.                     System.out.println( bot);
  99.                     r.prepareEncrypt(top, bot, encrypt);
  100.                    
  101.                     ArrayList<Character> encrypted = r.encrypt(encrypt);
  102.                    
  103.                 }
  104.                
  105.                 if(choice == 3 ) {
  106.                     //brute force stuff here
  107.                     return true;
  108.                 }
  109.                
  110.                 if (!bol) {
  111.                 System.out.println("Invalid choice! Please enter a number between 1-3.");
  112.                 return false;
  113.                 }
  114.                
  115.                 }
  116.                 catch (Exception e){
  117.                     System.out.println("Invalid key! Please enter an integer");
  118.                     return false;
  119.                 }
  120.                 return true;
  121.             }while (bol == false);
  122.         }
  123.        
  124.         if(state == 2) {
  125.             do{
  126.                 System.out.println("Enter your message: ");
  127.                 try {
  128.                     msg = scan.nextLine();
  129.                     if (msg.matches(".*\\d.*") || p.matcher(msg).find()) {
  130.                             System.out.println("no nums or special chars");
  131.                             return false;
  132.                             }
  133.                 }
  134.                 catch (Exception e){
  135.                     System.out.println("Invalid please enter a message");
  136.                     return false;
  137.                 }
  138.                 return true;
  139.             }while (bol == false);
  140.         }
  141.        
  142.         if(state == 3) {
  143.             do{
  144.                 System.out.println("\nEnter the key number : ");
  145.                 try {
  146.                 key = scan.nextInt();
  147.                 bol = (key >= 1 && key <= 26);
  148.                 if (!bol) {
  149.                 System.out.println("Invalid key! Please enter a number between 1-26.");
  150.                 return false;
  151.                 }  
  152.                 }
  153.                 catch (InputMismatchException ex){
  154.                     System.out.println("Invalid key! Please enter an integer");
  155.                     return false;
  156.                 }
  157.                 return true;
  158.             }while (bol == false);
  159.         }
  160.        
  161.         return false;
  162.     }
  163.    
  164.    
  165.     public void twoArray(ArrayList<Character> top, ArrayList<Character> bot) {
  166.  
  167.         for (int i = 0; i <= msg.length() - 1; i++)
  168.         {
  169.             if (i == 0) {
  170.                  top.add(msg.charAt(i));
  171.             }
  172.            
  173.             else if (Character.isWhitespace(msg.charAt(i))) {
  174.                 if ((i & 1) == 0) {
  175.                     top.add(' ');
  176.                     top.add(' ');
  177.                 }
  178.                 else {
  179.                     bot.add(' ');
  180.                     bot.add(' ');
  181.                 }
  182.             }
  183.            
  184.             else if ((i & 1) == 0) {
  185.             top.add(' ');
  186.             top.add(msg.charAt(i));
  187.             }
  188.            
  189.             else { 
  190.             bot.add(' ');
  191.             bot.add(msg.charAt(i));
  192.             }
  193.         }
  194.         if (bot.size() > top.size())top.add(' ');
  195.         else bot.add(' ');
  196.        
  197.     }
  198.    
  199.    
  200.    
  201.    
  202.     public void prepareEncrypt(ArrayList<Character> top, ArrayList<Character> bot, ArrayList<Character> encrypt){
  203.        
  204.         for (int i = 0; i <= top.size() - 1; i++) {
  205.             if (Character.isWhitespace(top.get(i))){
  206.                 if (Character.isWhitespace(bot.get(i))){   
  207.                         encrypt.add(' ');
  208.                 }
  209.             }
  210.            
  211.             else {
  212.                 encrypt.add(top.get(i));
  213.             }
  214.         }
  215.        
  216.        
  217.         for (int i = 0; i <= bot.size() - 1; i++) {
  218.             if (Character.isWhitespace(bot.get(i))){
  219.                 if (Character.isWhitespace(top.get(i))){   
  220.                     encrypt.add(' ');
  221.                 }
  222.             }
  223.            
  224.             else {
  225.                 encrypt.add(bot.get(i));
  226.             }
  227.             }
  228.        
  229.        
  230.     }
  231.    
  232.     public ArrayList<Character> encrypt(ArrayList<Character> encrypt) {
  233.        
  234.         ArrayList<Character> encrypted = new ArrayList<>();
  235.        
  236.         for (int i=0; i<encrypt.size(); i++)
  237.         {
  238.            if (Character.isWhitespace(encrypt.get(i))){
  239.                encrypted.add(' ');
  240.            }
  241.             else if (Character.isUpperCase(encrypt.get(i)))
  242.             {
  243.                 char ch = (char)(((int)encrypt.get(i) +
  244.                                   key - 65) % 26 + 65);
  245.                 encrypted.add(ch);
  246.             }
  247.                
  248.            else
  249.             {
  250.                 char ch = (char)(((int)encrypt.get(i) +
  251.                                   key - 97) % 26 + 97);
  252.                 encrypted.add(ch);
  253.        
  254.             }
  255.            
  256.            
  257.         }
  258.         System.out.println("Your encrypted message is:");
  259.         System.out.println(encrypted);
  260.         return encrypted;
  261.      }
  262.        
  263.      
  264.     public void formatEncrypted(String msg) {
  265.         for (int i = 0; i <= msg.length() - 1; i++) {
  266.             if (Character.isWhitespace(msg.charAt(i))) {
  267.                 encrypted.add(' ');
  268.                 }
  269.             else encrypted.add(msg.charAt(i));
  270.         }
  271.     }  
  272.        
  273.  
  274.     public ArrayList<Character> decrypt(ArrayList<Character> encrypted) {
  275.        
  276.         ArrayList<Character> decrypted = new ArrayList<>();
  277.          
  278.             if (top.size() > bot.size())bot.add(' ');
  279.             else top.add(' ');
  280.         for (int i=0; i<encrypted.size(); i++)
  281.         {
  282.            if (Character.isWhitespace(encrypted.get(i))){  
  283.                decrypted.add(' ');
  284.            }
  285.             else if (Character.isUpperCase(encrypted.get(i)))
  286.             {
  287.                 char ch = (char)(((int)encrypted.get(i) +
  288.                                   (-key) - 65) % 26 + 65);
  289.                 decrypted.add(ch);
  290.             }
  291.                
  292.            else
  293.             {
  294.                 char ch = (char)(((int)encrypted.get(i) +
  295.                                   (-key) - 97) % 26 + 97);
  296.                 decrypted.add(ch);
  297.        
  298.             }
  299.        
  300.  
  301.             }
  302.        
  303.         System.out.println("Your decrpyed message is:");
  304.         System.out.println(decrypted);
  305.         return decrypted;
  306.        
  307.     }
  308.    
  309.     public void formatMessage(ArrayList<Character> decrypted) {
  310.        
  311.     }
  312.    
  313.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement