# Cryptanalysis with N-Grams

DevFeed: [Cryptanalysis with N-Grams](<https://devfeed.tech/articles/cryptanalysis-with-n-grams-40260.md>)

Original publisher: [Read original article](<https://www.jeremykun.com/2012/02/03/cryptanalysis-with-n-grams/>)

Published: 2012-02-03T13:52:16Z

Content type: tutorial

Language: en

Sources: [Jeremy Kun](<https://devfeed.tech/sources/jeremy-kun.md>)

Topics: [Computing](<https://devfeed.tech/topics/computing.md>), [Cryptography](<https://devfeed.tech/topics/cryptography.md>), [Decoding](<https://devfeed.tech/topics/decoding.md>), [Algorithm](<https://devfeed.tech/topics/algorithm.md>), [Python](<https://devfeed.tech/topics/python.md>), [Encryption](<https://devfeed.tech/topics/encryption.md>)

Tags: [algorithm](<https://devfeed.tech/tags/algorithm.md>), [code](<https://devfeed.tech/tags/code.md>), [computing](<https://devfeed.tech/tags/computing.md>), [cryptanalysis](<https://devfeed.tech/tags/cryptanalysis.md>), [decoding](<https://devfeed.tech/tags/decoding.md>), [linguistics](<https://devfeed.tech/tags/linguistics.md>), [mathematics](<https://devfeed.tech/tags/mathematics.md>), [natural-language-data](<https://devfeed.tech/tags/natural-language-data.md>), [ngrams](<https://devfeed.tech/tags/ngrams.md>), [programming](<https://devfeed.tech/tags/programming.md>), [python](<https://devfeed.tech/tags/python.md>), [substitution-cipher](<https://devfeed.tech/tags/substitution-cipher.md>), [techniques](<https://devfeed.tech/tags/techniques.md>)

## AI overview

This tutorial explains how to decode substitution ciphers using n-gram-based word segmentation and a local-search algorithm over possible decryptions. It introduces the relevant concepts, implements the approach in Python, and tests it on historical substitution ciphers.

## Source excerpt

This post is the third post in a series on computing with natural language data sets. For the first two posts, see the relevant section of our main content page. A Childish Bit of Fun In this post, we focus on the problem of decoding substitution ciphers. First, we'll describe a few techniques humans use to crack ciphers. We'll find these unsatisfactory, and move on to a simplistic algorithm which does a local search on the space of all possible decryptions, where we utilize our word segmentation algorithm from last time to determine the likelihood that a decryption is correct.