insert an encrypt data in a Table

Hi all,

i have encrypted a data with HmacMD5, all its fine. but when i've tried to insert encrypt data in my table, hash code may return symbols like ?愥Z弮x狼. then when i do a select data has been corrupted. how can i encrypted in stardand symbols( like mysql passwords). here is my code:

KeyGenerator kg = KeyGenerator.getInstance("HmacMD5");

SecretKey sk = kg.generateKey();

// Get instance of Mac object implementing HMAC-MD5, and

// initialize it with the above secret key

Mac mac = Mac.getInstance("HmacMD5");

mac.init(sk);

byte[] result = mac.doFinal(dirMAC.getBytes());

String macenc=new String(result);

String x ="jdbc:mysql://localhost/"+

"mydatabase?user="+user+"&password="+

pass;

Class.forName("com.mysql.jdbc.Driver").newInstance();

conn = DriverManager.getConnection(x);

conn.createStatement().executeUpdate("insert into user " +

"(User,Password) values('system','"+myPass+"')");

java.sql.ResultSet rs=conn.createStatement().executeQuery("select password "+

"from " +"user where user ='system' ");

rs.next();

if((rs.getString(1).equals(macenc))){

System.out.println(rs.getString(1)+" YES "+macenc);

}else{

System.out.println(rs.getString(1)+" NO "+macenc);

}

Output NO. and sometimes when hash has (') character Query not found.

thanks.

[2079 byte] By [johnarevaloa] at [2007-10-3 4:05:18]
# 1

Thie is most probably the offending line

String macenc=new String(result);

It is never a good idea to try to convert arbitrary bytes into a String using this approach. Not all byte sequences have valid char representation. If you must have a String representation use Base64 or Hex encoding of your Hmac. Google for Jakarta Commons Codec to get a library to assist you with this.

sabre150a at 2007-7-14 22:04:35 > top of Java-index,Security,Cryptography...