移植含无符号的字符指针在C C#code [英] Porting code containing unsigned char pointer in C to C#

查看:250
本文介绍了移植含无符号的字符指针在C C#code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须用C本code,我需要移植到C#:

I have this code in C that I need to port to C#:

void CryptoBuffer(unsigned char *Buffer, unsigned short length)
{
    unsigned short i;
    for(i=0; i < length; i++)
    {
        *Buffer ^= 0xAA;
        *Buffer++ += 0xC9;
    }
}

我试过这样:

public void CryptoBuffer(byte[] buffer, int length)
{
    for(int i = 0; i < length; i++)
    {
        buffer[i] ^= 0xAA;
        buffer[i] += 0xC9;
    }
}

但结果一个预期不匹配。

But the outcome doesn't match the one expected.

根据该实施例,这样的:

According to the example, this:

A5 03 18 01...

应该成为这样的:

should become this:

A5 6F 93 8B...

它还说,第一个字节是不加密的,所以这就是为什么A5保持不变。

It also says the first byte is not encrypted, so that's why A5 stays the same.

修改澄清:该规范只是说你应该跳过第一个字节,它不赘述了,所以我猜你只是从位置1通过序列,直到最后一个位置跳过第一个字节。

EDIT for clarification: The specification just says you should skip the first byte, it doesn't go into details, so I'm guessing you just pass the sequence from position 1 until the last position to skip the first byte.

但我与C#端口的结果是:

But my outcome with that C# port is:

A5 72 7B 74...

这是正确的端口还是我失去了一些东西?

Is this port correct or am I missing something?

编辑2 :为进一步澄清,这是一个封闭的协议,所以我就不赘述了,这就是为什么我只提供了足够的信息来帮助我端口code,即C code为这是给我的人,这就是规范表示将尽。
真正的问题是,和0xAA是错误的规范,这就是为什么输出不是所预期的。在C#code这里提供和接受的答案是正确的毕竟。

EDIT 2: For further clarification, this is a closed protocol, so I can't go into details, that's why I provided just enough information to help me port the code, that C code was the one that was given to me, and that's what the specification said it would do. The real problem was that the "0xAA" was wrong in the specification, that's why the output wasn't the expected one. The C# code provided here and by the accepted answer are correct after all.

推荐答案

让我们来分析一下好吗,一步一个脚印的时间。

Let's break it down shall we, one step at a time.

void CryptoBuffer(unsigned char *Buffer, unsigned short length)
{
    unsigned short i;
    for(i=0; i < length; i++)
    {
        *Buffer ^= 0xAA;
        *Buffer++ += 0xC9;
    }
}

不管其他一些言论,这的的你怎么通常做这些东西在C / C ++。没有什么花哨的这个code是,这是不是太复杂,但我认为这是好事,打破它给你看会发生什么。

Regardless of some other remarks, this is how you normally do these things in C/C++. There's nothing fancy about this code, and it isn't overly complicated, but I think it is good to break it down to show you what happens.

注意事项:


  1. unsigned char型是基本相同,在C#中的字节

  2. 无符号长度具有0-65536之间的值。诠释应该做的伎俩。

  3. 缓冲器具有后增量

  4. 字节赋值(+ = 0xC9)会溢出。如果溢出它截短为8位在这种情况下。

  5. 的缓冲区由PTR过去了,所以在调用方法的指针将保持不变。

  6. 这只是基本的C code,没有C ++。这是相当安全的假设人不使用操作符重载这里。

唯一的难在这里就是缓冲区++。详细信息可在该书例外C ++,从萨特读取,但一个小的例子说明了这一点。幸运的是,我们有我们所掌握的一个很好的例子。 A 文字以上code的翻译是:

The only "difficult" thing here is the Buffer++. Details can be read in the book "Exceptional C++" from Sutter, but a small example explains this as well. And fortunately we have a perfect example at our disposal. A literal translation of the above code is:

void CryptoBuffer(unsigned char *Buffer, unsigned short length)
{
    unsigned short i;
    for(i=0; i < length; i++)
    {
        *Buffer ^= 0xAA;
        unsigned char *tmp = Buffer;
        *tmp += 0xC9;
        Buffer = tmp + 1;
    }
}

在这种情况下,临时变量可以平凡解决,这使我们

In this case the temp variable can be solved trivially, which leads us to:

void CryptoBuffer(unsigned char *Buffer, unsigned short length)
{
    unsigned short i;
    for(i=0; i < length; i++)
    {
        *Buffer ^= 0xAA;
        *Buffer += 0xC9;
        ++Buffer;
    }
}

更改此code到C#现在pretty容易:

Changing this code to C# now is pretty easy:

private void CryptoBuffer(byte[] Buffer, int length)
{
    for (int i=0; i<length; ++i) 
    {
        Buffer[i] = (byte)((Buffer[i] ^ 0xAA) + 0xC9);
    }
}

这是基本相同的端口code。这意味着,在路上的东西别的地方出了问题......所以,让我们破解cryptobuffer好吗? : - )

This is basically the same as your ported code. This means that somewhere down the road something else went wrong... So let's hack the cryptobuffer shall we? :-)

如果我们假设第一个字节没有使用(如你所说),而'和0xAA和/或0xC9是错误的,我们可以简单地尝试所有组合:

If we assume that the first byte isn't used (as you stated) and that the '0xAA' and/or the '0xC9' are wrong, we can simply try all combinations:

static void Main(string[] args)
{
    byte[] orig = new byte[] { 0x03, 0x18, 0x01 };
    byte[] target = new byte[] { 0x6F, 0x93, 0x8b };

    for (int i = 0; i < 256; ++i)
    {
        for (int j = 0; j < 256; ++j)
        {
            bool okay = true;
            for (int k = 0; okay && k < 3; ++k)
            {
                byte tmp = (byte)((orig[k] ^ i) + j);
                if (tmp != target[k]) { okay = false; break; }
            }
            if (okay)
            {
                Console.WriteLine("Solution for i={0} and j={1}", i, j);
            }
        }
    }
    Console.ReadLine();
}

有我们去:哎呀有没有解决方案。这意味着,cryptobuffer是不是做你认为这是干什么,或者C $ C $的C部分是在这里失踪。 F.ex.他们是否真的通过缓冲区的CryptoBuffer方法还是他们之前改变指针?

There we go: oops there are no solutions. That means that the cryptobuffer is not doing what you think it's doing, or part of the C code is missing here. F.ex. do they really pass 'Buffer' to the CryptoBuffer method or did they change the pointer before?

最后,我想这里唯一的好答案是解​​决这个问题是缺少关键信息。

Concluding, I think the only good answer here is that critical information for solving this question is missing.

这篇关于移植含无符号的字符指针在C C#code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