Skip to content

Latest commit

 

History

27 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Python Cryptography

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

Course Information

This project is based on material from the following course:


Repository Goals

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.

Repository Structure

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

1. Substitution Ciphers

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.

Files

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

2. Block Ciphers

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

Files

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

Confusion and Diffusion

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.


3. Stream Ciphers

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

Files

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

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.


Cryptographic Concepts Covered

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

Requirements

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 numpy

Getting Started

Clone the repository:

git clone https://github.com/wangjiacheng1219/python-cryptography.git
cd python-cryptography

Launch Jupyter Notebook:

jupyter notebook

You can then explore the notebooks and Python implementations in each cryptographic module.


Future Work

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

Disclaimer

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.


License

This project is released under the MIT License.

See the LICENSE file for details.


Acknowledgements

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.

About

A growing collection of Python implementations of cryptographic algorithms, cryptanalysis techniques, and related mathematical concepts.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages