How can an encrypt my password while sending to the database
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Naveen_Kumar
Posted On:   Wednesday, October 8, 2003 04:26 AM

Hi

How can an encrypt my password while sending to the database.And decrypt it after retreiveing it from database.Can any one give some sample code and tell me which software to use

thanks

Naveen

Re: How can an encrypt my password while sending to the database

Posted By:   Christopher_Schultz  
Posted On:   Tuesday, October 28, 2003 07:36 PM

It depends on what your requirements are. Do you really need to be able to decrypt the password?



Often it is only necessary to one-way "encrypt" (or hash) the password. Then, you compare the hashes. That is the approach suggested above.



If you want reversible encryption, you have to use something like 3DES, etc.



-chris

u can use MD5 secrity protocol

Posted By:   Anonymous  
Posted On:   Thursday, October 9, 2003 11:19 PM

this is the sample program which i have used

import java.security.*;
import java.sql.*;
import java.net.*;

public class CreateParticipant {


public static void main (String [] args ){

MessageDigest md = null;

try{

md = MessageDigest.getInstance("MD5");

}catch(Exception e){}

String strLoginName = args[0];
String strPassword = args[1];
String strEncryptedPassword ="";
String strEncryptedPassword_computed ="";
String strURLEncodedEncryptedPassword ="";

byte [] abytPassword = null;
byte [] abytEncrptedPassword = null;

abytPassword = strPassword.getBytes();
abytEncrptedPassword = md.digest(abytPassword);
strEncryptedPassword_computed = new String(abytEncrptedPassword);
strURLEncodedEncryptedPassword = URLEncoder.encode(strEncryptedPassword_computed);

System.out.println("Password --" +strPassword);
System.out.println("Encrypted Password --" +strEncryptedPassword_computed);
System.out.println("Encrypted Password Length --" +strEncryptedPassword.length());
System.out.println("URL encoded Encrypted Password computed --" +strURLEncodedEncryptedPassword);


createParticipant(strLoginName , strURLEncodedEncryptedPassword);


}

public static void createParticipant(String strLoginName ,String strURLEncodedEncryptedPassword){

try {

String strDriver = "org.postgresql.Driver";

Class.forName(strDriver);
String strURL = "jdbc:postgresql://172.16.100.4:5432/general_user";
Connection conn = DriverManager.getConnection(strURL, "postgres" , "postgres");

StringBuffer stbQuery = new StringBuffer();

stbQuery.append("insert into participant ( login_name , password )values (? ,?)");
String strQuery = stbQuery.toString();

PreparedStatement pstmt = conn.prepareStatement(strQuery);
pstmt.setString(1,strLoginName.replaceAll("'","''"));
pstmt.setString(2,strURLEncodedEncryptedPassword);


System.out.println(strQuery);

pstmt.executeUpdate();

}catch (Exception e){System.out.println(e);}



}
}
About | Sitemap | Contact