Posted By

TopcatSoftware on 01/09/11


Tagged

encode crypt encrypt ROT13 cipher


Versions (?)

rot13x()


 / Published in: JavaScript
 

URL: http://www.topcat.hypermart.net/rot13x-javascript.html

ROT13 is a self encrypting/decrypting cipher. While not terribly secure, its still a handy method for casually masking text. Here's my implementation of the ROT13 cipher in javascript extended to include digits. Feel free to use it in your own work, and contact us if you improve it. Wikipedia has more information about obfuscation.

  1. // rot13x.js - Topcat Software LLC. 2011
  2. // rot13 cipher extended to include digits
  3. // http://www.topcat.hypermart.net/rot13x-javascript.html
  4. //
  5. // referencing the script in your HTML:
  6. //
  7. // <html>
  8. // <head>
  9. // <script type="text/javascript" src="rot13x.js"></script>
  10. // </head>
  11. // <body>
  12. //
  13. // <script>var foo = rot13x("321 some string..."); var bar = rot13x(foo)</script>
  14. // <p>encoded: <script>document.write(foo)</script>
  15. // <p>decoded: <script>document.write(bar)</script>
  16. //
  17. // </body>
  18. // </html>
  19.  
  20. function rot13x(s) {
  21.  
  22. var rxi = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
  23. var rxo = "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm5678901234"
  24. var map = []
  25. var buf = ""
  26.  
  27. for (z = 0; z <= rxi.length; z++) {map[rxi.substr(z, 1)] = rxo.substr(z, 1)}
  28.  
  29. for (z = 0; z <= s.length; z++) {
  30. var c = s.charAt(z)
  31. buf += (c in map) ? map[c] : c
  32. }
  33.  
  34. return buf
  35.  
  36. }

Report this snippet  

You need to login to post a comment.