## Monday, February 27, 2012

### How to break the 'rapper code'

The Mirror has a story about a code sent by a prisoner that revealed a bribery plot. The code was apparently broken by a professional code breaker. Looking at the picture in the article I figured I'd have a go.

It's actually a pretty easy code. The first step was to transcribe the code so I could play with it and then do a simple frequency analysis. Lo and behold it looks like it's a substitution cipher. The top six most frequent numbers are:
```Number   Frequency
10       0.101243339253996
5        0.0959147424511545
27       0.0852575488454707
8        0.0746003552397869
3        0.0728241563055062
16       0.0603907637655417
25       0.0532859680284192
```
and there's a steep drop off that looks just like the pattern you'd see in English. So those first few numbers likely match the most common letters in English: ETAION. So a quick substitution in that order reveals:
```... ... ..NO .O. IE . .. .N..T ..TEIT
..NA ..I. I... AE.T . .E.O . E .TT. .E AEI.
OI A. .T ....OIAT. .. EN. .TENI NT.T.IIE..
..TEIT AEI. IAEAT.TNA .... I.E.E A...... E
ON.T.TN.ENA I..O.OA.. IE.ON A.T ...O.T .E.T
.O. .O.. .T ..A ON .E O. .E.E.T .. .O.ON..O.
E IO.NE. OT IAEN.ON. .. .AE.ON. .O. ... .TN
...O. EE.T .. A. E..OT.T A.OI AEI.. ..IA .ON.
A.O. . .O. ... AE.. . .E.I . ..IA.. . . .TN..
EA .OT .E....A.T.I .E..... ..IA .O.. .. .E.
. .TTA.O. EI E .EIA .TI..A O .E.T E..TE..
.E.T ..NAE.A .OA. I.E.EI ..IITI .E ....ONI
.E.O.. ..NT.I .. .E ..N. .T.I IA.O. ....
ON ..ONEA..N A.T. .ENA .O.ATTN . ... .E
IAEAT.TNA .T.AO.OT. ..A A.T. .E.T A. .TA..N
.. .T.I . ...NTI A. I... .... .EOA..T...T EN.
.E..TNA
```
Since the commas in the original text look like they might be word breaks I've added spaces where they were and I've output a . when I don't know the substitution.

