Guest User

Untitled

a guest
Apr 27th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.14 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Encryption {
  4.     /* File: Encryption.java
  5.     * -------------------------------
  6.     * This program will read in a four-digit number and encrypt/decrypt it based on a set algorithm.
  7.     * 1. Replace each digit by (the sum of that digit plus 7) modulus 10.
  8.     * 2. Swap the first digit with the third, and swap the second digit with the forth.
  9.     */
  10.  
  11.     public static void main ( String[] args )
  12.     {
  13.        
  14.         Scanner scan = new Scanner( System.in );
  15.        
  16.         while(true) {
  17.             System.out.printf("SUPER Encryption! Enter a four-digit number and we will encrypt or decrypt it for you.\n");
  18.        
  19.             //These lines will ask the user if he/she wants to encrypt a number or decrypt a number.
  20.             System.out.printf("Would you like to encrypt, or decrypt today? Enter enc or dec.\n");
  21.  
  22.             String EncTest = scan.nextLine();
  23.  
  24.             //Provides error checking in case the user enters invalid input.
  25.             while(EncTest.equalsIgnoreCase("enc") != true && EncTest.equalsIgnoreCase("dec") != true)   {
  26.            
  27.                 System.out.printf("Please answer either enc or dec.\n");
  28.                 EncTest=scan.nextLine();
  29.             }
  30.  
  31.             //This line will ask the user for their input, as an int. Ptext stands for plain text.
  32.             System.out.printf("\nWhat number are you wishing to encrypt or decrypt? Please enter only one.\n");
  33.             String ptext = scan.nextLine();
  34.  
  35.             //Errorchecking if entered number is different than 4.
  36.             while(ptext.length() != 4) {
  37.                 System.out.printf("Please enter a four-digit number.\n");
  38.                 ptext = scan.nextLine();
  39.             }
  40.        
  41.             //Will run the encrypt or decrypt functions.
  42.             if (EncTest.equalsIgnoreCase("enc")){
  43.                     System.out.printf("Your encrypted number is: %s\n", encrypt(ptext));
  44.             }
  45.  
  46.             else{
  47.                 System.out.printf("Your decrypted number is: %s\n", decrypt(ptext));
  48.             }
  49.        
  50.             //Asks the user whether he/she wants to rerun the program.
  51.             System.out.printf("Do you wish to run this program again? Answer yes or no.\n");
  52.             String rerun = scan.nextLine();
  53.        
  54.             //Provides error checking in case the user enters invalid input.
  55.             while(rerun.equalsIgnoreCase("yes") != true && rerun.equalsIgnoreCase("no") != true)    {
  56.                 System.out.printf("Please answer either yes or no.\n");
  57.                 rerun=scan.nextLine();
  58.             }
  59.            
  60.             if(rerun == "no")System.exit(0);
  61.         }
  62.     }
  63.  
  64.  
  65.     /* Method: encrypt
  66.      * This method will help encrypt a four-digit number using the algorithm stated above.
  67.      * Parameters: String to be encrypted.
  68.      * Return: Encrypted String
  69.      */
  70.    
  71. public static String encrypt (String ptext){
  72.  
  73.     //Makes a char array to represent the string ptext.
  74.     char[] etext = ptext.toCharArray();
  75.    
  76.     //Step one of the algorithm. Replace each digit by (the sum of that digit plus 7) modulus 10.
  77.     for(int i = 0; i < ptext.length(); i++) {
  78.        
  79.         //Changes etext[i] to a string, then parses to int, then applies the math.
  80.         //The + 48 is required at the end to parse the values back into decimal integers instead of ASCII values.
  81.         etext[i] =  (char) (((Integer.parseInt (Character.toString (etext[i])) + 7) % 10) + 48);
  82.  
  83.     }
  84.  
  85.     //Step two. To replace the first digit with the third, and swap the second digit with the forth.
  86.     char temp;
  87.    
  88.     temp = etext[0];
  89.     etext[0] = etext[2];
  90.     etext[2] = temp;
  91.    
  92.     temp = etext[2];
  93.     etext[1] = etext[3];
  94.     etext[3] = temp;
  95.    
  96.     return new String(etext);
  97. }
  98.  
  99.  
  100. public static String decrypt (String ptext){
  101.  
  102.     //Makes a char array to represent the string ptext.
  103.         char[] etext = ptext.toCharArray();
  104.        
  105.         //Step one of the algorithm in reverse. Replace each digit by (the sum of that digit plus 3) modulus 10.
  106.         for(int i = 0; i < ptext.length(); i++) {
  107.            
  108.             //Changes etext[i] to a string, then parses to int, then applies the math.
  109.             //The + 48 is required at the end to parse the values back into decimal integers instead of ASCII values.
  110.             etext[i] =  (char) (((Integer.parseInt (Character.toString (etext[i])) + 3) % 10) + 48);
  111.  
  112.         }
  113.  
  114.         //Step two. To replace the first digit with the third, and swap the second digit with the forth.
  115.         char temp;
  116.        
  117.         temp = etext[0];
  118.         etext[0] = etext[2];
  119.         etext[2] = temp;
  120.        
  121.         temp = etext[2];
  122.         etext[1] = etext[3];
  123.         etext[3] = temp;
  124.        
  125.         return new String(etext);
  126.     }
  127.  
  128. }
Add Comment
Please, Sign In to add comment