1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
// Copyright © 2021 rsiddharth <s@ricketyspace.net>
// SPDX-License-Identifier: ISC
package challenge
import (
"fmt"
"ricketyspace.net/cryptopals/lib"
)
func C19() {
texts := []string{
"SSBoYXZlIG1ldCB0aGVtIGF0IGNsb3NlIG9mIGRheQ==",
"Q29taW5nIHdpdGggdml2aWQgZmFjZXM=",
"RnJvbSBjb3VudGVyIG9yIGRlc2sgYW1vbmcgZ3JleQ==",
"RWlnaHRlZW50aC1jZW50dXJ5IGhvdXNlcy4=",
"SSBoYXZlIHBhc3NlZCB3aXRoIGEgbm9kIG9mIHRoZSBoZWFk",
"T3IgcG9saXRlIG1lYW5pbmdsZXNzIHdvcmRzLA==",
"T3IgaGF2ZSBsaW5nZXJlZCBhd2hpbGUgYW5kIHNhaWQ=",
"UG9saXRlIG1lYW5pbmdsZXNzIHdvcmRzLA==",
"QW5kIHRob3VnaHQgYmVmb3JlIEkgaGFkIGRvbmU=",
"T2YgYSBtb2NraW5nIHRhbGUgb3IgYSBnaWJl",
"VG8gcGxlYXNlIGEgY29tcGFuaW9u",
"QXJvdW5kIHRoZSBmaXJlIGF0IHRoZSBjbHViLA==",
"QmV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FzIGJlZW4gY2hhbmdlZCBpbiBoaXMgdHVybiw=",
"VHJhbnNmb3JtZWQgdXR0ZXJseTo=",
"QSB0ZXJyaWJsZSBiZWF1dHkgaXMgYm9ybi4=",
}
// English alphabets ordered by frequency
// (https://mdickens.me/typing/letter_frequency.html)
ascii := []byte{
' ', 'e', 't', 'a', 'o', 'i', 'n', 's', 'r', 'h', 'l', 'd',
'c', 'u', 'm', 'f', 'g', 'p', 'y', 'w', 'b', ',', '.',
'v', 'k', '-', '"', '_', '\'', 'x', ')', '(', ';', '0', 'j',
'1', 'q', '=', '2', ':', 'z', '/', '*', '!', '?', '$', '3',
'5', '>', '{', '}', '4', '9', '[', ']', '8', '6', '7', '\\',
'+', '|', '&', '<', '%', '@', '#', '^', '`', '~',
}
// Key: ASCII character; Value: score
asciiScores := make(map[byte]int, 0)
for pos, a := range ascii {
asciiScores[a] = 255 - pos
// Also add the uppercase version of ascii
// character if it exists.
au := lib.ByteToUpper(a)
if a != au {
asciiScores[au] = 255 - pos
}
}
// Utility functions.
cipherStreamByteGroups := func(ciphers [][]byte) map[int][]byte {
kbg := make(map[int][]byte, 0)
for i := 0; i < len(ciphers); i++ {
for j := 0; j < len(ciphers[i]); j++ {
if _, ok := kbg[j]; !ok {
kbg[j] = make([]byte, 0)
}
kbg[j] = append(kbg[j], ciphers[i][j])
}
}
return kbg
}
crackOutputBlockByteForGroup := func(pos int, g []byte) byte {
po := make([][]byte, len(g)) // Potential Output block bytes
for i, c := range g {
po[i] = make([]byte, 0)
for _, a := range ascii {
o := c ^ a
po[i] = append(po[i], o)
// Also try the uppercase version of ascii
// character if it exists.
au := lib.ByteToUpper(a)
if a != au {
o := c ^ au
po[i] = append(po[i], o)
}
}
}
co := lib.BytesInCommon(po) // Common Output Block bytes.
if len(co) < 1 {
return 0 // NUL
}
if len(co) == 1 {
return co[0]
}
tob := byte(0) // The Output Block byte.
bscr := 0 // Best score.
for _, o := range co {
scr := 0
for _, c := range g {
p := c ^ o
if s, ok := asciiScores[p]; ok {
scr += s
}
if pos == 0 && lib.ByteIsUpper(p) {
scr += 1
}
}
if scr > bscr {
bscr = scr
tob = o
}
}
return tob
}
// Make ciphers from plain text data.
ciphers := make([][]byte, len(texts))
key, err := lib.RandomBytes(16)
if err != nil {
fmt.Printf("Error generating key %v", err)
return
}
nonce := uint64(lib.RandomInt(0, 10))
for i, b64 := range texts {
text := lib.Base64ToBytes(b64)
ciphers[i], err = lib.AESEncryptCTR(text, key, lib.AESGenCTRFunc(nonce))
if err != nil {
fmt.Printf("Error encrypting text:%d: %v", i, err)
return
}
}
// Group the cipher streams into groups by position of the cipher
// byte in the stream.
cbg := cipherStreamByteGroups(ciphers)
// Try get crack and get the output block stream.
obs := make(map[int]byte, 0) // Output Block Stream map.
for pos, cgrp := range cbg {
obs[pos] = crackOutputBlockByteForGroup(pos, cgrp)
}
// Decipher cipher using the output block stream.
for _, cipher := range ciphers {
for i, c := range cipher {
p := c ^ obs[i]
fmt.Printf("%c", p)
}
fmt.Printf("\n")
}
}
// Output:
// I have met them at close of day
// Coming with vivid faces
// From counter or desk among grey
// Eighteenth-century houses.
// I have passed with a nod of the heae
// Or polite meaningless words,
// Or have lingered awhile and said
// Polite meaningless words,
// And thought before I had done
// Of a mocking tale or a gibe
// To please a companion
// Around the fire at the club,
// Being certain that they and I
// But lived where motley is worn:
// All changed, changed utterly:
// A terrible beauty is born.
// That woman's days were spent
// In ignorant good will,
// Her nights in argument
// Until her voice grew shrill.
// What voice more sweet than hers
// When young and beautiful,
// She rode to harriers?
// This man had kept a school
// And rode our winged horse.
// This other his helper and friend
// Was coming into his force;
// He might have won fame in the end,
// So sensitive his nature seemed,
// So daring and sweet his thought.
// This other man I had dreamed
// A drunken, vain-glorious lout.
// He had done most bitter wrong
// To some who are near my heart,
// Yet I number him in the song;
// He, too, has resigned his part
// In the casual comedy;
// He, too, has been changed in his tus
// Transformed utterly:
// A terrible beauty is born.
// Stuff the program did not get correctly:
// 1. On the 5th line, 'heae' -> 'head'
// 2. On the 38th line, 'tus ' -> 'turn'
|