Doesn't look very revealing so I tried permuting the first few numbers so that 5 = E and 10 = A to get:
```... ... ..NO .O. IA . .. .N..E ..EAIE
..NT ..I. I... TA.E . .A.O . A .EE. .A TAI.
OI T. .E ....OITE. .. AN. .EANI NE.E.IIA..
..EAIE TAI. ITATE.ENT .... I.A.A T......
A ON.E.EN.ANT  I..O.OT.. IA.ON T.E ...O.E
.A.E .O. .O.. .E ..T ON .A O. .A.A.E ..
.O.ON..O. A IO.NA. OE  ITAN.ON. .. .TA.ON.
.O. ... .EN ...O. AA.E .. T. A..OE.E T.OI
TAI.. ..IT .ON. T.O. . .O. ... TA.. . .A.I
. ..IT.. . . .EN.. AT .OE .A....T.E.I
.A..... ..IT .O.. .. .A. . .EET.O.
AI A .AIT .EI..T O .A.E A..EA.. .A.E ..NTA.T
.OT. I.A.AI ..IIEI .A ....ONI .A.O.. ..NE.I
.. .A ..N. .E.I IT.O. .... ON ..ONAT..N T.E.
.ANT .O.TEEN . ... .A ITATE.ENT .E.TO.OE.
..T T.E. .A.E T. .ET..N .. .E.I . ...NEI T.
I... .... .AOT..E...E AN. .A..ENT
```
The word ITATE.ENT really stands out. Could it be STATEMENT? Updating the number to letter mapping reveals:
```... ..M M.NO .O. SA . M. .N..E ..EASE
..NT ..S. S... TA.E . .A.O . A .EE. .A TAS.
OS T. .E ..M.OSTE. .. AN. MEANS NE.E.SSA..
..EASE TAS. STATEMENT ...M S.A.A T...... A
ON.E.EN.ANT S..O.OT.. SA.ON T.E ...O.E MA.E
.OM .O.. ME ..T ON .A O. .A.A.E .. .O.ON..OM
A SO.NA. OE STAN.ON. .. .TA.ON. .O. ... .EN
M..O. AAME .. T. A..OE.E T.OS TAS.. M.ST .ON.
T.O. . .O. .M. TA.. . .A.S . M.ST.. . . .EN..
AT .OE .A..M.T.E.S .A..... M.ST .O.. M. .A. .
MEET.OM AS A .AST .ES..T O .A.E A..EA.. MA.E
..NTA.T .OT. S.A.AS ..SSES .A M...ONS .AMO..
..NE.S .. .A ..N. .E.S ST.O. .... ON ..ONAT..N
T.E. .ANT .O.TEEN . ... .A STATEMENT .E.TO.OE.
..T T.E. .A.E T. .ET..N M. .E.S . ...NES T.
S... .... .AOT..E...E AN. .A.MENT
```
Now MEANS NE.E.SSA.. stands out. So substitute the right numbers to get:
```... C.M M.NO .O. SA . MY .NC.E ..EASE
..NT R.S. S... TA.E . .AYO . A .EE. .A TAS.
OS T. .E C.M.OSTE. .Y ANY MEANS NECERSSARY
..EASE TAS. STATEMENT .R.M S.A.A T.R.... A
ON.E.EN.ANT S..OCOT.R SAYON T.E ...OCE MA.E
.OM .O.. ME ..T ON .A O. .ARA.E .Y .O.ON..OM
A SO.NA. OE STAN.ON. .. .TA.ON. .O. ... .EN
MY.OC AAME .. T. AC.OE.E T.OS TAS.. M.ST .ON.
T.O. . .O. .M. TA.. . .ARS . M.ST.. . C .EN.Y
AT .OE .A..M.T.ERS YAR.Y.. M.ST RO.. MY .A.
. MEET.OM AS A .AST RES.RT O .A.E A.REA.Y MA.E
C.NTACT .OT. S.A.AS ..SSES .A M...ONS .AMO.Y
..NERS .. .A ..N. .E.S STRO. .... ON C.ONAT..N
T.EY .ANT .O.TEEN . ..R .A STATEMENT CERTO.OE.
..T T.EY .A.E T. RET.RN M. .EYS . ...NES T.
S... .... .AOT..E..RE ANY .AYMENT
```
A few things stand out here: "ANY .AYMENT", "A.REA.Y MA.E C.NTACT", ".AST RES.RT" and also the ".Y" before "ANY MEANS NECERSSARY". Substituting those characters we get:
```.O. C.M M.NI .ID SA . MY .NCLE PLEASE
DONT R.S. S..D TA.E . DAYI . A .EE. DA TAS.
IS TO BE COMPISTED BY ANY MEANS NECERSSARY
PLEASE TAS. STATEMENT .ROM S.A.A T.RO.P.
A INDEPENDANT SOLICITOR SAYIN T.E POLICE
PI.INP.IM A SIPNAL IE STANDINP .P .TA.INP
LID O.. PEN MYPIC AAME .P TO AC.IE.E T.IS
TAS.. M.ST LIN. TBIP . .ID .M. TAL. . PARS .
M.STPO . C BENPY AT .IE BAB.MOT.ERS YARDYO.
M.ST RI.P MY DAD . MEET.IM AS A LAST RESORT
DA M.LLINS .AMILY O.NERS O. DA LONP LEPS STRIP
.L.B IN C.INATO.N T.EY .ANT .I.TEEN . .OR DA
STATEMENT CERTI.IED B.T T.EY .A.E TO RET.RN
M. .EYS . P.ONES TO S.O. POOD .AIT.BE.ORE ANY
PAYMENT
```
The rest is left as an exercise for the reader.

The only surprising thing here is the comment from the 'expert':
Dr Olsson, who has worked on hundreds of cases for police around the world, told the trial: ‘The thought process behind the code shows someone who is very able, very intelligent, very skillful.’
Really? Looks like someone who'd read the simplest childhood book on codes to me. Not saying he's an idiot, but hardly very skillful code making.

