Tuesday, September 9, 2014

Encryption and Decryption using AES and MYSQL

<%--
    Document   : test
    Created on : Sep 10, 2014, 11:08:50 AM
    Author     : JK
--%>

<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <%


           try{
   
           
            Class.forName("com.mysql.jdbc.Driver");
   
            java.sql.Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/jk","user","password");
     //insert ur db and password
            Statement st=con.createStatement();
           
            String key="nopqrstuvwxyz6789FGHIJKLMNOPQRSTUVWXYZ";
            //you can define your own key
            String Query="insert into pass values(AES_ENCRYPT(1234,'"+key+"'),AES_ENCRYPT(3545,'"+key+"'))";
   
            int o=st.executeUpdate(Query);
           
            if(o==0)
            {
                out.print("Not Executed");
            }
            else
            {
                out.print("  Executed");
           
            }
            String s="";
            String s2="";
            String Query2="select AES_DECRYPT(Verification,'"+key+"'),AES_DECRYPT(PIN,'"+key+"') from pass";//retriving decrypted values
            ResultSet rs=st.executeQuery(Query2);
            while(rs.next())
            {
           
                Blob l=rs.getBlob(1);
               
                byte[] bdata = l.getBytes(1, (int) l.length());
                 s = new String(bdata);
             
                 Blob l2=rs.getBlob(2);
               
                byte[] bdata2 = l2.getBytes(1, (int) l2.length());
                 s2 = new String(bdata2);
               
             
            }
           
             out.print(s);
             out.print(s2);
           
           }
           catch(Exception e)
           {
               out.print("Exception"+e);
         
           }
 
            %>
    </body>
</html>

No comments:

Post a Comment

Home Java Python PHP