Tomas Ramirez Sarduy
Tomas Ramirez Sarduy

Reputation: 17471

Convert function from ActionScript to Javascript

I receive a get request from server, but is encrypted with a simple algorithm in Python, so I have this Method in ActionScript for decrypt this :

public static function Decrypt (encrypted : String) : String
{
var resultArray : ByteArray = new ByteArray();
for (var i:int = 0; i < encrypted.length; i++){
resultArray.writeByte(encrypted.charCodeAt(i) ^ 0x34);
} var resultString : String = resultArray.toString();
return resultString;
}

Now, I need to implement this function in Javascript, but there is no ByteArray class in JS, any idea of how i can do this? Code and librarys are welcome.

Upvotes: 1

Views: 550

Answers (3)

Diode
Diode

Reputation: 25135

 function Decrypt(encrypted) {
    var resultString = '';
    for (var i = 0; i < encrypted.length; i++) {
        resultString += String.fromCharCode(encrypted.charCodeAt(i) ^ 0x34);
    } 
    return resultString;
 }

Upvotes: 1

Unpaid Oracles
Unpaid Oracles

Reputation: 2247

Try something like this:

function Decrypt(encrypted) {
    var resultString = '';
    for (var i = 0; i < encrypted.length; i++) {
        resultString += (encrypted[i] ^ 0x34);
    } 
    return resultString
}

Upvotes: 1

user578895
user578895

Reputation:

Just replace it with a normal JS array ( [] ) and change resultArray.writeByte to resultArray.push. Also make resultArray.toString() into resultArray.join(''). All the rest of the code should work as is (assuming you drop things like public static, : String, :int, etc that aren't valid in JS)

Upvotes: 0

Related Questions