【问题标题】:Bit manipulation from a ByteArray来自 ByteArray 的位操作
【发布时间】:2012-05-03 17:06:06
【问题描述】:

我想知道如何操作 ByteArray 中的位。 我需要的是根据我拥有的“表”来移动位。

表:

Bit 0 -> Bit 26
Bit 1 -> Bit 31
Bit 2 -> Bit 17
...
Bit 31 -> Bit 5

我使用这种方法将 ByteArray 转换为 BitArray

public static BitArray ByteArraytoBitArray(byte[] bytes)
{
    BitArray bits = new BitArray(bytes);
    return bits;
}

但我被困在那里,我不知道如何根据表格移位然后回到ByteArray。

编辑:

代码片段:

public static byte[] StringToByteArray(String hex)
{
    int NumberChars = hex.Length;
    byte[] bytes = new byte[NumberChars / 2];
    for (int i = 0; i < NumberChars; i += 2)
        bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
    return bytes;
}
private void button3_Click(object sender, EventArgs e)
{
    string featherearring = "00804804A02A1CA20100000000000000D2F8B6FABBB700000000000000000000";
    var strarray = StringToByteArray(featherearring);

    byte[] strarray_comp = Enc.Encrypt(strarray);

    string conv = BitConverter.ToString(strarray_comp);
    MessageBox.Show(conv.Replace("-", ""));
}



public static byte[] BitArrayToByteArray(BitArray bits)
{
    byte[] bytes = new byte[bits.Length / 8];
    bits.CopyTo(bytes, 0);
    return bytes;
}
public static byte[] Encrypt(byte[] input)
{
    BitArray source = new BitArray(input);
    BitArray target = new BitArray(source.Length);

    target[26] = source[0];
    target[31] = source[1];
    target[17] = source[2];
    target[10] = source[3];
    target[30] = source[4];
    target[16] = source[5];
    target[24] = source[6];
    target[2] = source[7];
    target[29] = source[8];
    target[8] = source[9];
    target[20] = source[10];
    target[15] = source[11];
    target[28] = source[12];
    target[11] = source[13];
    target[13] = source[14];
    target[4] = source[15];
    target[19] = source[16];
    target[23] = source[17];
    target[0] = source[18];
    target[12] = source[19];
    target[14] = source[20];
    target[27] = source[21];
    target[6] = source[22];
    target[18] = source[23];
    target[21] = source[24];
    target[3] = source[25];
    target[9] = source[26];
    target[7] = source[27];
    target[22] = source[28];
    target[1] = source[29];
    target[25] = source[30];
    target[5] = source[31];

    return BitArrayToByteArray(target);
}

my input byte array is "00804804A02A1CA20100000000000000D2F8B6FABBB700000000000000000000" and my output with zimdanen's code is "5012000000000000000000000000000000000000000000000000000000000000" and it should be "501200002FD901000000000400000000BFE8C4DB140D11F40000000000000000" 如您所见,它正确获取了前 2 个字节,但其余的全部为空。

【问题讨论】:

  • 必须快速还是正确?
  • 看看我对 [c# - left shift an entire byte array][1] 的回答。 [1]:stackoverflow.com/questions/8440938/…
  • 我不介意该方法不是最快的,我真的很关心它是否正确地进行操作。

标签: c# bytearray bit-manipulation


【解决方案1】:

它需要到位吗?您可以创建一个新的 BitArray,然后将这些位复制过来:

BitArray source = new BitArray(bytes);

// Create target array.
BitArray target = new BitArray(source.Length);

// Map bits.
// ...
target[26] = source[0];
// ...

或者,根据您要如何维护映射“表”,您可以这样做:

// Setup mapping - <source, target>.
Dictionary<int, int> mapping = new Dictionary<int, int>();
// ...
mapping.Add(0, 26);
// ...

BitArray source = new BitArray(bytes);

// Create target array.
BitArray target = new BitArray(source.Length);

// Map bits.
foreach (int sourceIndex in mapping.Keys)
{
    int targetIndex = mapping[sourceIndex];
    target[targetIndex] = source[sourceIndex];
}

【讨论】:

  • 不必就位,只需要相应地移动位,然后返回 ByteArray 编辑:我会看看你的代码,看看我是否能正常工作:)
  • my input byte array is "00804804A02A1CA20100000000000000D2F8B6FABBB700000000000000000000" and my output with your code is "5012000000000000000000000000000000000000000000000000000000000000" and it should be "501200002FD901000000000400000000BFE8C4DB140D11F40000000000000000" As you can see, it gets the first 2 bytes right, but then is all无效。
  • 这个Bit操作是对文件进行编码和解码,我也应该上传文件吗?
  • 正如 Jim 所说,您的映射仅适用于前 32 位。你的数据比这长得多。您需要修复映射。
  • 是的,谢谢!我打算一次将数据读入 4 个字节的数组。
【解决方案2】:

您的第一个问题是您有一个 32 字节的数组,您将其转换为 BitArray。你会发现source.Count属性的值是256。而target.Count只有32。所以你的Encrypt方法只改变了位数组的前32位,对应四个字节-- 八个十六进制字符。数组的其余部分将为空值。

您可以通过更改您的 BitArrayToByteArray 方法来验证这一点,在复制之前使用 0xFF 填充目标:

public static byte[] BitArrayToByteArray(BitArray bits)
{
    byte[] bytes = new byte[bits.Length / 8];
    // Fill the array with 0xFF to illustrate.
    for (int i = 0; i < bytes.Length; ++i)
    {
        bytes[i] = 0xFF;
    }
    bits.CopyTo(bytes, 0);
    return bytes;
}

我想你会发现结果是“50120000FFFFFF....”

很难准确地说出您要做什么。如果您想打乱字符串中的字节,则无需使用BitArray。如果你真的想加扰这些位,那么你需要加扰所有的位。如果没有有关您要做什么的更多信息,我没有建议。除了也许你应该使用许多现有的加密算法中的一种,而不是尝试使用你自己的。

【讨论】:

  • 太棒了!谢谢你!我并不是真的想在这里写我自己的算法,只是从游戏中解密和加密一个文件。 :)
猜你喜欢
  • 1970-01-01
  • 2011-04-01
  • 1970-01-01
  • 2014-01-05
  • 2012-06-16
  • 2013-10-06
  • 1970-01-01
  • 2013-06-12
相关资源
最近更新 更多