PS Some people have been asking about the code I wrote to do this. Here's my little program:
```use strict;
use warnings;

my @code = ( [qw/6 25 4/], [qw/30 40 13/], [qw/13 12 16 8/], [qw/X 8
11/], [qw/3 10/], [qw/1/], [qw/13 43/], [qw/40 16 30 9 5/], [qw/_ 9 5
10 3 5/], [qw/11 25 16 27/], [qw/20 40 3 4/], [qw/3 4 40 11/], [qw/27
10 6 5/], [qw/2/], [qw/11 10 43 8/], [qw/2/], [qw/10/], [qw/X 5 5 6/],
[qw/11 10/], [qw/27 10 3 6/], [qw/8 3/], [qw/27 25/], [qw/19 5/],
[qw/30 25 13 _ 8 3 27 5 11/], [qw/19 43/], [qw/10 16 43/], [qw/13 5 10
16 3/], [qw/16 5 30 5 20 3 3 10 20 43/], [qw/_ 9 5 10 3 5/], [qw/27 10
3 6/], [qw/3 27 10 27 5 13 5 16 27/], [qw/24 20 25 13/], [qw/3 4 10 6
10/], [qw/27 4 20 25 40 _ 4/], [qw/10/], [qw/8 16 11 5 _ 5 16 11 10 16
27/], [qw/3 25 9 8 30 8 27 25 20/], [qw/3 10 43 8 16/], [qw/27 4 5/],
[qw/_ 25 9 8 30 5/], [qw/13 10 11 5/], [qw/4 8 13/], [qw/_ 8 80 6/],
[qw/13 5/], [qw/25 40 27/], [qw/8 16/], [qw/11 10/], [qw/8 11/], [qw/_
10 20 10 11 5/], [qw/19 43/], [qw/_ 8 42 8 16 _ 4 8 13/], [qw/10/],
[qw/3 8 _ 16 10 9/], [qw/8 5/], [qw/3 27 10 16 11 8 16 _/], [qw/40
_/], [qw/1 27 10 6 8 16 _/], [qw/9 8 11/], [qw/25 24 24/], [qw/_ 5
16/], [qw/13 43 _ 8 30/], [qw/10 10 13 5/], [qw/40 _/], [qw/27 25/],
[qw/10 30 4 8 5 42 5/], [qw/27 4 8 3/], [qw/27 10 3 6 40/], [qw/13 40
3 27/], [qw/9 8 16 6/], [qw/27 19 8 _/], [qw/1/], [qw/X 8 11/], [qw/48
13 2/], [qw/27 10 9 6/], [qw/2/], [qw/_ 10 20 3/], [qw/40/], [qw/13 40
3 27 _ 25/], [qw/1/], [qw/30/], [qw/19 5 16 _ 43/], [qw/10 27/], [qw/4
8 5/], [qw/19 10 19 40 13 25 27 4 5 20 3/], [qw/43 10 20 11 43 25
40/], [qw/13 40 3 27/], [qw/20 8 17 _/], [qw/13 43/], [qw/11 10 11/],
[qw/1/], [qw/13 5 5 27 4 8 13/], [qw/10 3/], [qw/10/], [qw/9 10 3
27/], [qw/20 5 3 25 20 27/], [qw/8/], [qw/4 10 42 5/], [qw/10 9 20 5
10 11 43/], [qw/13 10 11 5/], [qw/30 25 16 27 10 30 27/], [qw/X 8 27
4/], [qw/3 4 10 6 10 3/], [qw/19 25 3 3 5 3/], [qw/11 10/], [qw/13 40
9 9 8 16 3/], [qw/24 10 13 8 9 43/], [qw/25 X 16 5 20 3/], [qw/25
24/], [qw/11 10/], [qw/9 25 16 _/], [qw/9 5 _ 3/], [qw/3 27 20 8 _/],
[qw/50 9 40 19/], [qw/8 16/], [qw/30 4 8 16 10 27 25 X 16/], [qw/27 4
5 43/], [qw/X 10 16 27/], [qw/24 8 24 27 5 5 16/], [qw/6/], [qw/24 25
20/], [qw/11 10/], [qw/3 27 10 27 5 13 5 16 27/], [qw/30 5 20 27 8 24
8 5 11/], [qw/19 40 27/], [qw/27 4 5 43/], [qw/4 10 42 5/], [qw/27
25/], [qw/20 5 27 40 20 16/], [qw/13 40/], [qw/6 5 43 3/], [qw/1/],
[qw/_ 4 25 16 5 3/], [qw/27 25/], [qw/3 4 25 X/], [qw/_ 25 25 11/],
[qw/24 10 8 27 4 19 5 24 25 20 5/], [qw/10 16 43/], [qw/_ 10 43 13 5
16 27/] );

my %f;
my \$count = 0;

foreach my \$row (@code) {
foreach my \$c (@\$row) {
\$f{\$c} += 1;
\$count += 1;
}
}

foreach my \$c (sort { \$f{\$b} <=> \$f{\$a} } keys %f) {
my \$p = \$f{\$c}/\$count;
print "\$c \$p\n";
}

my %s = (
'10' => 'E',
'5'  => 'T',
'27' => 'A',
'8'  => 'I',
'3'  => 'O',
'16' => 'N' );

foreach my \$row (@code) {
foreach my \$c (@\$row) {
if ( defined( \$s{\$c} ) ) {
print \$s{\$c};
} else {
print ".";
}
}
print " ";
}
print "\n";
```

killa bee said...

Check out this ACL paper (http://www.aclweb.org/anthology/P/P06/P06-2065.pdf) which breaks substitution ciphers without any domain-specific knowledge. It works pretty well, and you could use some kind of Bayesian metaprior to, e.g., favor substitution tables that are more "bijective".

Louis said...

Looks like your last example is putting P where there should be G: "lonp leps strip club", "sipnal", "pood", "standinp" ?

Louis said...

Looks like your last example might have substituted P for G by mistake: "standinp", "sipnal", "lonp leps strip club", "pood" ?

Hopper said...

You should give the author props for deliberate misspelling in order to make such analysis harder.
Assuming, of course, he knew how to spell in the first place.