-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCaesarCipher.java
152 lines (144 loc) · 5.56 KB
/
CaesarCipher.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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;
public class CaesarCipher {
public static void main(String[] args){
System.out.println("1.Register \n 2.Login\n3.View Users \n4.Show data base state(Encrypted data)");
Scanner in =new Scanner(System.in);
while(true){
System.out.println("Enter your Choice");
int n=in.nextInt();
switch(n){
case 1:
op1();
break;
case 2:
op2(); break;
case 3:
op3(); break;
case 4:
op4(); break;
}
}
}
static void op1(){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/project","root","Kndheeraj@1997");
Statement st=con.createStatement();
// st.execute("create table login(User VARCHAR(20),Password VARCHAR(20))");
Scanner in =new Scanner(System.in);
System.out.println("Enter Username");
String usr=in.next();
System.out.println("Enter Password");
String p=in.next();
System.out.println("ReEnter Password");
String rep=in.next();
if(p.equals(rep)){
String cipheruser = encrypt(usr);
String cipherpass = encrypt(p);
st.executeUpdate("INSERT INTO login VALUES('"+cipheruser+"','"+cipherpass+"')");
System.out.println("A/C created");}}catch(Exception e){
System.out.println("Exception:"+e);
}}
static void op2(){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/project","root","Kndheeraj@1997");
Statement st=con.createStatement();
Scanner in =new Scanner(System.in);
System.out.println("Enter Username:");
String ul=in.next();
String cipheruser = encrypt(ul);
st.executeQuery("select Password from login where User='"+cipheruser+"'");
ResultSet rs = st.getResultSet();
rs.next();
String l=rs.getString(1);
String decryptedpv = decrypt(l);
System.out.println("Enter Password:");
String pl=in.next();
if(pl.equals(decryptedpv)){
System.out.println("Login Successful");}
}catch(Exception e){
System.out.println("Exception:"+e);
}}
static void op3(){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/project","root","Kndheeraj@1997");
Statement st=con.createStatement();
Scanner in =new Scanner(System.in);
st.executeQuery("select user from login");
ResultSet ru = st.getResultSet();
while(ru.next())
{
String reus=ru.getString(1);
String decryptedusr = decrypt(reus);
System.out.println("User: " + decryptedusr);}
}catch(Exception e){
System.out.println("Exception:"+e);
}}
static void op4(){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/project","root","Kndheeraj@1997");
Statement st=con.createStatement();
System.out.println("Database Content:");
System.out.println("User Password");
st.executeQuery("select * from login");
ResultSet rs = st.getResultSet();
while(rs.next()){
String usr=rs.getString(1);
String pass=rs.getString(2);
System.out.println(""+usr+" "+pass);
}}catch(Exception e){
System.out.println("Exception:"+e);
}}
public static String encrypt(String plainText) {
char chars[] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9','!','@','#','$','%','^','&','(',')','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','+','-','*','/','.','[',']','{','}','=','<','>','?','_'};
String empty = "empty";
String cipher = null;
char[] plain = plainText.toCharArray();
for(int i = 0;i<plain.length;i++){
for(int j = 0 ; j<85;j++){
if(plain[i]==chars[j]){
if(j<=80){
plain[i] = chars[j+5];
break;
}
else{
plain[i] = chars [j-80];
}
}
}
}
plainText = String.valueOf(plain);
return plainText;
}
public static String decrypt(String cip) {
char chars[] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9','!','@','#','$','%','^','&','(',')','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','+','-','*','/','.','[',']','{','}','=','<','>','?','_'};
String cipher = null;
String empty = "empty";
char[] cipher1 = cip.toCharArray();
if(cip .equals(empty)){
System.out.println(" No text is Decrypted");
}
else{
for(int i = 0;i<cipher1.length;i++){
for(int j = 0 ; j<85;j++){
if(j>=5 && cipher1[i]==chars[j]){
cipher1[i] = chars[j-5];
break;
}
if(cipher1[i] == chars[j] && j<5){
cipher1[i] = chars[81+j];
break;
}
}
}
} cipher = String.valueOf(cipher1);
return cipher;
}
}