Skip to content

Instantly share code, notes, and snippets.

@DiegoVallejoDev
Created June 18, 2024 18:14
Show Gist options
  • Save DiegoVallejoDev/10c20d7c473426fb5bc9cac539c2598e to your computer and use it in GitHub Desktop.
Save DiegoVallejoDev/10c20d7c473426fb5bc9cac539c2598e to your computer and use it in GitHub Desktop.
Vigenère cipher test
class VigenereCipher {
table: string[];
constructor(table: string[]) {
this.table = table;
}
encrypt(plaintext: string, key: string): string {
let ciphertext = "";
key = key
.repeat(Math.ceil(plaintext.length / key.length))
.slice(0, plaintext.length);
for (let i = 0; i < plaintext.length; i++) {
const pIndex = this.table[0].indexOf(plaintext[i]);
const kIndex = this.table[0].indexOf(key[i]);
if (pIndex !== -1 && kIndex !== -1) {
ciphertext += this.table[pIndex][kIndex];
} else {
ciphertext += plaintext[i]; // Non-alphabetic characters are added as-is
}
}
return ciphertext;
}
decrypt(ciphertext: string, key: string): string {
let plaintext = "";
key = key
.repeat(Math.ceil(ciphertext.length / key.length))
.slice(0, ciphertext.length);
for (let i = 0; i < ciphertext.length; i++) {
const kIndex = this.table[0].indexOf(key[i]);
if (kIndex !== -1) {
const row = this.table[kIndex];
const cIndex = row.indexOf(ciphertext[i]);
if (cIndex !== -1) {
plaintext += this.table[0][cIndex];
} else {
plaintext += ciphertext[i]; // Non-alphabetic characters are added as-is
}
} else {
plaintext += ciphertext[i]; // Non-alphabetic characters are added as-is
}
}
return plaintext;
}
prettyPrintTable(): void {
for (const row of this.table) {
console.log(row.split("").join(" "));
}
}
}
function generateAlphabet(key: string = "") {
let alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const keySet = new Set(key.toUpperCase());
// ie. key="KRYPTOS" -> alphabet="KRYPTOSABCDEFGHIJLMNQUVWXZ"
for (const char of keySet) {
alphabet = alphabet.replace(char, "");
}
alphabet = key + alphabet;
return alphabet;
}
// Example usage
const alphabet = generateAlphabet("KRYPTOS");
console.log("Alphabet:", alphabet);
const table: string[] = [];
// Generating the Vigenere table
for (let i = 0; i < alphabet.length; i++) {
table.push(alphabet.slice(i) + alphabet.slice(0, i));
}
const cipher = new VigenereCipher(table);
const plaintext =
"IT WAS TOTALLY INVISIBLE HOWS THAT POSSIBLE ? THEY USED THE EARTHS MAGNETIC FIELD X THE INFORMATION WAS GATHERED AND TRANSMITTED UNDERGRUUND TO AN UNKNOWN LOCATION X DOES LANGLEY KNOW ABOUT THIS ? THEY SHOULD ITS BURIED OUT THERE SOMEWHERE X WHO KNOWS THE EXACT LOCATION ? ONLY WW THIS WAS HIS LAST MESSAGE X THIRTY EIGHT DEGREES FIFTY SEVEN MINUTES SIX POINT FIVE SECONDS NORTH SEVENTY SEVEN DEGREES EIGHT MINUTES FORTY FOUR SECONDS WEST X LAYER TWO";
const key = "ABSCISSA";
const encrypted = cipher.encrypt(plaintext, key);
const decrypted = cipher.decrypt(encrypted, key);
console.log("Table:");
cipher.prettyPrintTable();
console.log("--------------------");
console.log("Key:", key);
console.log("--------------------");
console.log("Plaintext:", plaintext);
console.log("--------------------");
console.log("Encrypted:", encrypted);
console.log("--------------------");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment