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

