-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecretAtbash.java
More file actions
91 lines (80 loc) · 2.95 KB
/
SecretAtbash.java
File metadata and controls
91 lines (80 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import java.util.Scanner;
class SecretAtbash{
private Scanner scanner = new Scanner(System.in); //declaring global scanner to use at any point in the class
/*
* The following function creates the instance of this class and runs it
*/
public static void main(String[] args){
SecretAtbash instance = new SecretAtbash();
instance.run();
}
public void run(){
/*
* The following block of code asks the user for their massage, until it's valid
*/
String message;
do{
message = this.getUserInput("Enter your message: ");
if(!this.isMessageValid(message)){ //checkng if the massage is valid
message = ""; //so the loop repeats if the massage is not valid
System.out.println("Message shouldn't contain any commas");
}
}while(message.equals(""));
/*
* The following block of code asks the user for the action they want to perform, until it's valid
*/
String action = "";
do{
action = this.getUserInput("Encrypt - 1\nDecrypt - 2\nEnter the action you want to perform (1/2): ");
if(!action.equals("1") && !action.equals("2")){ //checking if the action is in range of allowed boundaries
action = ""; //so the loop repeats if the action is not valid
System.out.println("Unknown option!");
}
}while(action.equals(""));
/*
* Deciding what to do with the input, based on th action entered
*/
if(action.equals("1")){
System.out.println(this.encryptMessage(message));
}else if(action.equals("2")){
System.out.println(this.decryptMessage(message));
}
}
/*
* The following function returns bool value the represent if the message is valid or not
*/
private boolean isMessageValid(String message){
return !message.contains(",");
}
/*
* The following function gets the input from the user
*/
private String getUserInput(String outputMessage){
if(!outputMessage.equals("")){
System.out.print(outputMessage);
}
return this.scanner.nextLine();
}
/*
* The following function encrypts the message given using the Atbash technique
*/
private String encryptMessage(String message){
String encrypted = "";
for(int i = 0; i < message.length(); i++){ //a loop to go through and encrypt each character in the message given
char shiftedChar = (char) ((int) message.charAt(i) + 3);
encrypted += shiftedChar;
}
return encrypted;
}
/*
* The following function decrypts the message given using the reversed Atbash technique
*/
private String decryptMessage(String encrypted){
String decrypted = "";
for(int i = 0; i < encrypted.length(); i++){ //a loop to go through and decrypt each character in the message given
char shiftedChar = (char) ((int) encrypted.charAt(i) - 3); //shifts current character 3 places back
decrypted += shiftedChar;
}
return decrypted;
}
}