-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathROT13Cipher.java
More file actions
60 lines (57 loc) · 1.13 KB
/
ROT13Cipher.java
File metadata and controls
60 lines (57 loc) · 1.13 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
import java.util.Scanner;
public class ROT13Cipher{
static String encrypt(String pt){
int i,n;
char ch;
char[] encpt1=pt.toCharArray();
for(i=0;i<pt.length();i++){
n=encpt1[i];
if(n>=78){
n+=13;
n=n-90+64;
ch=(char)n;
encpt1[i]=ch;
}
else{
n+=13;
ch=(char)n;
encpt1[i]=ch;
}
}
String encpt2=new String(encpt1);
return encpt2;
}
static String decrypt(String encpt){
int i,n;
char ch;
char[] decpt1=encpt.toCharArray();
for(i=0;i<encpt.length();i++){
n=decpt1[i];
if(n<=77){
n-=13;
n=91-(65-n);
ch=(char)n;
decpt1[i]=ch;
}
else{
n-=13;
ch=(char)n;
decpt1[i]=ch;
}
}
String decpt2=new String(decpt1);
return decpt2;
}
public static void main(String[] args){
String pt,encpt,decpt;
System.out.println("Enter plaintext: ");
Scanner scanner=new Scanner(System.in);
pt=scanner.nextLine();
encpt=encrypt(pt);
System.out.println("Encrypted text: ");
System.out.println(encpt);
decpt=decrypt(encpt);
System.out.println("Decrypted text: ");
System.out.println(decpt);
}
}