Created
December 7, 2019 17:17
-
-
Save SethVandebrooke/a3b01e2c53c68c649ebcf3b1a1b50077 to your computer and use it in GitHub Desktop.
Symetric Encryption
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
function SymetricEncryption(chars) { | |
var self = this; | |
self.alphabet = chars || "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890!@#$%^&*()_+-=`~[]\\{}|;':\",./<>? "; | |
function gc(pos) { | |
pos = pos % self.alphabet.length; | |
return self.alphabet[pos >= 0 ? pos : pos + self.alphabet.length]; | |
} | |
function gp (char) { | |
return self.alphabet.indexOf(char); | |
} | |
function a(s, p, e) { | |
var o = "", i = 0; | |
s.split('').forEach(function (char){ | |
var p1 = gp(char), p2 = gp(p[i++ % p.length]); | |
o += e ? gc(p1 + p2) : gc(p1 - p2); | |
}); | |
return o; | |
} | |
self.encrypt = function (text, password) { | |
return a(text, password, true); | |
}; | |
self.decrypt = function (text, password) { | |
return a(text, password, false); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment