This repository contains a step-by-step Jupyter Notebook demonstrating the One-Time Pad (OTP) encryption algorithm. OTP is a classical cryptography technique that, when implemented correctly, provides perfect secrecy (information-theoretic security) and cannot be cracked, even with infinite computing power.
This notebook is structured for educational purposes, breaking down the encryption process into easily digestible chunks.
The notebook covers two main approaches to OTP encryption:
- Custom Byte-wise Implementation: Demonstrates the core mathematics of OTP using the bitwise XOR (
^) operation and cryptographically secure random keys generated viaos.urandom(). - Using External Libraries: Shows a quick, practical implementation using the
onetimepadPython library for rapid prototyping.
To run this notebook, you will need Jupyter installed on your system.
-
Clone the repository:
-
Install the required external library for the second section of the notebook:
pip install onetimepad
-
Launch Jupyter Notebook:
jupyter notebook
Open the .ipynb file and run the cells sequentially.
The core of OTP relies on the XOR operation:
C = P ⊕ K
Where C is the Ciphertext, P is the Plaintext, and K is the Key.
For the encryption to be truly unbreakable, the key must follow three absolute rules:
- It must be truly random (or generated by a Cryptographically Secure Pseudo-Random Number Generator like
os.urandom()). - It must be at least as long as the plaintext.
- It must never be reused in whole or in part.