J2SE

J2SE

How to encrypt password and retrive orginal password from database i.e decrypted form of password reply as early as possible.........

Thanks in advance ravindra

View Answers

May 3, 2012 at 10:46 AM

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

May 3, 2012 at 10:49 AM

import java.io.*;
import java.security.*;
import javax.crypto.*;

class EncryptAndDecrypt {

public static void main (String[] args) throws Exception{
KeyPairGenerator keygenerator = KeyPairGenerator.getInstance("RSA");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");
keygenerator.initialize(1024, random);

KeyPair keypair = keygenerator.generateKeyPair();
PrivateKey privateKey = keypair.getPrivate();
PublicKey publicKey = keypair.getPublic();
Cipher cipher = Cipher.getInstance("RSA");

cipher.init(Cipher.ENCRYPT_MODE, publicKey);
String st= "roseindia";
byte[] cleartext = null;
cleartext = st.getBytes();
byte[] ciphertext = null;
ciphertext = cipher.doFinal(cleartext);
System.out.println("the encrypted text is: " + ciphertext.toString());

cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] cleartext1 = cipher.doFinal(ciphertext);
System.out.println("the decrypted cleartext is: " + new String(cleartext1));
}
}

The above code will encrypt and decrypt any string.


May 5, 2012 at 11:34 AM

Hi i am using the jsp code,

it is generating error

javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher

please give me some idea about........

thanks in advance surya.p









