-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenge4.ts
31 lines (29 loc) · 995 Bytes
/
challenge4.ts
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
import {breakSingleByteXOR, SingleByteXORDecryptionResult} from './challenge3';
type SingleByteXORDecryption = {
encryptedString: string;
decryptionInfo: SingleByteXORDecryptionResult;
}
/**
* Detect & decrypt ciphertext encrypted with single byte XOR
* @param ciphertexts array of ciphertexts
*/
export function detectSingleByteXOREncryptedString(ciphertexts: ReadonlyArray<string>): SingleByteXORDecryption {
let encryptedString = '';
let result: SingleByteXORDecryptionResult = {
score: Number.MAX_SAFE_INTEGER,
keyByte: 0x00,
plaintext: Buffer.alloc(1)
};
let currentScore: SingleByteXORDecryptionResult;
ciphertexts.forEach(ciphertext => {
currentScore = breakSingleByteXOR(ciphertext);
if (currentScore.score < result.score) {
result = currentScore;
encryptedString = ciphertext;
}
});
return {
encryptedString: encryptedString,
decryptionInfo: result
};
}