Public
Edited
Oct 14, 2022
Insert cell
# Cryptopals 1.6

https://cryptopals.com/sets/1/challenges/6


Insert cell
import {lib} from "@pcarleton/cryptopals-helpers"
Insert cell
## 1: hamming distance
Insert cell
ham1 = (a, b) => {
return count1s(a ^ b);
}
Insert cell
hamArr = (c1, c2) => {
let r = 0;
for (let i = 0; i < c2.length; i += 1) {
r += ham1(c1[i], c2[i]);
}
return r;
}
Insert cell
count1s = (x) => {
return lib.toBinArr(x).reduce((acc, x) => acc + x, 0);
}
Insert cell
inputText = [
'this is a test',
'wokka wokka!!!',
];
Insert cell
sToArr = (s) => s.split("").map(lib.atoi)
Insert cell
sToArr(inputText[0])
Insert cell
hamArr(sToArr(inputText[0]), sToArr(inputText[1]))
Insert cell
Insert cell
import {chunkArr, encryptArrs} from "@pcarleton/cryptopals-1-5"
Insert cell
cText = [await FileAttachment("6.txt").text()] // an array so it doesn't take up so much vertical space
Insert cell
cArr = lib.fromb64(cText[0].split("\n").join(""))
Insert cell
avgHam = (bs, chunkSize) => {
let chunks = chunkArr(bs, chunkSize);
let r1 = hamArr(chunks[0], chunks[1]);
let r2 = hamArr(chunks[0], chunks[4]); // 0 //hamArr(chunks[2], chunks[3]);
let r3 = hamArr(chunks[1], chunks[5]); // 0 // hamArr(chunks[4], chunks[5]);
return (r1 + r2 + r3) / 3;
}
Insert cell
manyChunkedHams = (bs) => {
let hams = [];
for (let i = 1; i < Math.min(bs.length / 3, 40); i += 1) {
hams.push({keySize: i, avgHam: avgHam(bs, i) / i});
}
return hams
}
Insert cell
hamScores = manyChunkedHams(cArr)
Insert cell
Plot.plot({
color: {
type: "diverging",
scheme: "viridis"
},
marks: [
Plot.barY(hamScores, {y: "avgHam", x: "keySize", fill: d => d.avgHam})
]
})
Insert cell
suspectedKeySize = hamScores.sort((a,b) => a.avgHam - b.avgHam).slice(0, 3).map(x => x.keySize)
Insert cell
ks1 = suspectedKeySize[0];
Insert cell
bandedChunks = (arr, ks) => {
let bands = [];
for (let i = 0; i < arr.length; i += 1) {
let m = i % ks;
let b = bands[m] || [];
b.push(arr[i]);
bands[m] = b;
}

return bands;
}
Insert cell
ks1_chunks = bandedChunks(cArr, ks1)
Insert cell
import {getSpaceChiSquare, findBestChiKey} from "@pcarleton/cryptopals-1-1-through-1-4"
Insert cell
ks1_key = ks1_chunks.map(findBestChiKey).map(a => a[0].k).map(lib.itoa).join("")
Insert cell
encryptArrs(cArr, ks1_key.split("").map(lib.atoi)).map(lib.itoa).join("")
Insert cell
ks2_chunks = bandedChunks(cArr, 29)
Insert cell
encryptArrs(cArr, ks2_key.split("").map(lib.atoi)).map(lib.itoa).join("")
Insert cell
ks2_key = ks2_chunks.map(findBestChiKey).map(a => a[0].k).map(lib.itoa).join("")
Insert cell
Plot.plot({
color: {
type: "diverging",
scheme: "viridis"
},
marks: [
Plot.lineY(hamScores, {y: "avgHam", x: "keySize", fill: d => d.avgHam})
]
})

ks2_chunks.map(findBestChiKey).flat
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more