Public
Edited
Jan 21, 2023
1 fork
Importers
Insert cell
Insert cell
## Determining block size.

To simplify things, I first want to pretend like our block size is 4, and our encryption function is repeated xor.
Insert cell
Insert cell
CryptoJS = require('https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js')
Insert cell
Insert cell
Insert cell
cba = lib.ByteArray.fromb64(challengeText)
Insert cell
myBa = lib.ByteArray.fromBytes((new Array(16)).fill(lib.atoi("A")))
Insert cell
key = lib.ByteArray.fromWordArray(CryptoJS.lib.WordArray.random(16));
Insert cell
prependBs = (ba) => {
return lib.ByteArray.fromBytes(ba.bytes.concat(cba.bytes))
}
Insert cell
aes.encryptAes128Ecb(prependBs(myBa), key)
Insert cell
fillLen = (len) => {
return lib.ByteArray.fromBytes((new Array(len)).fill(lib.atoi("A")))
}
Insert cell
fillLast = (arr) => {
const rs = [];
for (let i = 0; i < 256; i++) {
rs.push(lib.ByteArray.fromBytes(arr.bytes.concat([i])))
}
return rs
}
Insert cell
lib.toHex(aes.encryptAes128Ecb(prependBs(fillLen(15)), key).bytes.slice(0, 16))

Insert cell
getByte0 = () => {
const b0 = lib.toHex(aes.encryptAes128Ecb(prependBs(fillLen(15)), key).bytes.slice(0, 16))
const cands = fillLast(fillLen(15));

const lut = {}
const checked = cands.map((c, i) => {
const encrypted = aes.encryptAes128Ecb(prependBs(c), key)
const hexBlock = lib.toHex(encrypted.bytes.slice(0, 16))
lut[hexBlock] = i;
});

return lut[b0];
}
Insert cell
lib.btos(lib.fromb64(challengeText)).slice(0,4)
Insert cell
pt0 = getByte0()
Insert cell
debug1 = {
const prefix = fillLen(14).concatRaw(pt0);
const b1 = lib.toHex(aes.encryptAes128Ecb(prependBs(prefix), key).bytes.slice(0, 16))
const cands = fillLast(prefix);

return {b1, prefix, cands};
}
Insert cell
debug1.cands[82]
Insert cell
getByte1 = (pt0) => {
const prefix = fillLen(14).concatRaw(pt0);

// return prefix;
const b1 = lib.toHex(aes.encryptAes128Ecb(prependBs(fillLen(14)), key).bytes.slice(0, 16))
const cands = fillLast(prefix);
//return cands;

const lut = {}
const checked = cands.map((c, i) => {
const encrypted = aes.encryptAes128Ecb(prependBs(c), key)
const hexBlock = lib.toHex(encrypted.bytes.slice(0, 16))
lut[hexBlock] = i;
});

return lut[b1];
}
Insert cell
pt1 = getByte1(pt0)
Insert cell
findMatch = (prefix, b) => {
const cands = fillLast(prefix);
const lut = {}
const checked = cands.map((c, i) => {
const encrypted = aes.encryptAes128Ecb(prependBs(c), key)
const hexBlock = lib.toHex(encrypted.bytes.slice(0, 16))
lut[hexBlock] = i;
});

return lut[b];
}
Insert cell
getByte2 = (pt0, pt1) => {
const prefix = fillLen(13).concatRaw([pt0, pt1]);
const b = lib.toHex(aes.encryptAes128Ecb(prependBs(fillLen(13)), key).bytes.slice(0, 16))
return findMatch(prefix, b)
}
Insert cell
lib.itoa(getByte2(pt0, pt1))
Insert cell
getNextByte = (bs) => {
const prepad = 15 - bs.length;
const prefix = fillLen(prepad).concatRaw(bs);
const b = lib.toHex(aes.encryptAes128Ecb(prependBs(fillLen(prepad)), key).bytes.slice(0, 16))
return findMatch(prefix, b)
}
Insert cell
getNextByte([82, 111])
Insert cell
attempt1[16].map(lib.itoa).join("")
Insert cell
attempt1 = {
let results = [[]];

for (let i = 0; i < 16; i += 1) {
const nb = getNextByte(results[i]);
results.push(results[i].concat([nb]));
}


return results
}
Insert cell
first16 = attempt1[16]
Insert cell
first16.slice(first16.length - 15, first16.length)
Insert cell
getNextByte2([82])
Insert cell
getNextByte2 = (bs) => {
const prepad = 15 - (bs.length % 16);
const prefix = fillLen(prepad).concatRaw(bs);

const blockNum = bs.length / 16;
const encrypted = aes.encryptAes128Ecb(prependBs(fillLen(prepad)), key);
const blocks = lib.chunkArr(encrypted.bytes, 16);
const b = lib.toHex(blocks[blockNum])
return findMatch(lib.ByteArray.fromBytes(bs.slice(bs.length - 15, bs.length)), b)
}
Insert cell
lib.itoa(getNextByte2(first16))
Insert cell
getNextByte3 = (bs) => {
const prepad = 15 - (bs.length % 16);
const prefix = fillLen(prepad).concatRaw(bs);

const blockNum = Math.floor(bs.length / 16);
const encrypted = aes.encryptAes128Ecb(prependBs(fillLen(prepad)), key);
const blocks = lib.chunkArr(encrypted.bytes, 16);

const b = lib.toHex(blocks[blockNum])

let holder = lib.ByteArray.fromBytes(bs.slice(Math.max(bs.length - 15, 0), bs.length));
if (holder.bytes.length < 15) {
holder = fillLen(15 - holder.bytes.length).concat(holder)
}
//return {b, blocks, blockNum, encrypted, prefix, prepad, holder, hl: 15 - holder.length};

return findMatch(holder, b);
}
Insert cell
getNextByte3(first16)
Insert cell
attempt2 = {
let results = [[]];

for (let i = 0; i < cba.bytes.length; i += 1) {
const nb = getNextByte3(results[i]);
results.push(results[i].concat([nb]));
}

return results

}
Insert cell
attempt2[attempt2.length -1].map(lib.itoa).join("")
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