Last active
October 8, 2016 04:17
-
-
Save korniychuk/ea8aa16e5b8cd808aef3a6ea30f5fae5 to your computer and use it in GitHub Desktop.
Simple encryption algorithm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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