-
Java Encryption and Decryption (2 messages)
- Posted by: Ran jith
- Posted on: May 24 2007 00:20 EDT
Hai, I am new java developer. Now i am doing one struts application. In my application, at the User registration time, i need to Encrypt the password field and then i like to store into the database. After that the same user will be logging, at the moment the database password will decrypt and it will be compare to the User entering password. If both password's are equal, then only the User will allow the logging. can anybody know or have any sample code for Encrypt and Decrypt. If yes, kindly post me, my mail id is tamilvanan at veradistech dot com. I am expecting anyone's reply. Thanks TamilvananThreaded Messages (2)
- Re: Java Encryption and Decryption by Cameron Purdy on May 24 2007 07:12 EDT
- Re: Java Encryption and Decryption by nikhil dinesh on May 03 2011 07:34 EDT
-
Re: Java Encryption and Decryption[ Go to top ]
- Posted by: Cameron Purdy
- Posted on: May 24 2007 07:12 EDT
- in response to Ran jith
I am new java developer. Now i am doing one struts application.
Java does support encryption / decryption. See: CipherOutputStream CipherInputStream However, it is usually considered a better approach to create a "signature" (or a "checksum") of the password to store, and not store the actual encrypted password. That way, you can always know if the user types in the right password, but no one with access to the system can "get" the password. Peace, Cameron Purdy Tangosol Coherence: The Java Data Grid
In my application, at the User registration time, i need to Encrypt the password field and then i like to store into the database.
After that the same user will be logging, at the moment the database password will decrypt and it will be compare to the User entering password.
If both password's are equal, then only the User will allow the logging. -
Re: Java Encryption and Decryption[ Go to top ]
- Posted by: nikhil dinesh
- Posted on: May 03 2011 07:34 EDT
- in response to Cameron Purdy
Hi,
For password encryption do the following/*
*create a file named PasswordService.java and store the below content
*
*/
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
//import org.myorg.SystemUnavailableException;
import sun.misc.BASE64Encoder;
import sun.misc.CharacterEncoder;
public final class PasswordService
{
private static PasswordService instance;
public PasswordService()
{
}
public synchronized String encrypt(String plaintext) throws Exception
{
MessageDigest md = null;
try
{
md = MessageDigest.getInstance("SHA"); //step 2
}
catch(NoSuchAlgorithmException e)
{
throw new Exception(e.getMessage());
}
try
{
md.update(plaintext.getBytes("UTF-8")); //step 3
}
catch(UnsupportedEncodingException e)
{
throw new Exception(e.getMessage());
}
byte raw[] = md.digest(); //step 4
String hash = (new BASE64Encoder()).encode(raw); //step 5
return hash; //step 6
}
public static synchronized PasswordService getInstance() //step 1
{
if(instance == null)
{
instance = new PasswordService();
}
return instance;
}
}
/*
*Create a file named PasswordUtil.java and store the below content
*
*/
public final class PasswordUtil
{
private PasswordUtil()
{
}
public static void main(String a[]) throws Exception
{
PasswordService ps=new PasswordService();
System.out.println(ps.encrypt("nikhildinesh"));//use this method for encryption "ps.encrypt()" and you can set the parameter as the value which is to be encrypted
}