Advertisement
BarraBod

Simple Encription

Jul 31st, 2016
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.19 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. //main
  4. public class MyFirst{
  5. public static void main(String[] args){
  6.    
  7.    
  8.     int selection = 0;
  9.     System.out.println("Would you like to make an encryption or decryption?\n1 - Encryption\n"
  10.             + "2 = Decrypton");
  11.    
  12.     try{
  13.     Scanner user = new Scanner(System.in);
  14.     selection = user.nextInt();
  15.     }
  16.     catch(Exception e){
  17.         e.getMessage();
  18.        
  19.     }
  20.    
  21.     if(selection == 1){
  22.         Encrypt encrypt = new Encrypt();
  23.        
  24.     }
  25.     else if(selection == 2){
  26.         Decrypt decrypt = new Decrypt();
  27.     }
  28.     else {
  29.         System.out.println("Error!");
  30.     }
  31.  
  32.     }
  33. }
  34.  
  35. //Encryption class
  36.  
  37. public class Encrypt {
  38.  
  39.     private char[] crypt;
  40.     private String userInput;
  41.     private String FinalEncrypt;
  42.     private int int1 = 2;
  43.    
  44.    
  45.     public Encrypt(){
  46.         System.out.println("Enter a string you would like encrypted: ");
  47.         Scanner ui = new Scanner(System.in);
  48.         userInput = ui.nextLine();
  49.        
  50.         crypt = userInput.toCharArray(); // convert string to array
  51.        
  52.         //goes through each char and adds to it, putting it in to a new array
  53.         for(int i = 0; i < crypt.length; i++){
  54.             crypt[i] = (char)(crypt[i] + int1);
  55.             int1 += int1 + 2;
  56.           }
  57.         FinalEncrypt = new String(crypt);
  58.         System.out.println("Encryption: " + FinalEncrypt);
  59.        
  60.     }
  61.    
  62. }
  63.  
  64. //Decryption class
  65.  
  66. public class Decrypt {
  67.    
  68.     private char[] crypt;
  69.     private String userInput;
  70.     private String FinalEncrypt;
  71.     private int int1 = 2;
  72.    
  73.     public Decrypt(){
  74.         System.out.println("Enter a string you would like decrypted: ");
  75.         Scanner ui = new Scanner(System.in);
  76.         userInput = ui.nextLine();
  77.        
  78.         crypt = userInput.toCharArray(); // convert string to array
  79.        
  80.         //goes through each char and adds to it, putting it in to a new array
  81.         for(int i = 0; i < crypt.length; i++){
  82.             crypt[i] = (char)(crypt[i] - int1);
  83.             int1 += int1 + 2;
  84.           }
  85.         FinalEncript = new String(crypt);
  86.         System.out.println("Decrypted text: " + FinalEncrypt);
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement