arrow-left

All pages
gitbookPowered by GitBook
1 of 1

Loading...

The One Time Pad

Author: chuck_bartowski

hashtag
Introduction

The One Time Pad (OTP) is a well known example of encryption schemes that provide "perfect secrecy". Informally, this means that observing a ciphertext does no give any information to the eavesdropper. A proof of this fact will be provided later. Crucially we will assume that the sender and the receiver have both access to a common source of random bits.

hashtag
XOR as a One-Time Pad

XOR(addition modulo 2) can be used as an encryption scheme as follows: The message space is (i.e.: length n bit strings), the key space is and the ciphertext space is also

  • Encryption:

  • Decryption:

circle-check

The correctness of the schemes is easily verifiable. If the encryption produces , then the decryption produces .

circle-info

In the Python snippet below with use to os module to generate random bits.

As seen above the receiver with access to the same key can recover the message.

hashtag
Generalized One-Time Pad

Although XOR is commonly used for the OTP, OTP can be made out of more general objects. If fact We can define an OTP if the message and the keys are objects from a set with a group like structure. (see GroupTheorySection #TODO)

M⊆{0,1}n\mathcal M \subseteq \{0, 1\}^nM⊆{0,1}n
K={0,1}\mathcal K = \{0, 1\}K={0,1}
{0,1}\{0,1\}{0,1}
Enc(m,k)=m⊕k\text{Enc}(m,k) = m \oplus kEnc(m,k)=m⊕k
Dec(c,k)=c⊕k\text{Dec}(c,k) = c \oplus kDec(c,k)=c⊕k
c=m⊕kc = m \oplus kc=m⊕k
m′=c⊕k=m⊕k⊕k=mm' = c \oplus k = m \oplus k \oplus k = mm′=c⊕k=m⊕k⊕k=m
import os

def xor(a,b):
    res = bytes([x^y for (x,y) in zip(a,b)])
    return res
    
message = b"YELLOW SUBMARINE"
key = os.urandom(len(message))
ciphertext = xor(message, key)
recovered = xor(ciphertext, key)
print(f"Message: {message}\nKey: {key}\nCiphertext: {ciphertext}\nrecovered: {recovered}")
# A possible ouput might be as below
# Message: b'YELLOW SUBMARINE'
# Key: b'\x8e<3\xc9\x8d\xbaD\x16Zb\x1b\xbb\xb3\x0c@<'
# Ciphertext: b'\xd7y\x7f\x85\xc2\xeddE\x0f V\xfa\xe1E\x0ey'
# recovered: b'YELLOW SUBMARINE'