简体   繁体   English

字符串数组的字节操作

[英]Byte manipulation of string array

This question is a follow up to this previous question that I solved: 这个问题是我之前解决的这个问题的跟进:

manipulating bytes in a binary string 处理二进制字符串中的字节

In that question I explained that I have a working encryption communication between php and c#. 在这个问题中,我解释说我在php和c#之间进行了有效的加密通信。 I added a slight byte tweak to increase the obscurity level just a bit more. 我添加了一些字节调整来增加模糊度级别。 I have noticed however that about 1 in 50 times it fails to understand the encrypted content. 但是,我注意到它大约有50倍无法理解加密内容。 This leads me to believe that my byte change- and byte revert code is not accounting for some edge case. 这使我相信我的字节更改和字节还原代码不能解决某些边缘情况。

Note that I tested this with the same message over and over so the only thing that is changing is the IV which is randomized each time and appended in front of the encrypted data. 请注意,我一遍又一遍地用相同的消息对它进行了测试,因此唯一改变的是IV,它每次都被随机化并附加在加密数据的前面。

Here is what I am doing on the PHP side which encrypts the response. 这是我在对响应进行加密的PHP方面所做的事情。 The idea is to roll the character over by 5, accounting for the limitations of base64 这个想法是将字符翻转5,考虑到base64的局限性

$data = getEncryptedBase64Data();
$ordVal = ord($data[strlen($data)-5]);
if($ordVal == 65)//'A'
{
    $ordVal = 47;//'/'
}
else if($ordVal == 48)//'0'
{
    $ordVal = 122;//'z'
}
else if($ordVal == 47)//'/'
{
    $ordVal = 45;//'+'
}
else if($ordVal == 45) //'+'
{
    $ordVal = 57;//'9'
}
else //B through z and 1 through 9
{
    $ordVal--;
}
$data[strlen($data)-5] = chr($ordVal);
return $data;

And here is what I am doing on the C# side which decrypts the response after it gets a Base64 string from the http response 这是我在C#端正在做的事情,它将在从HTTP响应中获取Base64字符串后解密响应

string webText = //The content from the http response
byte[] decoded = Utils.FromBase64ServerString(webText);
//do decryption

where FromBase64ServerString is defined as 其中FromBase64ServerString定义为

//Roll the 5th last bit to make things harder to decrypt
    char[] chars = s.ToCharArray();
    char ordinal = chars[chars.Length - 5];
    if(ordinal == 'z')
    {
        ordinal = '0';
    }
    else if(ordinal == '+')
    {
        ordinal = '/';
    }
    else if(ordinal == '/')
    {
        ordinal = 'A';
    }
    else if(ordinal == '9')
    {
        ordinal = '+';
    }
    else
    {
        ordinal++;
    }
    chars[chars.Length - 5] = ordinal;
    return Convert.FromBase64CharArray(chars,0,chars.Length);

Summary - Encryption code works fine on its own - Adding a byte inc\\dec after encryption and before decryption to reverse it only causes problems 5% of the time or so. 简介-加密代码本身可以正常工作-加密后和解密反向之前添加字节inc \\ dec只会导致大约5%的时间出现问题。 - Removing the byte fiddling and letting it spam messages back and forth all day results in no errors. -删除麻烦的字节并整天来回发送垃圾邮件不会导致任何错误。

I believe the problem occurs because else if($ordVal == 45) in PHP code, the correct ASCII value for + is 43 not 45 (45 is - character) which cause the C# side to decode the string incorrectly. 我相信会出现此问题,因为在PHP代码中else if($ordVal == 45)+的正确ASCII值是43而不是45(45是-字符),这会导致C#端错误地解码字符串。 changing PHP code to following snippet should solve the problem. 将PHP代码更改为以下代码段即可解决该问题。

$data = getEncryptedBase64Data();
$ordVal = ord($data[strlen($data)-5]);
if($ordVal == 65)//'A'
{
    $ordVal = 47;//'/'
}
else if($ordVal == 48)//'0'
{
    $ordVal = 122;//'z'
}
else if($ordVal == 47)//'/'
{
    $ordVal = 43;//'+'
}
else if($ordVal == 43) //'+'
{
    $ordVal = 57;//'9'
}
else //B through z and 1 through 9
{
    $ordVal--;
}
$data[strlen($data)-5] = chr($ordVal);
return $data;

I think that you must be getting collisions. 我认为您一定会遇到碰撞。 If the ordinal-- produces one of your special chars then you will not reverse if correctly. 如果顺序号产生了您的特殊字符之一,那么正确的话就不会反转。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM