-
Notifications
You must be signed in to change notification settings - Fork 0
/
DheerCipher.java
135 lines (129 loc) · 4.37 KB
/
DheerCipher.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
import java.io.IOException;
import java.math.BigInteger;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Random;
import java.util.Scanner;
public class DheerCipher
{
public static void main(String[] args) throws IOException
{
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);
}}
private static String encrypt(String usr) {
byte[] encrypted = usr.getBytes();
String enu=bytesToHex(encrypted);
return enu;
}
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);
byte[] decryptedpv = decrypt(l);
System.out.println("Enter Password:");
String pl=in.next();
if(pl.equals(new String(decryptedpv))){
System.out.println("Login Successful");}
}catch(Exception e){
System.out.println("Exception:"+e);
}}
private static byte[] decrypt(String l) {
byte[] decrypted = hexStringToByteArray(l);
return decrypted;
}
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);
byte[] decryptedusr = decrypt(reus);
System.out.println("User: " + new String(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 bytesToHex(byte[] in) {
final StringBuilder builder = new StringBuilder();
for(byte b : in) {
builder.append(String.format("%02x", b));
}
return builder.toString();
}
public static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i+1), 16));
}
return data;
}
}