-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncryptDecrypt.java
More file actions
60 lines (48 loc) · 1.79 KB
/
EncryptDecrypt.java
File metadata and controls
60 lines (48 loc) · 1.79 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;
class EncryptDecrypt{
static final String TABLE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()-=_+[]{}\\|;:\'\",.<>/? ";
public static String encrypted(String input , int key) {
String encoded = "";
for(int i = 0; i < input.length(); i++)
if(input.charAt(i) == ' '){
encoded += ' ';
}else{
encoded += TABLE.charAt(TABLE.indexOf(input.charAt(i)) + key % TABLE.length() );
}
System.out.print(encoded);
return encoded;
}
public static String decrypted(String input , int key) {
String decoded = "";
for(int i = 0; i < input.length(); i++)
if(input.charAt(i) == ' '){
decoded += ' ';
}else{
decoded += TABLE.charAt(TABLE.indexOf(input.charAt(i)) - key % TABLE.length());
}
System.out.print(decoded);
return decoded;
}
public static void main(String [] args){
Scanner sc = new Scanner(System.in);
System.out.println("do u want to decrypt or encrypt string?");
String choice = sc.nextLine();
if(choice.equalsIgnoreCase("encrypt")){
System.out.print("provide input");
String input = sc.nextLine();
System.out.print("provide key");
int key = sc.nextInt();
encrypted(input , key);
}
else if(choice.equalsIgnoreCase("decrypt")){
System.out.print("provide input");
String input = sc.nextLine();
System.out.print("provide key");
int key = sc.nextInt();
decrypted(input , key);
}
else{
System.out.println("invalid");
}
}
}