mdk.fr/content/pages/supinternet_python_uncipher.md
2018-11-27 11:52:59 +01:00

1.3 KiB

status: hidden title: Python — Uncipher slug: aena5lohSh5ri8-uncipher robots: noindex

Break a substitution cipher

Introduction

The project name is: uncipher.

You'll give back your project using the rendu protocol.

Project

Make a program taking a string, and optionally a dictionary file, like:

python3 subst_break.py --dict /usr/share/dict/french 'Cpokpvs mft hfot'

Your program should try every possible alphabetic substitution (in the alphabetic or ASCII range, maybe try different alphabets if needed), rank them by probability of being a real clear text (by counting the words being in the dictionary, or by doing frequency analysis on the letters, whatever...).

Your program should then print the most probable one (should easily be the right one in your tests).

You can write your own implementation of a substitution cipher for tests purpose in the ASCII range like this:

''.join([chr(ord(x) + 1) for x in "Bonjour les gens"])

(beware, I miss a modulo 255, correct implementation left as an exercise).

Or in a given alphabet like this:

import string

alphabet = string.ascii_lowercase * 2
    for shift in range(1, 10):
            print(''.join([alphabet[alphabet.index(x) + shift] if x in alphabet else x for x in "bonjour les gens"]))