Related Tutorials/Questions & Answers:
j2se
j2se  program to sort the employees 4 emp salarys
j2se
j2se  program to sort the employees 4 emp salarys
Advertisements
j2se
j2se  program to sort the employees 4 emp salarys
j2se
j2se  program to sort the employees 4 emp salarys
J2SE
J2SE  How to encrypt password and retrive orginal password from database i.e decrypted form of password reply as early as possible......... Thanks in advance ravindra   1)form.jsp: <html> <body> <form
j2se - Java Interview Questions
j2se  what is the difference between JVM AND JRE
JTA in J2SE JDB - JDBC
JTA in J2SE JDB  I am planning to implement a transaction control in J2SE based application with Oracle. JDBC also provides transaction control. I... with simple code example or tuorial about using JTA in j2se perspective
What is J2SE?
What is J2SE?       J2SE is a collection of Java Programming Language API (Application....  J2SE platform has been developed under the Java umbrella and primarily
Difference between Servlet 2.5 and Servlet 2.4
supports for Http, J2SE, and J2EE: Servlet 2.4 depends on Http1.1 and J2SE 1.3.... of the added features is given below: Dependency on J2SE 5.0: The minimum platform
Java 2 Platform-Enterprise Edition
Java 2 Platform-Enterprise Edition       Java Platform, Enterprise Edition builds on the solid foundation of J2SE. It has become an industry standard for developing next-generation web
Beginners Java Tutorials - Installing JDK
; In this beginners java Tutorial ,We will first download, install and configure the J2SE.... This tutorial is for installing JDK 1.5. Downloading and Installing J2SE Software on Windows Platform To download J2SE for development visit http
JavaBeans
JavaBeans         JavaBeans are reusable software components designed for the Java 2 Platform, Standard Edition (J2SE). Java Beans are completely written
J2EE Development-zen Platform
development and runtime environment for J2SE and J2EE (Java 2 Platform, Standard
What's new in JDBC 3.0?
please provide code - Java Beginners
please provide code  provide code to snake game in J2SE
JDBC - Java Beginners
JDBC  How to connect to mysql database from an applets or GUI components (on J2SE) using Eclipse
javabeans - Java Beginners
javabeans  hi sir, Question: what is the compatible J2SE version with bdk1.1(BEAN DEVELOPMENT KIT) . I Have BDK 1.1, to find related J2SE version is problem for me. will u help me in this area of this subject
java - Java Beginners
java  What is the difference between j2se 5.0 and jre? can anyone please tell me about the how to use jre
java standard edition - Java Beginners
java standard edition  good evening sir. i want to install j2se version-6 on my laptop and netbeans, so i want to know the path: and classpath: to type in the environmental variables Dialogue box. also full details about
Java Releases
, Standard Edition 1.4 (J2SE 1.4) February 2002 Java 2 Platform, Standard Edition 1.3 (J2SE 1.3) May 2000 Java 2... Platform, Standard Edition (J2SE) August 1999 Java
JAVA - Java Beginners
edition of Java, JDK 1.4 (or J2SE 1.4), includes new features like a new I/O system... Platform, Standard Edition (J2SE) is an advance version from J2SE 1.3. J2SE 1.4 adds... Edition 5.0 is an advance version of JDK 1.4 (or J2SE 1.4) includes the more
Java for Windows
and Installing J2SE Software on Windows Platform ADS_TO_REPLACE_1 To download J2SE for windows operating system visit http:/www.java.sun.com/j2se and download J2SE on your machine. In this tutorial we have used jdk-1_5_0_06-windows-i586.exe
Java installation
java programs. To download J2SE on your machine for development visit http:/www.java.sun.com/j2se. Read more at: http:/www.roseindia.net/java/beginners
how to convert jsp to exe
this application in another system. i donot like to install tomcat and j2se
file uploading - Java Beginners
file uploading   hi, i am developing one stand alone j2se programming in that i have to upload a file to another system in the LAN and have to download from the server .i am using java.io. and java.net and swing .the task
thanks - Development process
thanks  thanks for sending code for connecting jsp with mysql. I have completed j2se(servlet,jsp and struts). I didn't get job then i have learnt php. Is it right choice. someone say right or someone say wrong. Will u please
java doubt - Java3D
only idea about j2se.... please sugeste me
java - Java Interview Questions
and jdk1.5 Sun added some more advanced and some new features to new J2SE 1.5 released as compared to J2SE 1.4. Sun have made more additions to make the Collection
view data from jTextArea to jtable
user friendly. Java consists of Some parts, namely J2SE, J2EE, J2ME.** selection of words: language: java, programming, J2SE, J2EE, J2ME liaison: is, namely
Java Thread Context
the java.lang.Object.  They were introduced in J2SE specifications. This class is essential
Confermation for use of API - Java Beginners
Confermation for use of API  HI ! I am new here. I am making an application for wireless communication between mobile and pc. Let me know which api I have to use at J2SE side. I will be very thankful to you. Regards
java - Development process
java  What is the difference between j2sdk1.4 version and j2sdk1.5 version?  Hi friend, The J2SE 1.5 is the advance version released by the sun. They have made more addition in the existing J2SE1.4 in makeing
Java Programming Language
and of course for aiming core java J2SE is designed. (adsbygoogle... the major inclusions are AWT, JDBC, RMI, Java beans etc. J2SE 1.2 8-Dec-1998 The Third release was Java2 Standard Edition (J2SE), Its code name
JavaBeans Activation Framework
, Standard Edition (J2SE).   ADS_TO_REPLACE_1  
Identify EJB 2.0 container requirements.
, Standard Edition, v1.3 (J2SE) APIsEJB 2.0 Standard ExtensionJDBC 2.0 Standard Extension
cookie and session dependency
of login and logout action. We are using Struts2, apache tomcat 6.5, j2ee, j2se 1.6.
j2ee environment varaiable details
j2ee environment varaiable details  hi friends recently i installed jdk7, tomcat 7,j2ee6.If i run the j2se programs no problem but i run basicsevlet program hello world it show several error.please tell me the what
which version of eclipse is used to execute Servlet or JSP
which version of eclipse is used to execute Servlet or JSP  Hi Can any body tell me which version of eclipse i have to use inorder to execute Servlet or JSP. previosuly i installed eclipse 3.1 .i am having j2se 5.0 with which i
Download JDK
Platform, That comes in three editions J2ME, J2SE & J2EE. If you are beginner or learning Java then start by downloading J2SE. Acronyms: JDK  Java
What are the level in Jboss 5 application server to deploy the application
to the language - JavaBeans - JDBC - RMI J2SE 1.2 (December 8, 1998) playground This and subsequent releases through J2SE 5.0 were rebranded retrospectively Java 2 & version name "J2SE" (Java 2 platform, Standard edition) replaced JDK
Integration Libraries
223 (actually a framework)which is the Java API script (included in J2SE 6
Upload file on server automatically on specific time - JSP-Servlet
with, or used along side virtually any J2EE or J2SE application - from the smallest
What is JDBC?
What is JDBC?  hello, What is JDBC?   hello,ADS_TO_REPLACE_1 JDBC technology is an API (included in both J2SE and J2EE releases) that provides cross-DBMS connectivity to a wide range of SQL databases and access
Java API
Swing   javax.swing Included in J2SE 1.2... javax.crypto.spec Included as part of J2SE 1.4 and later. Available... and Authorization Service JAAS javax.security.auth Included in J2SE
Java Mail
Java 2 Platform, Standard Edition (J2SE) 1.1.7 and upwards versions, requires email
NetBeans IDE
Java application types i.e. J2SE, web, enterprise and mobile applications. It runs
Given a list, identify which are requirements for an EJB-jar file.
interfaces depend on, except J2EE and J2SE classes.ADS_TO_REPLACE_2
About Java
1.1 February 19, 1997 J2SE 1.2 December 8, 1998 J2SE 1.3 May 8, 2000 J2SE 1.4 February 6, 2002 J2SE 5.0
Features of Servlets 2.4
Features of Servlets 2.4       In this tutorial you will learn the new features added in Servlet 2.4. Upgraded supports for Http, J2SE, and J2EE: Servlet 2.4 depends on Http1.1 and J2SE
J2EE Interviews Question page9,J2EE Interviews Guide,J2EE Interviews
.    What is J2SE ? Abbreviate of Java 2 Platform, Standard Edition..., Standard Edition (J2SE) ? The core Java technology platform.    

Ads