The purpose for this is not highly security-relevant and the key will be long, so I'm just wanting to use simple XOR encryption to the strings.
Well, the Javascript on the client is as follows:
function dc_encrypt(str, key)
{
var ord = []; var res = ;
var i;
for (i = 1; i <= 255; i++) {ord[String.fromCharCode(i)] = i}
for (i = 0; i < str.length; i++)
res += String.fromCharCode(ord[str.substr(i, 1)] ^ ord[key.substr(i % key.length, 1)]);
return(res);
}
And the Java is is:
public String dc_decrypt(String str, String key)
{
StringBuilder sb = new StringBuilder();
for(int i = 0; i < str.length(); i++)
sb.append((char)(str.charAt(i) ^ key.charAt(i % (key.length()))));
return(sb.toString());
}
Unfortunately this produces some very weird results. Some letters differ after encrypting in JS, sending the result through a POST and decrypt in Java.
In every case it doesn't seem to be reliable.
I assume the issue must have something to do with encoding... does someone know a more reliable solution for this?
Huge thanks in advance! :)