Skip to content

Instantly share code, notes, and snippets.

@EnzoDiazDev
Created August 14, 2021 15:06
Show Gist options
  • Save EnzoDiazDev/a707d154806b23546f87fa8d5c00f0ad to your computer and use it in GitHub Desktop.
Save EnzoDiazDev/a707d154806b23546f87fa8d5c00f0ad to your computer and use it in GitHub Desktop.
vigenere with functions p1
function repeat_key(length){
let new_key = this.key;
while(new_key.length < length){
new_key = new_key.repeat(2)
}
return new_key.slice(0, length + 1);
}
function encode(str) {
const key = this.repeat_key(str.length)
return str
.split("")
.map((x, i) => {
const xi = this.alphabet.indexOf(x);
const k = key[i];
const ki = this.alphabet.indexOf(k);
if(xi === -1) return x;
const ei = (xi + ki) % this.l
return this.alphabet[ei];
})
.join("");
}
function decode(str) {
const key = this.repeat_key(str.length)
return str
.split("")
.map((c, i) => {
const ci = this.alphabet.indexOf(c);
const k = key[i];
const ki = this.alphabet.indexOf(k);
if(ci === -1) return c;
let di;
if((ci - ki) < 0) di = di = (ci - ki + this.l) % this.l;
else di = (ci - ki) % this.l;
return this.alphabet[di];
})
.join("");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment