-
Notifications
You must be signed in to change notification settings - Fork 0
/
ROT18Cipher.java
60 lines (50 loc) · 1.58 KB
/
ROT18Cipher.java
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
public class ROT18Cipher {
private final static int countShiftLetter = 13;
private final static int countShiftNumber = 5;
public static StringBuilder encryption(StringBuilder plainText)
{
plainText = new StringBuilder(plainText.toString().trim().toLowerCase());
StringBuilder cipherText=new StringBuilder();
for(char letterInTheText:plainText.toString().toCharArray())
{
cipherText.append(ShiftLetterNumber(letterInTheText));
}
return cipherText;
}
public static StringBuilder decryption(StringBuilder cipherText)
{
StringBuilder plainText=new StringBuilder();
for(char letterInTheText:cipherText.toString().toCharArray())
{
plainText.append(ShiftLetterNumber(letterInTheText));
}
return plainText;
}
public static Character ShiftLetterNumber(Character character)
{
if(!Character.isDigit(character) && !Character.isLetter(character))
return character;
int count = 0;
int countShift = Character.isLetter(character) ? countShiftLetter : countShiftNumber;
while(count<countShift)
{
switch(character)
{
case 'z' : character='a';character--;break;
case '9' : character='0';character--;break;
}
character++;
count++;
}
return character;
}
public static void main(String[] args)
{
StringBuilder plainText = new StringBuilder("software2019-2020");
System.out.println("Metnin Acik Hali :"+plainText);
StringBuilder cipherText = encryption(plainText);
System.out.println("Metnin Sifrelenmis Hali :"+cipherText);
plainText = decryption(cipherText);
System.out.println("Metnin Acik Hali :"+plainText);
}
}