Skip to content

Instantly share code, notes, and snippets.

@mikaturk
Created January 29, 2018 19:39
Show Gist options
  • Save mikaturk/83509a609e726b4171605ab01fb61148 to your computer and use it in GitHub Desktop.
Save mikaturk/83509a609e726b4171605ab01fb61148 to your computer and use it in GitHub Desktop.
function toLetter(number = 0) {
return String.fromCharCode(number%26+65)
}
function toNumber(string = 'A', index = 0) {
return string.charCodeAt(index)-65
}
function encryptLetter(message='', key='', index=0) {
return toLetter(
toNumber(message,index)
+toNumber(key,index%key.length)
%26
);
}
function shuffle(message='', shufflestring='ABCDEFGHIJKLMNOPQRSTUVWXYZ') {
return message
// transform message into array
.split('')
// do the shuffle operation for every letter
.map(letter => shufflestring[
(shufflestring.indexOf(letter)+1)%shufflestring.length
]
)
// transform the message back into a string
.join('')
}
function encrypt(message='', key='', shufflestring='ABCDEFGHIJKLMNOPQRSTUVWXYZ') {
let charcode = 0;
let shift = 0;
let sliceindex = 0;
let encrypted = '';
let newkey = '';
for (let a=0; a<message.length/key.length; a++) {
key = shuffle(key, shufflestring)
charcode = toNumber(key,a%key.length);
shift = (charcode+shift)%key.length;
sliceindex = key.length - shift;
newkey = key.slice(sliceindex) + key.slice(0,sliceindex);
console.log({charcode, shift, newkey, key});
for (let b=0; b<key.length; b++) {
const index = a*key.length+b
if (index<message.length) {
encrypted += encryptLetter(message, newkey, index)
}
}
}
return encrypted;
}
// Only CAPITALS and no punctuation marks
// third argument is the order in which the letters should be moved
// you should randomize the order and send that order with the key
console.log(encrypt(process.argv[2],process.argv[3], 'CLXUIQJPSADOWVHBFYERMGTZKN'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment