A collection of Python implementations of classical and modern cryptographic algorithms, cryptanalysis techniques, and related mathematical concepts.
This repository was originally developed as part of the Elements of Applied Data Security M course at the University of Bologna. The original coursework was completed as a team project. The version published here has since been reorganized, refined, and documented by Jiacheng Wang, with further extensions and experiments added over time.
The repository currently covers three main areas:
- Substitution ciphers and classical cryptanalysis
- Block ciphers, including AES and the principles of confusion and diffusion
- Stream ciphers, including LFSRs, bit generators, and sequence-based cryptanalysis
This project is based on material from the following course:
- Course: 90392 – Elements of Applied Data Security M
- Instructor: Dr. Alex Marchioni
- Institution: Alma Mater Studiorum – University of Bologna
- Official Course Repository: marchioa/security
The main goals of this repository are to:
- Implement cryptographic algorithms in Python.
- Explore practical cryptanalysis techniques.
- Connect cryptographic algorithms with their underlying mathematical concepts.
- Experiment with classical, block, and stream cipher constructions.
- Provide reproducible examples using Python and Jupyter notebooks.
- Extend the original coursework with additional implementations and experiments.
python-cryptography/
│
├── README.md
├── LICENSE
├── .gitignore
│
├── Substitution_Ciphers/
│ ├── Substitution_Ciphers.ipynb
│ ├── ciphertext_affine.txt
│ ├── ciphertext_caesar.txt
│ ├── ciphertext_simple.txt
│ ├── distribution.pkl
│ └── wikipedia_cybersecurity.txt
│
├── block cipher/
│ ├── aes.py
│ ├── block cipher.ipynb
│ ├── block_cipher.py
│ └── diffusion_confusion.py
│
└── stream cipher/
├── binary_sequence.bin
├── bitgenerator.py
├── bits.py
├── ciphertext.bin
├── known-plaintext.txt
├── lfsr.py
├── primitive_polynomials.py
└── stream cipher.ipynb
The Substitution_Ciphers directory contains implementations and experiments related to classical substitution ciphers and cryptanalysis.
Topics include:
- Caesar cipher
- Affine cipher
- Simple substitution ciphers
- Character-frequency analysis
- Ciphertext analysis
- Statistical properties of natural-language text
The Jupyter notebook provides the main implementations and experiments, while the accompanying ciphertext and data files are used for encryption and cryptanalysis exercises.
| File | Description |
|---|---|
Substitution_Ciphers.ipynb |
Main notebook containing substitution-cipher implementations and experiments |
ciphertext_affine.txt |
Ciphertext generated using an affine cipher |
ciphertext_caesar.txt |
Ciphertext generated using a Caesar cipher |
ciphertext_simple.txt |
Ciphertext for simple substitution analysis |
distribution.pkl |
Stored frequency-distribution data |
wikipedia_cybersecurity.txt |
Sample English text used for frequency analysis and experiments |
The block cipher directory explores the fundamental principles and implementations of block cryptography.
Topics include:
- Block cipher construction
- Advanced Encryption Standard (AES)
- Encryption and decryption operations
- Confusion and diffusion
- Cryptographic transformations
- Practical implementation of block-based encryption in Python
| File | Description |
|---|---|
aes.py |
Python implementation and utilities related to AES |
block cipher.ipynb |
Jupyter notebook containing block-cipher experiments |
block_cipher.py |
Implementation of block-cipher concepts and operations |
diffusion_confusion.py |
Experiments illustrating the principles of diffusion and confusion |
Two important principles in modern symmetric cryptography are confusion and diffusion.
Confusion aims to make the relationship between the encryption key and ciphertext as complex as possible.
Diffusion spreads the influence of individual plaintext bits across many ciphertext bits, reducing visible statistical relationships between the plaintext and ciphertext.
These concepts are fundamental to the design of secure block ciphers such as AES.
The stream cipher directory focuses on stream-cipher constructions and the generation and analysis of binary sequences.
Topics include:
- Linear Feedback Shift Registers (LFSRs)
- Pseudorandom binary sequences
- Primitive polynomials
- Bit generators
- Stream encryption
- Known-plaintext analysis
- Sequence reconstruction and cryptanalysis
| File | Description |
|---|---|
stream cipher.ipynb |
Notebook containing stream-cipher and cryptanalysis experiments |
binary_sequence.bin |
Binary sequence used in stream-cipher experiments |
bitgenerator.py |
Utilities for generating bit sequences |
bits.py |
Bit-level operations and helper functions |
ciphertext.bin |
Binary ciphertext used for cryptanalysis |
known-plaintext.txt |
Known plaintext used in cryptanalytic experiments |
lfsr.py |
Implementation of Linear Feedback Shift Registers |
primitive_polynomials.py |
Utilities related to primitive polynomials and LFSR construction |
Linear Feedback Shift Registers are simple and efficient mechanisms for generating deterministic binary sequences.
An LFSR updates its internal state using a linear recurrence over the finite field GF(2). With an appropriate primitive polynomial, an (n)-bit LFSR can generate a maximal-length sequence with period
[ 2^n - 1. ]
Although LFSRs have useful mathematical and engineering properties, a single LFSR is generally not cryptographically secure because its linear structure can be reconstructed from a sufficiently long observed sequence.
This makes LFSRs particularly useful for studying both stream-cipher construction and cryptanalysis.
The repository provides practical examples of several important cryptographic concepts:
| Area | Concepts |
|---|---|
| Classical Cryptography | Caesar cipher, affine cipher, substitution cipher |
| Cryptanalysis | Frequency analysis, known-plaintext analysis |
| Block Cryptography | AES, block transformations |
| Cipher Design | Confusion, diffusion |
| Stream Cryptography | LFSR, binary sequences, keystream generation |
| Mathematics | Modular arithmetic, finite fields, primitive polynomials |
| Implementation | Python, Jupyter Notebook, binary data processing |
The project is primarily implemented in Python 3 and uses Jupyter Notebook for interactive experiments.
A typical environment requires:
Python 3
Jupyter Notebook
NumPy
Additional dependencies may be required by individual notebooks or scripts.
To install the common dependencies:
pip install jupyter numpyClone the repository:
git clone https://github.com/wangjiacheng1219/python-cryptography.git
cd python-cryptographyLaunch Jupyter Notebook:
jupyter notebookYou can then explore the notebooks and Python implementations in each cryptographic module.
Possible future extensions include:
- Additional classical cipher implementations
- More detailed AES experiments
- Additional block-cipher modes of operation
- More advanced stream-cipher constructions
- Statistical randomness testing
- Additional LFSR cryptanalysis techniques
- Modern authenticated-encryption algorithms
- Performance comparisons between cryptographic implementations
- Hardware-oriented implementations of cryptographic primitives
This repository is intended primarily for educational and research purposes.
The implementations are designed to illustrate cryptographic algorithms, mathematical principles, and cryptanalysis techniques. They should not be considered production-ready cryptographic software.
For security-critical applications, use well-established and professionally audited cryptographic libraries.
This project is released under the MIT License.
See the LICENSE file for details.
The original coursework was developed as part of the Elements of Applied Data Security M course at the University of Bologna.
Thanks to Dr. Alex Marchioni for the course materials and instruction, and to the original project collaborators who contributed to the coursework on which parts of this repository are based.