Skip to content

Instantly share code, notes, and snippets.

@gausby
Created December 6, 2012 11:22
Show Gist options
  • Save gausby/4223796 to your computer and use it in GitHub Desktop.
Save gausby/4223796 to your computer and use it in GitHub Desktop.
function encryptROT13 (message) {
var input = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',
output = 'NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm',
rtn = [],
i
;
for (i in message) {
rtn[i] = output[input.indexOf(message[i])] || message[i];
}
return rtn.join('');
}
function decryptROT13 (message) {
var output = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz',
input = 'NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm',
rtn = [],
i
;
for (i in message) {
rtn[i] = output[input.indexOf(message[i])] || message[i];
}
return rtn.join('');
}
function stringToBinaryWords (message) {
var r = [],
i
;
for(i in message) {
var current = message.charCodeAt(i).toString(2);
current = "00000000".substr(current.length) + current;
r.push(current)
}
return r.join(' ');
}
function binaryWordsToString (message) {
var r = "",
i
;
message = message.split(" ");
for (i in message) {
r += String.fromCharCode(parseInt(message[i],2));
};
return r;
}
function reverseBinary (message) {
var i,
rtn = [],
lookup = {
'0': '1',
'1': '0',
' ': ' '
}
;
for (i in message) {
rtn.push(lookup[message[i]]);
}
return rtn.join('');
}
function encrypt (message) {
message = encryptROT13(message);
message = stringToBinaryWords(message);
message = reverseBinary(message);
return message;
}
function decrypt (message) {
message = reverseBinary(message);
message = binaryWordsToString(message);
message = decryptROT13(message);
return message;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment