Skip to content

Instantly share code, notes, and snippets.

@awhit012
Last active July 26, 2019 20:00
Show Gist options
  • Save awhit012/0aba5c6b7bdbbce6c86cdcaaee8d7468 to your computer and use it in GitHub Desktop.
Save awhit012/0aba5c6b7bdbbce6c86cdcaaee8d7468 to your computer and use it in GitHub Desktop.
const alphabet = {
"a": 0,
"b": 1,
"c": 2,
"d": 3,
"e": 4,
"f": 5,
"g": 6,
"h": 7,
"i": 8,
"j": 9,
"k": 10,
"l": 11,
"m": 12,
"n": 13,
"o": 14,
"p": 15,
"q": 16,
"r": 17,
"s": 18,
"t": 19,
"u": 20,
"v": 21,
"w": 22,
"x": 23,
"y": 24,
"z": 25,
" ": 26,
0: "a",
1: "b",
2: "c",
3: "d",
4: "e",
5: "f",
6: "g",
7: "h",
8: "i",
9: "j",
10: "k",
11: "l",
12: "m",
13: "n",
14: "o",
15: "p",
16: "q",
17: "r",
18: "s",
19: "t",
20: "u",
21: "v",
22: "w",
23: "x",
24: "y",
25: "z",
26: " "
}
const encrypt = (data, key) => {
data = data.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()']/g,"").toLowerCase()
// split data into array, change letters to numbers, add key
const numbers = data.split("").map( char => alphabet[char] + key)
// take number % the length of the alphabet, map back to letters and return
const letters = []
numbers.forEach( num => {
letters.push(alphabet[num % 27])
})
return letters.join("")
}
const decrypt = (data, key) => {
// split data into array, change letters to numbers, add the length of the alphabet minus the key modulo the length of the alphabet
const numbers = data.split("").map( char => alphabet[char] + (27 - key % 27))
// take number modulus 26 (length of alphabet), map back to letters and return
const letters = []
numbers.forEach( num => {
letters.push(alphabet[num % 27])
})
return letters.join("")
}
let a = encrypt("hello", 69834902303)
console.log(a)
console.log(decrypt(a, 69834902303))
let b = encrypt("If I do one thing right, god bless it'll be flipping this pancake proper.", 932849023843029)
console.log(b)
console.log(decrypt(b, 932849023843029))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment