Skip to content

Instantly share code, notes, and snippets.

/encdec.js Secret

Created December 30, 2012 20:48
Show Gist options
  • Save anonymous/a110621fe731facb40ff to your computer and use it in GitHub Desktop.
Save anonymous/a110621fe731facb40ff to your computer and use it in GitHub Desktop.
function encrypt(s, pw) {
var a=0, i=0, myString='', textLen=s.length, pwLen=pw.length;
for (i=0; i<textLen; i++) {
a = parseInt(s.charCodeAt(i), 10);
a = a^(pw.charCodeAt(i % pwLen));
a = a + "";
while (a.length<3) {
a='0'+a;
}
myString += a;
}
return myString; // returned encrypted string
}
function decrypt(s, pw) {
var myString='',
a=0, i=0, pwLen=pw.length,
textLen=s.length, myHolder="";
while(i < s.length-2) {
myHolder = s.charAt(i) + s.charAt(i+1) + s.charAt(i+2);
if (s.charAt(i) === '0') {
myHolder = s.charAt(i+1) + s.charAt(i+2);
}
if ((s.charAt(i) ==='0') && (s.charAt(i+1) === '0')) {
myHolder = s.charAt(i+2);
}
a = parseInt(myHolder, 10);
a = a^(pw.charCodeAt((i/3) % pwLen));
myString += String.fromCharCode(a);
i+=3;
}
return myString; // returned decrypted string
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment