problem in encryption and decryption

hello everyone..

I'm a new bee in this forum.I don't know weather it is the right place to put my query or some other place.I saw in this forum people putting up their problems regarding the java development.So i came up with my problem.

I'm working on a web application using jdk1.5,struts 1.1,apache tomcat5.5 and mysql5.2.For user registering and loging i'm using a encryption /decryption code to encrypt the password to the database and decrypt it back during userid and password verification in the code.The code of the encryption/decryption is as follows...

import java.util.Random;

public class Crypt

{

String key = "uy67jwq98JWPOI99dj9021032amiet";

public String strencrypt(String str)

{

String result="";

int i = 0, current = 0;

Random r = new Random();

current = r.nextInt(30);

if(current<10)result = "0";

result = result + current;

if(((key.charAt(current)+ "").hashCode() + str.length()) < 10)

{

result = result + "0";

}

result = result + (char)((key.charAt(current)+ "").hashCode() + str.length());

while(i<str.length())

{

result = result + ( (char)( ((str.charAt(i)+"").hashCode()) + ((key.charAt(current++)+"").hashCode()) ) );

if(current==key.length())current=0;

i++;

}

while(i><key.length())

{

result = result + ( (char) ((r.nextInt(30)) + ((key.charAt(current++)+"").hashCode())) );

if(current==key.length())current=0;

i++;

}

return result;

}

public String strdecrypt(String str)

{

int current=0, len = 0, i = 0, header = 3;

String result="", slen = "";

current = Integer.parseInt(str.substring(0,2));

slen = "" + (str.charAt(2)+"").hashCode();

len = (Integer.parseInt(slen)) - ((key.charAt(current)+"").hashCode());

i = header;

while(i><(header + len))

{

result = result + ( (char) ((str.charAt(i)+ "").hashCode() - ((key.charAt(current++)+"").hashCode())) );

if(current == key.length())

current=0;

i++;

}

return result;

}

}

**********************************************************************

But the problem that i'm facing is regarding the the database mysql5.2 is installed in two operating system ie windows xp and windows 2000 server.When i try to connect my web application to the windows xp installed database mysql5.2 and try creating a new user and then try to login ,the loging fails.Even i have found out the reason.The above pasted code couldn't decrypt properly.Heres what i get when i System.out.println(""); the data retrived from the database...I'm pasting it also...

********************************************************************************

s retriving from db=16l╦▐⌐?螵?7pmofv?A?l?rNCdhhLAK

password coming from welcome.jsp=gtplpune

c.strencrypt(password)=14A帷╘╓⌐厌?LH7}?te?HG⌂?QFUkPj]

c.strdecrypt(s)=gtp☼pu☼♀

encryption mismatch

****************************************************************************

see that teh password coming from welcome/jsp is gtplpune

and the password after decryption comingh from database is gtp☼pu☼♀....

where u can see some letter such as l,n,e could not be decrypted or in some other format....So the code is unable to validate teh user.....

But teh strange thing is that when i'm using the mysql5.2 installed in windows 2000 server everything seems to work fine.There no problem in encryption or decryption and everything works fine...So anyone of you have any idea what can be the raeson for it.And what can be the probable solution to it.I'm waiting for ur replies which i guess will help me out.

Thank you

sabyasachi

[3912 byte] By [sabyasachi.roya] at [2007-11-27 8:08:09]
# 1
Encryption of passwords should be handled in a binary manner. Otherwise you might lose some bytes due to encoding conversion. When saving encrypted data to a text type (e.g. a VARCHAR field in your database), use a text encoding for binary values such as base64 or uuencode.
quittea at 2007-7-12 19:50:59 > top of Java-index,Java Essentials,Java Programming...
# 2
Thanks for ur reply,well my database field has datatype varchar(50),coloumn charset utf8and coloumn collate utf8_general_ci...So can u be more clear what to change in this...thank you
sabyasachi.roya at 2007-7-12 19:50:59 > top of Java-index,Java Essentials,Java Programming...
# 3

When encrypting, you change byte values of your characters to other codes which might be invalid codes respective to a certain encoding or control codes. Saving these as textual data can lead to a loss of bytes. So handle the result of encryption as bytes/a byte[], and use a textual binary encoding (look up what Base64 is made for, then you'll understand) for saving into the backend and do the opposite work when reading/decrypting.

quittea at 2007-7-12 19:50:59 > top of Java-index,Java Essentials,Java Programming...
# 4
i'm using 32 bit xp professional where the problem is arising.But why it's absolutely working fine in windows 2000 server...If i change my databse to linux will i also get the same problem with the existing code and database setting?
sabyasachi.roya at 2007-7-12 19:50:59 > top of Java-index,Java Essentials,Java Programming...
# 5
> But why it's absolutely working fine in windows 2000 server...My guess: pure luck.> If i change my databse to linux will i also get the same problem > with the existing code and database setting?I'd expect trouble, yes.
quittea at 2007-7-12 19:50:59 > top of Java-index,Java Essentials,Java Programming...
# 6
ok thaks a lot quitte for ur reply and i will try using textual binary encoding as u said so that it works fine wetaher mysql installed in any operating system.
sabyasachi.roya at 2007-7-12 19:50:59 > top of Java-index,Java Essentials,Java Programming...