-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecryption.java
More file actions
62 lines (51 loc) · 2.54 KB
/
Decryption.java
File metadata and controls
62 lines (51 loc) · 2.54 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
package CoreIdeas.Riddles;
public class Encryption {
// Fields
private static final String ALPHABET = "abcdefghijklmnopqrstuvwxyz"; // 26 alphabets letters
private int keyNum; // decryption key
private String text2cipher; // the text from the user
// Constructor
public Encryption(String text2cipher) {
// set the text from the user to the field
this.text2cipher = text2cipher;
this.keyNum = 3;
}
// Methods
public String getText2cipher() {
// get function - usually template for getters
return text2cipher;
}
private int getIndex(char c) {
// gets a letter from the user text and return the index of the letter in the alphabet string
return ALPHABET.indexOf(c);
}
private char getChar(int index) {
// gets an index of the letter in the alphabet string and return the letter - for example for index 0 it will return a
return ALPHABET.charAt(index);
}
private int getKey(int index) {
// gets an index of the letter in the alphabet string and return the key - for example for index 0 it will return 3
// % is the operator for modulo function
return (index + this.keyNum)%26;
}
private String encrypt() {
// encrypts the text from the user
StringBuilder sb = new StringBuilder(); // String is immutable object, thats why we used StringBuilder class to conect all the letters in each iteration to one string
for(int i = 0; i < text2cipher.length(); i++) {
// special case for spaces - without change
if(this.text2cipher.charAt(i) == ' ') sb.append(' ');
else {
int index = this.getIndex(this.text2cipher.charAt(i));
int key = this.getKey(index);
sb.append(this.getChar(key));
}
}
return sb.toString();
}
public static void main(String[] args) {
Encryption e = new Encryption("this is how we do it"); // change the text here
System.out.println("User text : " + e.text2cipher);
System.out.println("Encrypted text : " + e.encrypt());
// H.W for lesson 13 --> Creat your own Decryption class, you can use this class to help you. Good luck !
}
}