Skip to content

Instantly share code, notes, and snippets.

@korniychuk
Last active October 8, 2016 04:17
Show Gist options
  • Save korniychuk/ea8aa16e5b8cd808aef3a6ea30f5fae5 to your computer and use it in GitHub Desktop.
Save korniychuk/ea8aa16e5b8cd808aef3a6ea30f5fae5 to your computer and use it in GitHub Desktop.
Simple encryption algorithm
class Security {
static encrypt(text, password) {
password = String(password);
return String(text)
.split('')
.map((char, index) => String.fromCharCode(char.charCodeAt(0) + password.charCodeAt(index % password.length)))
.join('');
}
static decrypt(text, password) {
password = String(password);
return String(text)
.split('')
.map((char, index) => String.fromCharCode(char.charCodeAt(0) - password.charCodeAt(index % password.length)))
.join('');
}
}
let
text = 'Super Secret Text',
password = 'Some Super-Password';
let
encryptedText = Security.encrypt(text, password),
decryptedText = Security.decrypt(encryptedText, password);
console.log(`Is : '${text}' === '${decryptedText}'`, decryptedText === text);
console.log(`Encrypted text: ${encryptedText}`);
/* Output:
*
* Is : 'Super Secret Text' === 'Super Secret Text' true
* Encrypted text: ¦äÝʒsÈÕÈä…ÄÇØïã
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment