convert encryption/decryption program from action script to java
Hi friends,
I am trying to convert the encryption/decryption program that was written in Action script and finding its java equivalent. The developer who wrote the action script code tells me that he used "TEA" algorithm to do it. But i went searching for an equivalent TEAm algorithm and run it, Unfortunately the result that get is different from the result from action script. Can anyone help me in this please. Thanks in advance
I have pasted the action script codes here.
//apiTEA.prototype.encrypt = function(src, key, token) {
WIN_apiTEA.prototype.encrypt = function(src, key) {
if(src.length == 0) return "";
var v = this.charsToLongs(this.strToChars(src));
var k = this.charsToLongs(this.hexToChars(key));
var n = v.length;
if (n % 2==1) v[n++] = 0;
tempv = new Array(2);
newv = new Array(v.length);
for (i=0; i<v.length; i=i+2){
tempv = this.mcrypt_encrypt(k,v,v[i+1]);
newv[i] = tempv[0];
newv[i+1] = tempv[1];
}
var encStr = this.charsToHex(this.longsToChars(newv));
return [encStr];
}
WIN_apiTEA.prototype.mcrypt_encrypt = function(k,y,z)
{
sum = 0;
while (sum != -957401312) {
y = int(y + ((((z << 4) ^ (z >>> 5)) + z) ^ (sum + k[sum & 3])));
sum = int(sum + 0x9E3779B9);
z = int(z + ((((y << 4) ^ (y >>> 5)) + y) ^ (sum + k[(sum >>> 11) & 3])));
}
return [y,z];
}
WIN_apiTEA.prototype.hexToChars = function(hex) {
var codes = [];
var hexArr = hex.split("");
var hl = hexArr.length/2;
for(var i=0;i<hl;i++) {
codes[i] = int("0x"+hexArr[i*2]+hexArr[(i*2)+1]);
}
return codes;
}
WIN_apiTEA.prototype.charsToLongs = function(chars) {
var tlength = Math.ceil(chars.length/4);
var temp = [];
var ti = 0;
for(var i = 0; i<tlength; i++){
ti = i*4;
temp[i] = (((chars[ti] << 24) + (chars[ti+1]<<16)) + (chars[ti+2]<<8)) + chars[ti+3];
}
return temp;
}
WIN_apiTEA.prototype.longsToChars = function(longs) {
var codes = [];
var ll = longs.length;
var x = 0;
for(var i = 0; i<ll; i++) {
codes[x++] = longs[i]>>>24 & 0xFF;
codes[x++] = longs[i]>>>16 & 0xFF;
codes[x++] = longs[i]>>>8 & 0xFF;
codes[x++] = longs[i] & 0xFF;
}
return codes;
}
WIN_apiTEA.prototype.charsToStr = function(chars) {
var temp = "";
var cl = chars.length;
for(var i = 0; i<cl; i++) {
temp += chr(chars[i]);
}
return temp;
}
WIN_apiTEA.prototype.strToChars = function(src) {
var codes = [];
var strArr = src.split("");
var sl = strArr.length;
var cnt = 0;
for(var i = 0; i<sl; i++) {
codes[i] = ord(strArr[i]);
}
return codes;
}
WIN_apiTEA.prototype.changeKey = function(enctext, keytext){
return (enctext.substr(0, 8) + keytext.substr(8, 24));
}