Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- //main
- public class MyFirst{
- public static void main(String[] args){
- int selection = 0;
- System.out.println("Would you like to make an encryption or decryption?\n1 - Encryption\n"
- + "2 = Decrypton");
- try{
- Scanner user = new Scanner(System.in);
- selection = user.nextInt();
- }
- catch(Exception e){
- e.getMessage();
- }
- if(selection == 1){
- Encrypt encrypt = new Encrypt();
- }
- else if(selection == 2){
- Decrypt decrypt = new Decrypt();
- }
- else {
- System.out.println("Error!");
- }
- }
- }
- //Encryption class
- public class Encrypt {
- private char[] crypt;
- private String userInput;
- private String FinalEncrypt;
- private int int1 = 2;
- public Encrypt(){
- System.out.println("Enter a string you would like encrypted: ");
- Scanner ui = new Scanner(System.in);
- userInput = ui.nextLine();
- crypt = userInput.toCharArray(); // convert string to array
- //goes through each char and adds to it, putting it in to a new array
- for(int i = 0; i < crypt.length; i++){
- crypt[i] = (char)(crypt[i] + int1);
- int1 += int1 + 2;
- }
- FinalEncrypt = new String(crypt);
- System.out.println("Encryption: " + FinalEncrypt);
- }
- }
- //Decryption class
- public class Decrypt {
- private char[] crypt;
- private String userInput;
- private String FinalEncrypt;
- private int int1 = 2;
- public Decrypt(){
- System.out.println("Enter a string you would like decrypted: ");
- Scanner ui = new Scanner(System.in);
- userInput = ui.nextLine();
- crypt = userInput.toCharArray(); // convert string to array
- //goes through each char and adds to it, putting it in to a new array
- for(int i = 0; i < crypt.length; i++){
- crypt[i] = (char)(crypt[i] - int1);
- int1 += int1 + 2;
- }
- FinalEncript = new String(crypt);
- System.out.println("Decrypted text: " + FinalEncrypt);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement