Updated · By Pete Bromfield, former Head of Maths with IB examining experience

IA idea · Cryptography & number theory

Breaking a cipher with letter frequencies and chi-squared

AI SLAA SLAI HLAA HL Accessible Also in: Statistics

Research question

How short can a message be before frequency analysis stops breaking a Caesar or affine cipher reliably, using a chi-squared statistic to score each possible key?

Make it yours, then agree it with your teacher.

Next step: plan this IA free — the planner opens with this question filled in. For feedback on your own draft, the IA package (€39 once) gives you 5 AI feedback credits and every annotated exemplar in full.

Why it makes a good exploration

Frequency analysis is a real attack with a real statistical core. Measuring how its success depends on message length turns a code-breaking game into a quantitative investigation.

The mathematics you'll need

  • Letter frequencies from a large reference text
  • Chi-squared statistic as a goodness-of-fit score
  • Affine functions mod 26 and which keys are valid (counting)
  • Proportion of successes against message length
  • Fitting a model to the success curve

Where the data comes from

Build reference frequencies from a public-domain book on Project Gutenberg; test on messages of different lengths from other texts.

  • Project Gutenberg — 70,000+ free public-domain books as plain text — ideal for letter and word frequency counts.
  • Desmos graphing calculator — Free graphing and regression (y₁ ~ ax₁ + b) — fit models to your data and show residuals.

Real data for this idea

  1. Primary source: Project Gutenberg: Free ebooks. Download two or three books as “Plain Text UTF-8” by hand and remove the licence header before counting. Licence: Public domain in the US; download a few books by hand (the site blocks bulk robots).

Links checked October 2026; we link to the data and never copy it. More free data sets.

A possible outline

  1. Explain the cipher and count the valid keys.
  2. Build reference frequencies.
  3. Score every key with chi-squared and pick the best.
  4. Test many messages of each length and record the success rate.
  5. Model the success rate and reflect on what makes short messages hard.

Pitfalls that cost marks

  • Using chi-squared as a test with a p-value when it is really being used as a score; say which.
  • Testing on the same text you built frequencies from.
  • Too few messages per length.

Showing personal engagement

  • Encrypt messages for friends and break theirs.
  • Compare languages you speak.
  • Find the shortest message you can still break.

See Criterion C: personal engagement for what examiners look for.

Which course is it for?

CourseFitMaths to lean on
AA SLGood fitLetter frequencies from a large reference text; Chi-squared statistic as a goodness-of-fit score
AA HLFits, but add an HL techniqueLetter frequencies from a large reference text; Chi-squared statistic as a goodness-of-fit score
AI SLGood fitLetter frequencies from a large reference text; Chi-squared statistic as a goodness-of-fit score
AI HLFits, but add an HL techniqueLetter frequencies from a large reference text; Chi-squared statistic as a goodness-of-fit score

Level: Accessible. A good first extended piece of maths, with room to go deeper. See how the IA differs between AA and AI, SL and HL.

How this idea reaches the top bands

Reflection (D)

For this idea, start with the weak points: using chi-squared as a test with a p-value when it is really being used as a score; say which; and testing on the same text you built frequencies from. Say how each one affects your answer.

Use of mathematics (E)

SL: letter frequencies from a large reference text; chi-squared statistic as a goodness-of-fit score; affine functions mod 26 and which keys are valid (counting); proportion of successes against message length; fitting a model to the success curve, each explained in your own words and carried out correctly.

How cryptography explorations reach the top bands in general: Cryptography & number theory IA ideas. Guides: A · B · C · D · E · self-grade sheet.

Taking it further

Attack a Vigenère cipher by splitting it into Caesar ciphers, or compare chi-squared with another score.

Extending it for HL

Add an HL technique the question genuinely needs: what an HL cryptography exploration adds.

Frequently asked questions

Which IB Maths course is this IA idea for?

It works for AI SL, AA SL, AI HL and AA HL. We rate it Accessible: a good first extended piece of maths, with room to go deeper.

What mathematics does this IA use?

Letter frequencies from a large reference text, chi-squared statistic as a goodness-of-fit score, affine functions mod 26 and which keys are valid (counting), proportion of successes against message length and fitting a model to the success curve.

Where can I get data for this IA?

Build reference frequencies from a public-domain book on Project Gutenberg; test on messages of different lengths from other texts. Checked sources and tools: Project Gutenberg and Desmos graphing calculator.

Turn this idea into your IA

The planner opens with “Breaking a cipher with letter frequencies and chi-squared” filled in: the question, the maths and the data sources.

Plan this IA free

Your plan is kept in this browser. A free account keeps it with your drafts on any device: Join free · Sign in

Also: free IA self-check · A–E checklist (PDF) · have a draft? AI feedback or a review with Pete (€100)

Writing it up: how to write a Maths IA, step by step · structure and format · mistakes that cost marks · annotated exemplars, marked on every criterion

Similar ideas

All cryptography ideas · AI SL ideas · AA SL ideas · AI HL ideas · AA HL ideas · All 239 IA ideas