实现Byte []操作的麻烦

时间:2017-01-11 09:33:43

标签: c# arrays algorithm boolean-logic

我对byte []

使用XOR运算
public static byte[] XOR(byte[] buffer1, byte[] buffer2)
{
    for (int i = 0; i < buffer1.Length; i++)
        buffer1[i] ^= buffer2[i];
    return buffer1;
}

实现散列算法的移位方法。

public static byte[] RotHi(byte[] B, int count)
{
    for (int i = 0; i < count; i++)
    {
        byte[] a = ShHi(B);
        byte[] b = B;
        for (int j = (B.Length * 8) - 2; j >=0 ; j--)
        {
            b = ShLo(b);
        }
        B = XOR(a,b);
    }
    return B;
}
public static byte[] ShHi(byte[] B)
{
    return BitConverter.GetBytes(BitConverter.ToUInt64(B, 0) >> 1);
}
public static byte[] ShLo(byte[] B)
{
    return BitConverter.GetBytes(BitConverter.ToUInt64(B, 0) << 1);
}

当它单独工作时结果是正确的

W0 = b194bac80a08f53b.
W1 = e12bdc1ae28257ec.
W2 = e9dee72c8f0c0fa6.
m1 = 8; n1 = 53.

T0 = RotHi(W0, m1); // 3bb194bac80a08f5
W0 = XOR(W0,W1);
W0 = XOR(W0,W2); // b96181fe6786ad71

但下一个代码返回wring结果^

T1 = XOR(W1,(RotHi(W0, n1))); // ed242f26d7e9da27, but should be CDFB23D652B779DB

出了什么问题?

1 个答案:

答案 0 :(得分:0)

麻烦在于字节顺序。注意BigEndian或LittleEndian。