Skip to content

Instantly share code, notes, and snippets.

@jochemstoel
Forked from SethVandebrooke/RollingCypher.js
Created January 18, 2021 21:54
Show Gist options
  • Save jochemstoel/1fd342a3ca8e6635a581a21d2615a348 to your computer and use it in GitHub Desktop.
Save jochemstoel/1fd342a3ca8e6635a581a21d2615a348 to your computer and use it in GitHub Desktop.
Encrypt and decrypt text with a password and this simple yet effective cypher
function RollingCypher() {
var chars = " qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890!@#$%^&*()_+-=[]\\{}|;':\",./<>?`~\t\r\n";
chars = chars.concat(chars.concat(chars));
function encrypt(s, p) {
var o = "";
s = s.split('');
p = p.split('');
for (var i = 0; i < s.length; i++) {
o += chars[chars.indexOf(s[i]) + chars.indexOf(p[i % p.length])];
}
return o;
}
function decrypt(s, pass) {
var o = "";
s = s.split('');
pass = pass.split('');
for (var i = 0; i < s.length; i++) {
o += chars[chars.lastIndexOf(s[i]) - chars.indexOf(pass[i % pass.length])];
}
return o;
}
this.encrypt = encrypt;
this.decrypt = decrypt;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment