C#Bit to Bit XOR&文件I / O.

时间:2011-03-06 20:29:48

标签: c# io bit-manipulation xor

好的,我有一个35 MB的文件,加密的Bit to Bit XOR(我认为是Bitwise XOR),我想知道一种解密它的好方法,然后再使用File I /加密它O#C#。

这是Enc / Dec算法:

Encrypt:----------------------Decrypt:
Bit  0 -> Bit 26--------------Bit  0 -> Bit 18
Bit  1 -> Bit 31--------------Bit  1 -> Bit 29
Bit  2 -> Bit 17--------------Bit  2 -> Bit  7
Bit  3 -> Bit 10--------------Bit  3 -> Bit 25
Bit  4 -> Bit 30--------------Bit  4 -> Bit 15
Bit  5 -> Bit 16--------------Bit  5 -> Bit 31
Bit  6 -> Bit 24--------------Bit  6 -> Bit 22
Bit  7 -> Bit  2--------------Bit  7 -> Bit 27
Bit  8 -> Bit 29--------------Bit  8 -> Bit  9
Bit  9 -> Bit  8--------------Bit  9 -> Bit 26
Bit 10 -> Bit 20--------------Bit 10 -> Bit  3
Bit 11 -> Bit 15--------------Bit 11 -> Bit 13
Bit 12 -> Bit 28--------------Bit 12 -> Bit 19
Bit 13 -> Bit 11--------------Bit 13 -> Bit 14
Bit 14 -> Bit 13--------------Bit 14 -> Bit 20
Bit 15 -> Bit  4--------------Bit 15 -> Bit 11
Bit 16 -> Bit 19--------------Bit 16 -> Bit  5
Bit 17 -> Bit 23--------------Bit 17 -> Bit  2
Bit 18 -> Bit  0--------------Bit 18 -> Bit 23
Bit 19 -> Bit 12--------------Bit 19 -> Bit 16
Bit 20 -> Bit 14--------------Bit 20 -> Bit 10
Bit 21 -> Bit 27--------------Bit 21 -> Bit 24
Bit 22 -> Bit  6--------------Bit 22 -> Bit 28
Bit 23 -> Bit 18--------------Bit 23 -> Bit 17
Bit 24 -> Bit 21--------------Bit 24 -> Bit  6
Bit 25 -> Bit  3--------------Bit 25 -> Bit 30
Bit 26 -> Bit  9--------------Bit 26 -> Bit  0
Bit 27 -> Bit  7--------------Bit 27 -> Bit 21
Bit 28 -> Bit 22--------------Bit 28 -> Bit 12
Bit 29 -> Bit  1--------------Bit 29 -> Bit  8
Bit 30 -> Bit 25--------------Bit 30 -> Bit  4
Bit 31 -> Bit  5--------------Bit 31 -> Bit  1

3 个答案:

答案 0 :(得分:4)

这不是一个按位异或 - 它实际上是一个按位substitution cypher。你意识到它只是在最松散的意义上的“加密”,对吗?

基本上你需要两个步骤:

  • 编写用于转换加密/解密位的方法,每个方法采用32位整数并返回32位整数
  • 一次读取一个32位整数的文件,应用适当的操作并将结果写出到另一个文件。您可能希望使用BinaryReaderBinaryWriter来实现此目的。

(显然你可以通过缓冲优化,但这是一般的要点。)

您可能会发现最简单的方法是使用uint代替int,以避免担心签名位。像这样:

public static uint Encrypt(uint input)
{
    return (((input >> 0) & 1) << 26) |
           (((input >> 1) & 1) << 31) |
           (((input >> 2) & 1) << 17) |
           ...
           (((input >> 31) & 1) << 5);
}

您可以使用加密表和解密表创建此表驱动程序,但我不确定是否会烦恼。

请注意,如果您实际使用它来存储敏感信息,则应尽快开始使用真实加密。

答案 1 :(得分:1)

那不是XOR。如果是,您只需将具有相同值的数据再次与decrypt it进行异或。

您所描述的是某种加密加密。

正如其他人所说,这不是安全加密。它使用通常称为“security through obscurity。”的方法。

答案 2 :(得分:1)

首先,你必须做一个函数来获得一个位和一个函数,以便将它保存在任何你想要的地方:

int getBit(int position, int word)
{
    return ((word >> position) & 1);
}

void setBit(int position, int value, ref word)
{
    word = (word & (value << position));
}

然后你必须手动完成每次转换,例如(如果我理解你的算法正确):

int b1 = getBit(0, word);
int b2 = getBit(18, word);
setBit(0, b1 ^ b2, ref word);
相关问题