Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Encryption {
- /* File: Encryption.java
- * -------------------------------
- * This program will read in a four-digit number and encrypt/decrypt it based on a set algorithm.
- * 1. Replace each digit by (the sum of that digit plus 7) modulus 10.
- * 2. Swap the first digit with the third, and swap the second digit with the forth.
- */
- public static void main ( String[] args )
- {
- Scanner scan = new Scanner( System.in );
- while(true) {
- System.out.printf("SUPER Encryption! Enter a four-digit number and we will encrypt or decrypt it for you.\n");
- //These lines will ask the user if he/she wants to encrypt a number or decrypt a number.
- System.out.printf("Would you like to encrypt, or decrypt today? Enter enc or dec.\n");
- String EncTest = scan.nextLine();
- //Provides error checking in case the user enters invalid input.
- while(EncTest.equalsIgnoreCase("enc") != true && EncTest.equalsIgnoreCase("dec") != true) {
- System.out.printf("Please answer either enc or dec.\n");
- EncTest=scan.nextLine();
- }
- //This line will ask the user for their input, as an int. Ptext stands for plain text.
- System.out.printf("\nWhat number are you wishing to encrypt or decrypt? Please enter only one.\n");
- String ptext = scan.nextLine();
- //Errorchecking if entered number is different than 4.
- while(ptext.length() != 4) {
- System.out.printf("Please enter a four-digit number.\n");
- ptext = scan.nextLine();
- }
- //Will run the encrypt or decrypt functions.
- if (EncTest.equalsIgnoreCase("enc")){
- System.out.printf("Your encrypted number is: %s\n", encrypt(ptext));
- }
- else{
- System.out.printf("Your decrypted number is: %s\n", decrypt(ptext));
- }
- //Asks the user whether he/she wants to rerun the program.
- System.out.printf("Do you wish to run this program again? Answer yes or no.\n");
- String rerun = scan.nextLine();
- //Provides error checking in case the user enters invalid input.
- while(rerun.equalsIgnoreCase("yes") != true && rerun.equalsIgnoreCase("no") != true) {
- System.out.printf("Please answer either yes or no.\n");
- rerun=scan.nextLine();
- }
- if(rerun == "no")System.exit(0);
- }
- }
- /* Method: encrypt
- * This method will help encrypt a four-digit number using the algorithm stated above.
- * Parameters: String to be encrypted.
- * Return: Encrypted String
- */
- public static String encrypt (String ptext){
- //Makes a char array to represent the string ptext.
- char[] etext = ptext.toCharArray();
- //Step one of the algorithm. Replace each digit by (the sum of that digit plus 7) modulus 10.
- for(int i = 0; i < ptext.length(); i++) {
- //Changes etext[i] to a string, then parses to int, then applies the math.
- //The + 48 is required at the end to parse the values back into decimal integers instead of ASCII values.
- etext[i] = (char) (((Integer.parseInt (Character.toString (etext[i])) + 7) % 10) + 48);
- }
- //Step two. To replace the first digit with the third, and swap the second digit with the forth.
- char temp;
- temp = etext[0];
- etext[0] = etext[2];
- etext[2] = temp;
- temp = etext[2];
- etext[1] = etext[3];
- etext[3] = temp;
- return new String(etext);
- }
- public static String decrypt (String ptext){
- //Makes a char array to represent the string ptext.
- char[] etext = ptext.toCharArray();
- //Step one of the algorithm in reverse. Replace each digit by (the sum of that digit plus 3) modulus 10.
- for(int i = 0; i < ptext.length(); i++) {
- //Changes etext[i] to a string, then parses to int, then applies the math.
- //The + 48 is required at the end to parse the values back into decimal integers instead of ASCII values.
- etext[i] = (char) (((Integer.parseInt (Character.toString (etext[i])) + 3) % 10) + 48);
- }
- //Step two. To replace the first digit with the third, and swap the second digit with the forth.
- char temp;
- temp = etext[0];
- etext[0] = etext[2];
- etext[2] = temp;
- temp = etext[2];
- etext[1] = etext[3];
- etext[3] = temp;
- return new String(etext);
- }
- }
Add Comment
Please, Sign In to add comment