C# 位对位异或 &文件输入/输出

发布于 2024-10-20 11:13:07 字数 1869 浏览 1 评论 0原文

好的,我有一个 35 MB 的文件,它是通过位对位异或加密的(我相信是按位异或),我想知道一种解密它的好方法,然后使用 C# 上的文件 I/O 再次加密它。

这是 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

OK, I have a 35 MB file, that is Encrypted with Bit to Bit XOR(Bitwise XOR I believe) and I'd like to know a good way to decrypt it, and then encrypt it again, using File I/O on C#.

Here is the Enc/Dec algorithm:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

牵你的手,一向走下去 2024-10-27 11:13:07

这不是按位异或 - 它实际上是按位替换密码。您知道这只是最宽松意义上的“加密”,对吗?

基本上,您需要两个步骤:

  • 编写方法来转置位以进行加密/解密,每个方法采用 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);
}

您可以使用一个用于加密的表和一个用于解密的表来使这个表驱动,但我不确定我是否会打扰。

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

This isn't a bitwise XOR - it's effectively a bitwise substitution cypher. You realise it's only "encryption" in the loosest sense of the word, right?

Basically you'll need two steps:

  • Write methods to transpose bits for encryption/decryption, each one taking a 32-bit integer and returning a 32-bit integer
  • Read the file one 32-bit integer at a time, apply the appropriate operation and write out the result to the other file. You may well want to use BinaryReader and BinaryWriter for this.

(Obviously you can optimize with buffering, but that's the general gist.)

You may find it's easiest to work with uint instead of int to avoid worrying about sign bits. Something like this:

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

You could make this table-driven with a table for encryption and a table for decryption, but I'm not sure I'd bother.

Note that if you're actually using this to store sensitive information, you should start using real encryption as soon as possible.

靑春怀旧 2024-10-27 11:13:07

首先,您必须执行一个函数来获取单个位,然后执行一个函数将其保存在您想要的任何位置:

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);

First you have to do a function to get a single bit and a function to save it wherever you want:

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

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

Then you have to do each conversion manually, something like (if I understood your algorithm correctly):

int b1 = getBit(0, word);
int b2 = getBit(18, word);
setBit(0, b1 ^ b2, ref word);
悸初 2024-10-27 11:13:07

这不是XOR。如果是这样,您只需再次对具有相同值的数据进行异或即可解密它

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

正如其他人所说,这不是安全加密。它使用一种通常称为“通过模糊实现安全性”的方法。

That isn't XOR. If it was you'd simply XOR the data with the same value again to decrypt it.

What you are describing is some kind of bit scrambling encryption.

As others have said, this is not secure encryption. It uses a method commonly known as "security through obscurity."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文