Skip to Main Content
Hands-On Cryptography with Python
book

Hands-On Cryptography with Python

by Samuel Bowne
June 2018
Beginner to intermediate content levelBeginner to intermediate
100 pages
2h 9m
English
Packt Publishing
Content preview from Hands-On Cryptography with Python

XOR

This section explains what XOR is on single bits with a truth table, and then shows how to do it on bytes. XOR undoes itself, so decryption is the same operation as encryption. You can use single bytes or multiple byte keys for XOR, and we will use looping to test keys. Here's the XOR truth table:

  • ^ 0 = 0
  • ^ 1 = 1
  • ^ 0 = 1
  • 1 ^ 1 = 0

If you feed in two bits and the two bits are the same, the answer is 0. If the bits are different, the answer is 1.

XOR operates on one bit at a time. Python indicates XOR with the ^ operator.

The truth table shows how it works. You feed in bits that are equally likely to be 0 and 1 and XOR them together, then you end up with 50% ones and zeros, which means that XOR does not destroy any information. ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Practical Cryptography in Python: Learning Correct Cryptography by Example

Practical Cryptography in Python: Learning Correct Cryptography by Example

Seth James Nielson, Christopher K. Monson
Cryptography Algorithms

Cryptography Algorithms

Massimo Bertaccini

Publisher Resources

ISBN: 9781789534443Supplemental Content