我正在向Javascript移植一个简单的C ++函数,但似乎我遇到了Javascript处理按位运算符的问题。
在C ++中:
AnsiString MyClass::Obfuscate(AnsiString source)
{
int sourcelength=source.Length();
for(int i=1;i<=sourcelength;i++)
{
source[i] = source[i] ^ 0xFFF;
}
return source;
}
Obfuscate(“test”)产生临时的intvalues
-117, -102, -116, -117
Obfuscate(“test”)产生stringvalue
‹šŒ‹
在Javascript中:
function obfuscate(str)
{
var obfuscated= "";
for (i=0; i<str.length;i++) {
var a = str.charCodeAt(i);
var b = a ^ 0xFFF;
obfuscated= obfuscated+String.fromCharCode(b);
}
return obfuscated;
}
obfuscate(“test”)产生临时的intvalues
3979 , 3994 , 3980 , 3979
obfuscate(“test”)产生stringvalue
ྋྚྌྋ
现在,我意识到有很多线程,他们指出Javascript将所有数字视为浮点数,而按位操作涉及临时转换为32位int。
除非我在Javascript中混淆并在C ++中进行反转,否则它确实不会成为问题,并且不同的结果并不匹配。
如何将Javascript结果转换为C ++结果?是否有一些简单的班次?
答案 0 :(得分:4)
根据x 116
与0xFFF
给出-117的结果判断,我们必须模仿
2补码javascript中的8位整数:
function obfuscate(str)
{
var bytes = [];
for (var i=0; i<str.length;i++) {
bytes.push( ( ( ( str.charCodeAt(i) ^ 0xFFF ) & 0xFF ) ^ 0x80 ) -0x80 );
}
return bytes;
}
好的这些字节在windows cp 1252中被解释,如果它们是负数,可能只是从256减去。
var ascii = [
0x0000,0x0001,0x0002,0x0003,0x0004,0x0005,0x0006,0x0007,0x0008,0x0009,0x000A,0x000B,0x000C,0x000D,0x000E,0x000F
,0x0010,0x0011,0x0012,0x0013,0x0014,0x0015,0x0016,0x0017,0x0018,0x0019,0x001A,0x001B,0x001C,0x001D,0x001E,0x001F
,0x0020,0x0021,0x0022,0x0023,0x0024,0x0025,0x0026,0x0027,0x0028,0x0029,0x002A,0x002B,0x002C,0x002D,0x002E,0x002F
,0x0030,0x0031,0x0032,0x0033,0x0034,0x0035,0x0036,0x0037,0x0038,0x0039,0x003A,0x003B,0x003C,0x003D,0x003E,0x003F
,0x0040,0x0041,0x0042,0x0043,0x0044,0x0045,0x0046,0x0047,0x0048,0x0049,0x004A,0x004B,0x004C,0x004D,0x004E,0x004F
,0x0050,0x0051,0x0052,0x0053,0x0054,0x0055,0x0056,0x0057,0x0058,0x0059,0x005A,0x005B,0x005C,0x005D,0x005E,0x005F
,0x0060,0x0061,0x0062,0x0063,0x0064,0x0065,0x0066,0x0067,0x0068,0x0069,0x006A,0x006B,0x006C,0x006D,0x006E,0x006F
,0x0070,0x0071,0x0072,0x0073,0x0074,0x0075,0x0076,0x0077,0x0078,0x0079,0x007A,0x007B,0x007C,0x007D,0x007E,0x007F
];
var cp1252 = ascii.concat([
0x20AC,0xFFFD,0x201A,0x0192,0x201E,0x2026,0x2020,0x2021,0x02C6,0x2030,0x0160,0x2039,0x0152,0xFFFD,0x017D,0xFFFD
,0xFFFD,0x2018,0x2019,0x201C,0x201D,0x2022,0x2013,0x2014,0x02DC,0x2122,0x0161,0x203A,0x0153,0xFFFD,0x017E,0x0178
,0x00A0,0x00A1,0x00A2,0x00A3,0x00A4,0x00A5,0x00A6,0x00A7,0x00A8,0x00A9,0x00AA,0x00AB,0x00AC,0x00AD,0x00AE,0x00AF
,0x00B0,0x00B1,0x00B2,0x00B3,0x00B4,0x00B5,0x00B6,0x00B7,0x00B8,0x00B9,0x00BA,0x00BB,0x00BC,0x00BD,0x00BE,0x00BF
,0x00C0,0x00C1,0x00C2,0x00C3,0x00C4,0x00C5,0x00C6,0x00C7,0x00C8,0x00C9,0x00CA,0x00CB,0x00CC,0x00CD,0x00CE,0x00CF
,0x00D0,0x00D1,0x00D2,0x00D3,0x00D4,0x00D5,0x00D6,0x00D7,0x00D8,0x00D9,0x00DA,0x00DB,0x00DC,0x00DD,0x00DE,0x00DF
,0x00E0,0x00E1,0x00E2,0x00E3,0x00E4,0x00E5,0x00E6,0x00E7,0x00E8,0x00E9,0x00EA,0x00EB,0x00EC,0x00ED,0x00EE,0x00EF
,0x00F0,0x00F1,0x00F2,0x00F3,0x00F4,0x00F5,0x00F6,0x00F7,0x00F8,0x00F9,0x00FA,0x00FB,0x00FC,0x00FD,0x00FE,0x00FF
]);
function toStringCp1252(bytes){
var byte, codePoint, codePoints = [];
for( var i = 0; i < bytes.length; ++i ) {
byte = bytes[i];
if( byte < 0 ) {
byte = 256 + byte;
}
codePoint = cp1252[byte];
codePoints.push( codePoint );
}
return String.fromCharCode.apply( String, codePoints );
}
结果
toStringCp1252(obfuscate("test"))
//"‹šŒ‹"
答案 1 :(得分:1)
我认为AnsiString
是某种形式,char
的数组。这就是问题所在。在c中,char
通常只能容纳8位。因此,当您使用0xfff
进行异或,并将结果存储在char
时,它与使用0xff
的XORing相同。
javascript不是这种情况。使用Unicode的JavaScript。通过查看整数值来证明这一点:
-117 == 0x8b
和3979 == 0xf8b
我建议使用0xff
进行异或,因为这适用于两种语言。或者,您可以切换c++代码以使用Unicode。
答案 2 :(得分:1)
我猜测AnsiString
包含8位字符(因为ANSI字符集是8位)。当您将XOR的结果分配回字符串时,它会被截断为8位,因此结果值的范围为[-128 ... 127]。
(在某些平台上,它可能是[0..255],而在其他平台上,范围可能更宽,因为没有指定char
是有符号还是无符号,或者它是8位还是更大)
Javascript字符串包含unicode字符,可以包含更宽范围的值,结果不会截断为8位。 XOR的结果将具有至少12位的范围,[0 ... 4095],因此您看到的数字很大。
假设原始字符串仅包含8位字符,那么将操作更改为a ^ 0xff
应该会在两种语言中产生相同的结果。
答案 3 :(得分:0)
首先,将您的AnsiString
转换为wchar_t*
。然后才混淆其个别角色:
AnsiString MyClass::Obfuscate(AnsiString source)
{
/// allocate string
int num_wchars = source.WideCharBufSize();
wchar_t* UnicodeString = new wchar_t[num_wchars];
source.WideChar(UnicodeString, source.WideCharBufSize());
/// obfuscate individual characters
int sourcelength=source.Length();
for(int i = 0 ; i < num_wchars ; i++)
{
UnicodeString[i] = UnicodeString[i] ^ 0xFFF;
}
/// create obfuscated AnsiString
AnsiString result = AnsiString(UnicodeString);
/// delete tmp string
delete [] UnicodeString;
return result;
}
抱歉,我不是C ++ Builder的专家,但我的观点很简单:在JavaScript中你有WCS2符号(或UTF-16),所以你必须先将AnsiString
转换成宽字符。< / p>
尝试使用WideString
代替AnsiString
答案 4 :(得分:0)
我根本不知道AnsiString
,但我的猜测是这与其角色的宽度有关。具体来说,我怀疑它们的宽度小于32位,当然还有按位操作,你操作的宽度与问题有关,特别是在处理2的补码数时。
在JavaScript中,"t"
中的"test"
是字符代码116,即b00000000000000000000000001110100。 0xFFF(4095)是b00000000000000000000111111111111,你得到的结果(3979)是b00000000000000000000111110001011。我们可以很容易地看到您为XOR获得了正确的结果:
116 = 00000000000000000000000001110100 4095 = 00000000000000000000111111111111 3979 = 00000000000000000000111110001011
所以我认为你在C ++代码中得到了一些截断或类似的东西,尤其是因为-117是8位2补码中的b10001011 ...这正是我们的意思看作上面3979的最后八位。