Encryption Decryption

Ads
 

Encryption Decryption

I want to store a type string to my database, specifically MSSQL, I want the stored data in the database to be encrypted and I want to retrieve it in it's decrypted format. I am using JSP's, I want to encrypt user input on one jsp and retrieve it, decrypted, on another jsp for viewing and record purposes.

View Answers

February 23, 2012 at 6:00 PM

1)form.jsp:

<html>
<body>
<form name="userform" method="post" action="encrypt.jsp">
<table>
<tr><td>User Name</td><td><input type="text" name="user"></td></tr>
<tr><td>Password</td><td><input type="password" name="pass"></td></tr>
<tr><td>Confirm Password</td><td><input type="password" name="cpass"></td></tr>
<tr><td>Name</td><td><input type="text" name="name"></td></tr>
<tr><td>Phone</td><td><input type="text" name="phone"></td></tr>
<tr><td>Contact</td><td><input type="text" name="contact"></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.MessageDigest"%>
<%
String username=request.getParameter("user");
String password=request.getParameter("pass");
String name=request.getParameter("name");
String phone=request.getParameter("phone");
String contact=request.getParameter("contact");
String algorithm="";
int ph=Integer.parseInt(phone);
byte[] unencodedPassword = password.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();
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 register(user,pass,name,phone,contact) VALUES(?,?,?,?,?)");
ps.setString(1,username);
ps.setString(2,passw);
ps.setString(3,name);
ps.setInt(4,ph);
ps.setString(5,contact);
int i = ps.executeUpdate();
ps.close();
con.close();
}
catch(Exception ex){}

%>

