You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@royale.apache.org by Hugo Ferreira <hf...@gmail.com> on 2020/08/29 08:50:31 UTC

Help please with XOR class (full source code shared)

Hello,

I have a XOR class in both Flex and Royale projects.
This class share 100% the same code.

Executing this method in both Flex and Royale with "AAA" string I get the
same result but for example with the string "a", I get different results.
Seems to me that String.fromCharCode return a space string in special char
codes like 12 (I saw that the same happens in pure JS).

Flex:

var result1:String = XOR.encode("AAA", "MyKey"); // result = DDgK
var result2:String = XOR.encode("a", "MyKey");   // result = LA==

Royale:

var result1:String = XOR.encode("AAA", "MyKey"); // result = DDgK
var result2:String = XOR.encode("a", "MyKey"); // result = LAAA

public class XOR
{
private static function xor(source:String, key:String):String
{
var result:String = new String();
for (var i:Number = 0; i < source.length; i++)
{
if (i > (key.length - 1))
key += key;
result += String.fromCharCode(source.charCodeAt(i) ^ key.charCodeAt(i));
}
return Strings.toUTF8(result);
}

public static function encode(source:String, key:String):String
{
return Base64.encode(xor(source, key));
}
public static function decode(source:String, key:String):String
{
if (source == null)
return null;
return xor(Base64.decode(source), key);
}
}

Re: Help please with XOR class (full source code shared)

Posted by Hugo Ferreira <hf...@gmail.com>.
Problem solved.
The error was on the base64 encode function, so I just switched to the
native JS function for that.

public class XOR
{
private static function xor(source:String, key:String):String
{
var result:String = "";
for (var i:Number = 0; i < source.length; i++)
{
result += String.fromCharCode(source.charCodeAt(i) ^ key.charCodeAt(i));
}
return result;
}

public static function encode(source:String, key:String):String
{
return btoa(xor(source, key));
}
public static function decode(source:String, key:String):String
{
if (source == null)
return null;
return xor(atob(source), key);
}
}

Hugo Ferreira <hf...@gmail.com> escreveu no dia sábado, 29/08/2020
à(s) 09:50:

> Hello,
>
> I have a XOR class in both Flex and Royale projects.
> This class share 100% the same code.
>
> Executing this method in both Flex and Royale with "AAA" string I get the
> same result but for example with the string "a", I get different results.
> Seems to me that String.fromCharCode return a space string in special char
> codes like 12 (I saw that the same happens in pure JS).
>
> Flex:
>
> var result1:String = XOR.encode("AAA", "MyKey"); // result = DDgK
> var result2:String = XOR.encode("a", "MyKey");   // result = LA==
>
> Royale:
>
> var result1:String = XOR.encode("AAA", "MyKey"); // result = DDgK
> var result2:String = XOR.encode("a", "MyKey"); // result = LAAA
>
> public class XOR
> {
> private static function xor(source:String, key:String):String
> {
> var result:String = new String();
> for (var i:Number = 0; i < source.length; i++)
> {
> if (i > (key.length - 1))
> key += key;
> result += String.fromCharCode(source.charCodeAt(i) ^ key.charCodeAt(i));
> }
> return Strings.toUTF8(result);
> }
>
> public static function encode(source:String, key:String):String
> {
> return Base64.encode(xor(source, key));
> }
> public static function decode(source:String, key:String):String
> {
> if (source == null)
> return null;
> return xor(Base64.decode(source), key);
> }
> }
>