Skip to content

Instantly share code, notes, and snippets.

@liketaurus
Created March 18, 2016 13:55
Show Gist options
  • Save liketaurus/9e0044a444d5b05bf8cf to your computer and use it in GitHub Desktop.
Save liketaurus/9e0044a444d5b05bf8cf to your computer and use it in GitHub Desktop.
JavaScript function that takes a ROT13 encoded string and decodes it
function rot13(str) {
var result="";
var rot=13;
var s=(26-rot)%26; //s=rot; if you want to encrypt!
for (var i=0;i<str.length;i++)
{
var c = str.charCodeAt(i);
if (c>=65 && c<=90)
result=result+String.fromCharCode((c-65+s)%26+65);
else
if (c>=97 && c<=122)
result=result+String.fromCharCode((c-97+s)%26+97);
else
result=result+str.charAt(i);
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment