chinsw
asked on
how to decrypt the encrypted value from Javascript in Java
I encrypt the value in Javascript using this code :
function Encrypt(theText){
output = new String;
Temp = new Array();
Temp2 = new Array();
TextSize = theText.length;
for (i = 0; i < TextSize; i++) {
rnd = Math.round(Math.random() * 122) + 68;
Temp[i] = theText.charCodeAt(i) + rnd;
Temp2[i] = rnd;
}
for (i = 0; i < TextSize; i++) {
output += String.fromCharCode(Temp[i ], Temp2[i]);
}
return output;
}
and I want to decrypt back the value in Java server side.but, I don't know how to convert the decrypt function into Java. I got the decrypt function in Javascript like this :
function Decrypt(theText){
output = new String;
Temp = new Array();
Temp2 = new Array();
TextSize = theText.length;
for (i = 0; i < TextSize; i++) {
Temp[i] = theText.charCodeAt(i);
Temp2[i] = theText.charCodeAt(i + 1);
}
for (i = 0; i < TextSize; i = i+2) {
output += String.fromCharCode(Temp[i ] - Temp2[i]);
}
return output;
}
can somebody help me to convert it to Java? thanks.
function Encrypt(theText){
output = new String;
Temp = new Array();
Temp2 = new Array();
TextSize = theText.length;
for (i = 0; i < TextSize; i++) {
rnd = Math.round(Math.random() * 122) + 68;
Temp[i] = theText.charCodeAt(i) + rnd;
Temp2[i] = rnd;
}
for (i = 0; i < TextSize; i++) {
output += String.fromCharCode(Temp[i
}
return output;
}
and I want to decrypt back the value in Java server side.but, I don't know how to convert the decrypt function into Java. I got the decrypt function in Javascript like this :
function Decrypt(theText){
output = new String;
Temp = new Array();
Temp2 = new Array();
TextSize = theText.length;
for (i = 0; i < TextSize; i++) {
Temp[i] = theText.charCodeAt(i);
Temp2[i] = theText.charCodeAt(i + 1);
}
for (i = 0; i < TextSize; i = i+2) {
output += String.fromCharCode(Temp[i
}
return output;
}
can somebody help me to convert it to Java? thanks.
How *can* you decrypt it if you've applied a random number in the encryption in that way?
ASKER
i got it when i was searching in google...i tested before and it can work both encrypt and decrypt function..but it's only in javascript. do you mean can't decrypt it in java?
You need to pass the encrypted text as well as the encryption key (the randomly generated number) to the server.
You need to modify your decryption program to include that key in the algo.
THanks
You need to modify your decryption program to include that key in the algo.
THanks
ASKER
actually, i got convert the decrypt function to java also,and it can work, but it cant work if i input a chinese character on the textfield...the decrypt function cant decrypt the encrypted value back to chinese character in java.
the decrypt function in java is like this :
public static String Decrypt(String theText){
String output = "";
int TextSize = theText.length();
int Temp[] = new int[TextSize];
int Temp2[] = new int[TextSize];
for (int i = 0; i < TextSize - 1; i++) {
Temp[i] = theText.codePointAt(i);
Temp2[i] = theText.codePointAt(i + 1);
}
for (int j = 0; j < TextSize; j = j+2) {
output += new Character((char) ((char) Temp[j] - Temp2[j])).toString();
}
return output;
}
the decrypt function in java is like this :
public static String Decrypt(String theText){
String output = "";
int TextSize = theText.length();
int Temp[] = new int[TextSize];
int Temp2[] = new int[TextSize];
for (int i = 0; i < TextSize - 1; i++) {
Temp[i] = theText.codePointAt(i);
Temp2[i] = theText.codePointAt(i + 1);
}
for (int j = 0; j < TextSize; j = j+2) {
output += new Character((char) ((char) Temp[j] - Temp2[j])).toString();
}
return output;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
1 more thing, the decrypt function in javascript can work with the chinese character...it can decrypt it back from the encrypted value..but my decrypt function in java cant do this...
ASKER
Hi gurvinder372, do you mean encode the encrypted string or the chinese string in javascript?
i simply meant that before calling the decrypt function with encrypted text as argument, you need to change the encoding of the string.
>>How *can* you decrypt it if you've applied a random number in the encryption in that way?
Ignore that - the key *is* stored in the cipher text
Ignore that - the key *is* stored in the cipher text
Good observation @CEHJ
@chinsw: Is your problem resolved after changing the encoding?
THanks
@chinsw: Is your problem resolved after changing the encoding?
THanks
ASKER
I'm doing like this :
byte[] utf8Bytes = theText.getBytes("UTF8");
String roundTrip = new String(utf8Bytes, "UTF16");
but,i still cant get the correct result. I see on the screen the encrypted text is like this :¿©¿w¿L¿j¿±
but when go into java,it shows funny character.
byte[] utf8Bytes = theText.getBytes("UTF8");
String roundTrip = new String(utf8Bytes, "UTF16");
but,i still cant get the correct result. I see on the screen the encrypted text is like this :¿©¿w¿L¿j¿±
but when go into java,it shows funny character.
please check these tools for chinese encoding converter
http://www.mandarintools.com/zhcode.html
http://www.chinesecomputing.com/programming/java.html
http://www.mandarintools.com/zhcode.html
http://www.chinesecomputing.com/programming/java.html