Posted By

TopcatSoftware on 01/09/11


Tagged

cipher


Versions (?)

xorEncode()


 / Published in: JavaScript
 

URL: http://www.topcat.hypermart.net/index.html

Here's an implementation of the classic xor cipher written javascript. The script encodes/decodes plain-text using a password you supply. Feel free to use it in your own work, and contact us if you improve it. Something to remember, the longer the password string, the better...

  1. // xorEncode.js - Topcat Software LLC. 2011
  2. // bitwise XOR cipher for javascript
  3. // http://www.topcat.hypermart.net/index.html
  4. //
  5. // referencing the script in your HTML:
  6. //
  7. // <html>
  8. // <head>
  9. // <script type="text/javascript" src="xorEncode.js"></script>
  10. // </head>
  11. // <body>
  12. //
  13. // <script>
  14. // var foo = xorEncode("we can be heros...", "mypassword")
  15. // var bar = xorEncode(foo, "mypassword")
  16. // </script>
  17. //
  18. // <p>encoded: <script>document.write(foo)</script>
  19. // <p>decoded: <script>document.write(bar)</script>
  20. //
  21. // </body>
  22. // </html>
  23.  
  24. function xorEncode(txt, pass) {
  25.  
  26. var ord = []
  27. var buf = ""
  28.  
  29. for (z = 1; z <= 255; z++) {ord[String.fromCharCode(z)] = z}
  30.  
  31. for (j = z = 0; z < txt.length; z++) {
  32. buf += String.fromCharCode(ord[txt.substr(z, 1)] ^ ord[pass.substr(j, 1)])
  33. j = (j < pass.length) ? j + 1 : 0
  34. }
  35.  
  36. return buf
  37.  
  38. }

Report this snippet  

You need to login to post a comment.