How to decrypt an encrypted password and store in database ?
Her is my code that i have done for encryption
> <%@page > import="java.sql.*,java.util.*"%> > <%@page import=" java.security.*"%> > <%@page import="javax.crypto.*"%> > <%@page import=" > java.security.MessageDigest"%> > > > > <% String > fname=request.getParameter("fname"); > String > lname=request.getParameter("lname"); > String > email=request.getParameter("email"); > String > pass=request.getParameter("password"); > String > cpass=request.getParameter("confirm_password"); > String > gender=request.getParameter("gender"); > String > username=request.getParameter("uname"); > String > phone=request.getParameter("phone"); > > > String algorithm=""; > > byte[] unencodedPassword = > pass.getBytes(); MessageDigest md = > null; try { md = > MessageDigest.getInstance("MD5"); } > catch (Exception e) {} md.reset(); > md.update(unencodedPassword); byte[] > encodedPassword = md.digest(); > StringBuffer buf = new StringBuffer(); > for (int i = 0; i < > encodedPassword.length; i++) { if > (((int) encodedPassword[i] & 0xff) < > 0x10) { buf.append("0"); } > buf.append(Long.toString((int) > encodedPassword[i] & 0xff, 16)); } > String passw=buf.toString(); > > > try{ > System.out.println("Username::" > +username); > Class.forName("com.mysql.jdbc.Driver"); > Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/demo", > "root", "root"); > Statement st=con.createStatement(); > int i=st.executeUpdate("insert into > users(username,password,lastname,email,phone,gender,firstname,confirm_password) > values('"+username+"','"+passw+"','"+lname+"','"+email+"','"+phone+"','"+gender+"','"+fname+"','"+cpass+"')"); > out.println("Data is successfully inserted!"); > } > catch(Exception e){ > System.out.print(e); > e.printStackTrace(); > } > > %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 > Transitional//EN" > "http://www.w3.org/TR/html4/loose.dtd"> > <html> <head> <meta > http-equiv="Content-Type" > content="text/html; > charset=ISO-8859-1"> > <title>Registration</title> </head> > <body> </body > > > </html>
The given allow the user to enter username, password along with other fields in order to submit the details into database. The password is first encrypted and then stored into database. If you want to get this password in decrypted form then you can use the decrypt function, created in the jsp.
1)form.jsp:
<html> <body> <form name="userform" method="post" action="encrypt.jsp"> <table> <tr><td>User Name</td><td><input type="text" name="name"></td></tr> <tr><td>Password</td><td><input type="password" name="pass"></td></tr> <tr><td>Address</td><td><input type="text" name="address"></td></tr> <tr><td>Contact No</td><td><input type="text" name="phone"></td></tr> <tr><td><input type="submit" value="Search"></td></tr> </table> </form> </body> </html>
2)encrypt.jsp:
<%@page import="java.sql.*"%> <%@page import=" java.security.*"%> <%@page import="javax.crypto.*"%> <%! private static String algorithm = "DESede"; private static Key key = null; private static Cipher cipher = null; private static byte[] encrypt(String input)throws Exception { cipher.init(Cipher.ENCRYPT_MODE, key); byte[] inputBytes = input.getBytes(); return cipher.doFinal(inputBytes); } %> <%! private static String decrypt(byte[] encryptionBytes)throws Exception { cipher.init(Cipher.DECRYPT_MODE, key); byte[] recoveredBytes = cipher.doFinal(encryptionBytes); String recovered = new String(recoveredBytes); return recovered; } %> <% String name=request.getParameter("name"); String password=request.getParameter("pass"); String address=request.getParameter("address"); String phone=request.getParameter("phone"); int ph=Integer.parseInt(phone); StringBuffer buffer=new StringBuffer(); key = KeyGenerator.getInstance(algorithm).generateKey(); cipher = Cipher.getInstance(algorithm); String input = password; System.out.println("Entered: " + input); byte[] encryptionBytes = encrypt(input); String passw=new String(encryptionBytes); String connectionURL = "jdbc:mysql://localhost:3306/test"; Connection con=null; try{ Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection(connectionURL, "root", "root"); PreparedStatement ps = con.prepareStatement("INSERT INTO user(name,password,address,telno) VALUES(?,?,?,?)"); ps.setString(1,name); ps.setString(2,passw); ps.setString(3,address); ps.setInt(4,ph); int i = ps.executeUpdate(); ps.close(); } catch(Exception ex){ System.out.println(ex); } try{ Statement st=con.createStatement(); ResultSet rs=st.executeQuery("Select * from user where id='1'"); String str=""; if(rs.next()){ str=rs.getString("password"); } out.println("Your password is: "+decrypt(str.getBytes())); System.out.println("Your password is: "+decrypt(str.getBytes())); } catch(Exception e){} %>
For the above code, we have create a database table:
CREATE TABLE `user` ( `id` bigint(255) NOT NULL auto_increment, `name` varchar(255) default NULL, `password` varchar(255) default NULL, `address` varchar(255) default NULL, `telno` int(255) default NULL, PRIMARY KEY (`id`) );