February 23, 2012 at 6:01 PM

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`)                                     
        );

April 1, 2012 at 1:35 PM

Given final block not properly padded


April 1, 2012 at 1:35 PM

m getting dis erro plz help

Ads









Related Tutorials/Questions & Answers:
encryption and decryption
encryption and decryption  how to encrypt and decrypt data in text file
encryption and decryption
encryption and decryption   ASCII encryptor I want to develop a program that encrypt and decrypt. The messages stored in a file should.... This is called encryption. When one wants to read the contents of the file
Advertisements
encryption and decryption
encryption and decryption   ASCII encryptor I want to develop a program that encrypt and decrypt. The messages stored in a file should.... This is called encryption. When one wants to read the contents of the file
encryption and decryption
encryption and decryption   ASCII encryptor I want to develop a program that encrypt and decrypt. The messages stored in a file should.... This is called encryption. When one wants to read the contents of the file
encryption and decryption
encryption and decryption   ASCII encryptor I want to develop a program that encrypt and decrypt. The messages stored in a file should.... This is called encryption. When one wants to read the contents of the file
Encryption Decryption
Encryption Decryption  I want to store a type string to my database, specifically MSSQL, I want the stored data in the database to be encrypted and I want to retrieve it in it's decrypted format. I am using JSP's, I want
encryption and decryption of files
encryption and decryption of files   Please can any one provide me with the code for encryption and decryption of files using RSA algorithm. I want to use a browse option to search for the file which is needed for encryption
servelt -jsp Encryption Decryption Code
servelt -jsp Encryption Decryption Code  Encryption code&Decryption code Iam developing a web security application for that reason i need a code of encryption &Decryption in servlet. Can u please send it to me.Its very
Encryption Decryption In Scriptlets/Java
Encryption Decryption In Scriptlets/Java  I have this code in my scriptlet, when I use the Encryption code to encrypt my data it executes... where the program stopped working and it stopped in the decryption part and I get
Password encryption and decryption
Password encryption and decryption  Hello, I'm developing a system that requires user to login to enter the system. so I wanted to store encrypted users' password in the database so that I wouldnt know their password
Sample code for An ASCII Encryption and Decryption code in C-language only.
Sample code for An ASCII Encryption and Decryption code in C-language only... code for this project including both the encryption and decryption aspects... stored in a file. This is called encryption. When one wants to read the contents
AES decryption in Java
AES decryption in Java   AES decryption in Java with password...-Decryption-using-key-password-.html    http://www.roseindia.net/answers/viewqa/Java-Beginners/26020-AES-Decryption-using-key-password-.html   
sound encryption
sound encryption  i want to ask if there is a simple code to perform encryption on sound file
AES encryption in J2ME
AES encryption in J2ME  Is it possible to do aes encryption in j2me
ModuleNotFoundError: No module named 'Encryption'
ModuleNotFoundError: No module named 'Encryption'  Hi, My Python... 'Encryption' How to remove the ModuleNotFoundError: No module named 'Encryption' error? Thanks   Hi, In your python environment you
Version of com.braintreepayments>encryption dependency
List of Version of com.braintreepayments>encryption dependency
AES Decryption using key password
AES Decryption using key password    /* Decrypt using AES with password */ /* developed by Nishanth Thomas - Insolutions Global Pvt Ltd Bangalore */ import java.security.spec.AlgorithmParameterSpec; import
ModuleNotFoundError: No module named 'aws-encryption'
ModuleNotFoundError: No module named 'aws-encryption'  Hi, My... named 'aws-encryption' How to remove the ModuleNotFoundError: No module named 'aws-encryption' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'aws-encryption'
ModuleNotFoundError: No module named 'aws-encryption'  Hi, My... named 'aws-encryption' How to remove the ModuleNotFoundError: No module named 'aws-encryption' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'aws-encryption'
ModuleNotFoundError: No module named 'aws-encryption'  Hi, My... named 'aws-encryption' How to remove the ModuleNotFoundError: No module named 'aws-encryption' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'cred-encryption'
ModuleNotFoundError: No module named 'cred-encryption'  Hi, My... named 'cred-encryption' How to remove the ModuleNotFoundError: No module named 'cred-encryption' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'cred-encryption'
ModuleNotFoundError: No module named 'cred-encryption'  Hi, My... named 'cred-encryption' How to remove the ModuleNotFoundError: No module named 'cred-encryption' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'cred-encryption'
ModuleNotFoundError: No module named 'cred-encryption'  Hi, My... named 'cred-encryption' How to remove the ModuleNotFoundError: No module named 'cred-encryption' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'dynamodb-encryption'
ModuleNotFoundError: No module named 'dynamodb-encryption'  Hi, My... named 'dynamodb-encryption' How to remove the ModuleNotFoundError: No module named 'dynamodb-encryption' error? Thanks   Hi
ModuleNotFoundError: No module named 'dynamodb-encryption'
ModuleNotFoundError: No module named 'dynamodb-encryption'  Hi, My... named 'dynamodb-encryption' How to remove the ModuleNotFoundError: No module named 'dynamodb-encryption' error? Thanks   Hi
ModuleNotFoundError: No module named 'py-encryption'
ModuleNotFoundError: No module named 'py-encryption'  Hi, My... 'py-encryption' How to remove the ModuleNotFoundError: No module named 'py-encryption' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'ska-encryption'
ModuleNotFoundError: No module named 'ska-encryption'  Hi, My... named 'ska-encryption' How to remove the ModuleNotFoundError: No module named 'ska-encryption' error? Thanks   Hi, In your python
Maven Dependency encryption >> 2.0.0
You should include the dependency code given in this page to add Maven Dependency of com.braintreepayments >> encryption version2.0.0 in your project
Maven Dependency encryption >> 2.1.0
You should include the dependency code given in this page to add Maven Dependency of com.braintreepayments >> encryption version2.1.0 in your project
ModuleNotFoundError: No module named 'aws-encryption-cli'
ModuleNotFoundError: No module named 'aws-encryption-cli'  Hi, My... named 'aws-encryption-cli' How to remove the ModuleNotFoundError: No module named 'aws-encryption-cli' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'aws-encryption-sdk'
ModuleNotFoundError: No module named 'aws-encryption-sdk'  Hi, My... named 'aws-encryption-sdk' How to remove the ModuleNotFoundError: No module named 'aws-encryption-sdk' error? Thanks   Hi, In your

Ads