Vigenère cipher: Difference between revisions

Content added Content deleted
m (added whitespace before the TOC (table of contents), added a ;Task: (bold) header, added other whitespace to the task's preamble.)
(TypeScript solution)
Line 3,152: Line 3,152:
dec: GWQWEACDCBLUXNWOIYXPQAHSHLPQFLNDRYUWUKEAWCHSNYU
dec: GWQWEACDCBLUXNWOIYXPQAHSHLPQFLNDRYUWUKEAWCHSNYU
check: BEWARETHEJABBERWOCKTHEJAWSTHATTHECLAWSTHATCATCH</pre>
check: BEWARETHEJABBERWOCKTHEJAWSTHATTHECLAWSTHATCATCH</pre>

=={{header|TypeScript}}==
<lang JavaScript>class Vigenere {

key: string

/** Create new cipher based on key */
constructor(key: string) {
this.key = Vigenere.formatText(key)
}

/** Enrypt a given text using key */
encrypt(plainText: string): string {
return Array.prototype.map.call(Vigenere.formatText(plainText), (letter: string, index: number): string => {
return String.fromCharCode((letter.charCodeAt(0) + this.key.charCodeAt(index % this.key.length) - 130) % 26 + 65)
}).join('')
}

/** Decrypt ciphertext based on key */
decrypt(cipherText: string): string {
return Array.prototype.map.call(Vigenere.formatText(cipherText), (letter: string, index: number): string => {
return String.fromCharCode((letter.charCodeAt(0) - this.key.charCodeAt(index % this.key.length) + 26) % 26 + 65)
}).join('')
}

/** Converts to uppercase and removes non characters */
private static formatText(text: string): string {
return text.toUpperCase().replace(/[^A-Z]/g, "")
}

}

/** Example usage */
(() => {
let original: string = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book."

console.log(`Original: ${original}`)

let vig: Vigenere = new Vigenere("vigenere")

let encoded: string = vig.encrypt(original)

console.log(`After encryption: ${encoded}`)

let back: string = vig.decrypt(encoded)

console.log(`After decryption: ${back}`)

})()
<\lang>


=={{header|VBScript}}==
=={{header|VBScript}}==