how to decrypt the encrypted value from Javascript in Java

chinsw
chinsw used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016

Commented:
How *can* you decrypt it if you've applied a random number in the encryption in that way?

Author

Commented:
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
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
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;
    }
i guess because the chinese character are 2 bytes long and the string that we pass is 1 byte long by default.

Can you try converting the string to utf-16 encode and then call this function?
check this link
http://java.sun.com/docs/books/tutorial/i18n/text/string.html

Thanks

Author

Commented:
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...

Author

Commented:
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.
Top Expert 2016

Commented:
>>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
Good observation @CEHJ

@chinsw: Is your problem resolved after changing the encoding?
THanks

Author

Commented:
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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial