Why is Exclusive Or (XOR) Important in Cryptography?

栏目: IT技术 · 发布时间: 4年前

内容简介:If you are getting into cryptography, or just trying to understand the fundamentals, you may have noticed that the exclusive-or operation is used quite often, especially in ciphers.XOR, or “exclusive or” operates on binary data. It returns true if both of
Why is Exclusive Or (XOR) Important in Cryptography?

If you are getting into cryptography, or just trying to understand the fundamentals, you may have noticed that the exclusive-or operation is used quite often, especially in ciphers.

What is XOR (⊕)?

XOR, or “exclusive or” operates on binary data. It returns true if both of its inputs are opposites (one false and one true), otherwise, it returns false.

Why is Exclusive Or (XOR) Important in Cryptography?

An example in go code would be something like:

func exclusiveOr(a bool, b bool) bool {
	return a != b
}

XOR Cipher – The Perfect Cipher

The XOR operation can be used as a simple cipher for encrypting and decrypting messages with a single key. This is known as symmetric encryption.

It is interesting to note that if:

  1. The key is the same size as the message
  2. The key is kept secret and generated truly randomly

Then the cipher is impossible to crack. This is known as a one time pad . However, a simple XOR shouldn’t be used in production due to the key length needing to be too long to be practical.

Cipher Example

As a simple example, let’s encrypt the word “hi”

1. Convert “hi” to binary, here is a free tool: https://www.rapidtables.com/convert/number/ascii-to-binary.html )

2. Create a random secret key that has the same length:

3. Create an encrypted message by XOR’ing the message and the key:

01101000 01101001 ("hi")
XOR
01010010 01000101 (secret key)
=
00111010 00101100 (encrypted message)

4. Decrypt the message by XOR’ing the key with the encrypted message again:

00111010 00101100 (encrypted message)
XOR
01010010 01000101 (secret key)
=
01101000 01101001 ("hi")

Why does it work?

XOR works as a cipher because it is its own inverse.

= ( )

And, as we demonstrated in our example:

encrypted = message key

and

message = encrypted key

Is XOR used in production ciphers?

The simple XOR cipher isn’t used in production because it is impractical to use keys that are the same length as the message body. However, the XOR is still extremely useful. In fact, it is used in almost all symmetric encryption algorithms. XOR is the primary operation in the “add round key” step of AES-256 . It is also used in the DES cipher.

Thanks For Reading

Lane on Twitter: @wagslane

Lane on Dev.to: wagslane

Download Qvault:https://qvault.io


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

领域驱动设计

领域驱动设计

埃文斯 / 赵俐、盛海艳、刘霞 / 人民邮电出版社 / 2010-11 / 69.00元

《领域驱动设计:软件核心复杂性应对之道》是领域驱动设计方面的经典之作。全书围绕着设计和开发实践,结合若干真实的项目案例,向读者阐述如何在真实的软件开发中应用领域驱动设计。书中给出了领域驱动设计的系统化方法,并将人们普遍接受的一些最佳实践综合到一起,融入了作者的见解和经验,展现了一些可扩展的设计最佳实践、已验证过的技术以及便于应对复杂领域的软件项目开发的基本原则。《领域驱动设计:软件核心复杂性应对之......一起来看看 《领域驱动设计》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具