使用插入符号操作进行字符异或

发布于 2024-09-08 13:36:23 字数 388 浏览 5 评论 0原文

对位进行异或运算对我来说是很清楚的。但在这里,XOR 正在处理单个字符。那么这是否意味着组成字符的字节正在被异或?这看起来像什么?

#include <iostream.h>
int main()
{
  char string[11]="A nice cat";
  char key[11]="ABCDEFGHIJ";
  for(int x=0; x<10; x++)
  {
    string[x]=string[x]^key[x];
    cout<<string[x];
  }
  return 0;
}

我知道位异或看起来像这样:
1010
1100
0110

Working with exclusive-OR on bits is something which is clear to me. But here, XOR is working on individual characters. So does this mean the byte which makes up the character is being XORed? What does this look like?

#include <iostream.h>
int main()
{
  char string[11]="A nice cat";
  char key[11]="ABCDEFGHIJ";
  for(int x=0; x<10; x++)
  {
    string[x]=string[x]^key[x];
    cout<<string[x];
  }
  return 0;
}

I know bits XORed look like this:
1010
1100
0110

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

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

发布评论

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

评论(5

剑心龙吟 2024-09-15 13:36:24

异或有一个很好的特性,如果使用相同的数据对某个内容进行异或两次,您将获得原始数据。您发布的代码是一些基本的加密函数,它使用密钥“加密”字符串。生成的密文可以通过同一程序进行解密。

XOR has the nice property that if you XOR something twice using the same data, you obtain the original. The code you posted is some rudimentary encryption function, which "encrypts" a string using a key. The resulting ciphertext can be fed through the same program to decrypt it.

早乙女 2024-09-15 13:36:24

在 C 和 C++ 中,字符串通常以 8 位字符值的形式存储在内存中,其中存储的值是 字符的 ASCII 值。

因此,您的代码对 ASCII 值进行异或。例如,输出中的第二个字符计算如下:

  'B' ^ ' '
= 66 ^ 32
= 01000010 ^ 00100000
= 01100010
= 98
= 'b'

如果您在使用 EBCDIC 而不是 ASCII。

In C and C++ strings are usually stored in memory as 8-bit char values where the value stored is the ASCII value of the character.

Your code is therefore XORing the ASCII values. For example, the second character in your output is calculated as follows:

  'B' ^ ' '
= 66 ^ 32
= 01000010 ^ 00100000
= 01100010
= 98
= 'b'

You could get a different result if you ran this code on a system which uses EBCDIC instead of ASCII.

兮颜 2024-09-15 13:36:24

字符的异或对两个字符(每个字节)的每个对应位执行异或运算。

The xor on characters performs the xor operation on each corresponding bit of the two characters (one byte each).

温柔一刀 2024-09-15 13:36:24

那么这是否意味着组成字符的字节正在被异或?

确切地。

这看起来像什么?

与任何其他异或一样:)。在 ASCII 中,“一只漂亮的猫”是(十六进制)

41 20 6E 69 63 65 20 63 61 74

和 ABCDEFGHIJ

41 42 43 44 45 46 47 48 49 4A

,因此,如果将每个字节相互异或,您会得到

00 62 2D 2D 26 23 67 2B 28 3E

,它是“\0b--&#g+(>”的十六进制表示形式,即 请注意,如果再次对结果文本进行异或,则会返回开始时

使用的文本;这就是在编码和加密中经常使用异或的原因。

So does this mean the byte which makes up the character is being XORed?

Exactly.

What does this look like?

As any other XOR :) . In ASCII "A nice cat" is (in hexadecimal)

41 20 6E 69 63 65 20 63 61 74

and ABCDEFGHIJ

41 42 43 44 45 46 47 48 49 4A

so, if you XOR each byte with each other, you get

00 62 2D 2D 26 23 67 2B 28 3E

, which is the hexadecimal representation of "\0b--&#g+(>", i.e. the string that is displayed when you run that code.

Notice that if you XOR again the resulting text you get back the text with which you started; this the reason why XOR is used often in encoding and cyphering.

冷清清 2024-09-15 13:36:24

这是一次一密加密的简单演示,正如您所看到的,它非常简单,而且恰好是唯一可证明牢不可破的加密形式。由于它是对称的并且具有与消息一样大的密钥,因此它通常不实用,但它仍然有许多有趣的应用程序..:-)

如果您还不熟悉它,需要注意的一件有趣的事情是密钥和密文之间的对称性。生成它们之后,就没有区别哪个是哪个,即哪个是先创建的,哪个是基于与另一个的明文异或的。除了基本加密之外,这还导致应用程序具有合理的推诿性。

This is a simple demonstration of one time pad encryption, which as you can see is quite simple and also happens to be the only provably unbreakable form of encryption. Due to it being symmetric and having a key as large as the message, it's often not practical, but it still has a number of interesting applications.. :-)

One fun thing to notice if you're not already familiar with it is the symmetry between the key and the ciphertext. After generating them, there's no distinction of which one is which, i.e. which one was created first and which was based on the plaintext xor'd with the other. Aside from basic encryption this also leads to applications in plausible deniability.

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