Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
344 views
in Technique[技术] by (71.8m points)

c++ - C ++中的XOR加密(XOR Encryption in C++)

I am trying to implement a simple XOR Encryption using a number, of type long .

(我正在尝试使用long类型的数字实现简单的XOR加密。)

Unfortunately, some of the numbers return some symbols that decrypting them gives a whole different text than the one entered.

(不幸的是,一些数字返回了一些符号,这些符号解密后得到的文本与输入的文本完全不同。)

For example, encrypting a text 'hello world' with the key '5105105105' returns this ' ?? ' .

(例如,使用密钥'5105105105'加密文本'hello world'将返回此' ?? ' 。)

To decrypt it back with the same key, it returns this '>nl wor>nl' .

(要使用相同的密钥将其解密,它将返回此'>nl wor>nl' 。)

I am not sure why is it doing this.

(我不确定为什么要这样做。)

This is my code:

(这是我的代码:)

string encDec(string str, long key) {
    string output = str;

    for (int i = 0; i < str.size(); i++)
        output[i] = str[i] ^ key;

    return output;
}

What am I doing wrong here?

(我在这里做错了什么?)

  ask by Zaki Sediqyar translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Let's look at: output[i] = str[i] ^ key;

(让我们看一下: output[i] = str[i] ^ key;)

What this does is that it first retrieves a character from the string str at index i .

(这是因为它首先从索引为i的字符串str中检索一个字符。)

A character in std::string is equivalent to an 8 bit byte.

(std::string等效于8位字节。)

Then this byte is being coaxed into a long using an implicit widening conversion to match the left key .

(然后这个字节被哄骗入一个long使用隐式加宽转换以匹配左key 。)

After that all the bits are XOR'ed with the key, performing the "encryption".

(之后,将所有位与密钥进行异或运算,执行“加密”。)

I've put this in quotes because repeated use of the key with a XOR cipher is of course not secure.

(我将其用引号引起来,因为用XOR密码重复使用密钥当然是不安全的。)

Finally an implicit narrowing conversion is used to assign this long value to the char array again.

(最后,使用隐式缩小转换将此long值再次分配给char数组。)

In other words, you can see this as performing: output[i] = (char) ((long) str[i] ^ key; if you make the conversions explicit. Note that many bits are first XOR'ed, and then thrown away during the narrowing conversion; probably not what you want as those key bits are basically ignored.

(换句话说,您可以看到它正在执行: output[i] = (char) ((long) str[i] ^ key;如果您将转换明确化,请注意,许多位首先被XOR运算,然后抛出在缩小转换过程中消失;可能不是您想要的,因为那些关键位基本上被忽略了。)


Of course if you XOR with random bits then what you get in return is a random binary pattern.

(当然,如果您对随机位进行XOR,那么您得到的是随机二进制模式。)

This kind of pattern may not consist of printable characters.

(这种样式可能不包含可打印字符。)

You either have to store or send those as binary values, or you must first convert them back to printable text.

(您要么必须将它们存储或发送为二进制值,要么必须首先将它们转换回可打印的文本。)

Usually we do that using hex or base 64 encoding of the bytes.

(通常,我们使用十六进制或字节的基数64编码来执行此操作。)

Modern cryptography operates on bits and bytes.

(现代密码学对位和字节进行操作。)

Classical ciphers such as Caesar and Vigenere ciphers do operate on text and gave text as output.

(凯撒(Caesar)和维吉涅(Vigenere)等经典密码确实对文本起作用,并将文本作为输出。)

In the end, XOR is an operation on bits, not on characters of a text.

(最后,XOR是对位的操作,而不是对文本字符的操作。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...