Text encryption, decryption, frequency analysis, and shift detection in Python.
The project reads text from .txt files, writes results to .txt files, uses a custom alphabet consisting of letters + digits, and leaves Polish diacritics / special characters unchanged.
This project focuses on:
- encrypting text with a Caesar-style shift cipher,
- decrypting text using a selected shift,
- analyzing character frequencies for the custom alphabet,
- finding the most likely shift for an encrypted text using a reference text.
The cipher uses the following alphabet:
abcdefghijklmnopqrstuvwxyz0123456789
The program transforms:
- lowercase English letters
a-z - digits
0-9
The following characters remain unchanged:
- Polish letters:
ą ć ę ł ń ó ś ź ż - spaces
- punctuation
- line breaks
- all other characters outside the custom alphabet
Uppercase and lowercase letters are treated as the same during processing.
The output is saved in lowercase.
- file-based encryption and decryption,
- frequency analysis of letters and digits,
- shift detection based on frequency comparison,
- support for both:
- English version of the script,
- Polish version of the script.
.
├─ maineng.py
├─ mainpl.py
├─ README.md
├─ LICENSE
├─ .gitignore
├─ input.txt
├─ output.txt
├─ reference.txt
├─ encrypted.txt
├─ decrypted.txt
├─ analysis.txt
└─ ranking.txt
Run the English version:
python maineng.pyRun the Polish version:
python mainpl.pyThe program provides three main functions:
- reads text from a
.txtfile, - encrypts or decrypts it using a given shift,
- saves the result to another
.txtfile.
- counts how many times each character from the custom alphabet appears,
- calculates the percentage share of each character,
- ignores spaces, punctuation, and other unsupported symbols.
- compares an encrypted text with a reference text,
- checks all possible shifts,
- selects the shift with the smallest frequency difference,
- saves the decrypted result and ranking to files.
Suggested example files for testing:
input.txt— plain text to encryptreference.txt— reference text for frequency comparisonencrypted.txt— encrypted textdecrypted.txt— recovered plaintextanalysis.txt— frequency reportranking.txt— list of shifts sorted by similarity
- This project implements a Caesar / shift cipher, not a transposition cipher.
- Characters outside the custom alphabet are preserved exactly as they appear.
- The project is intended as an educational Python exercise for text processing, file handling, and basic cryptography concepts.
Created by Avuii