1

I found the following code to decode a ROT13 string and put it into a webpage with JavaScript:

<script type="text/javascript">
 document.write("string".replace(/[a-zA-Z]/g, 
 function(c){return String.fromCharCode((c<="Z"?90:122)>=(c=c.charCodeAt(0)+13)?c:c-26);}));
</script>

I need a similar JavaScript code to decode ROT47.

I found a few functions, but I have no idea how to get it into the same script, so it will put the decoded string into a webpage.

One of them is here - http://www.visualco.de/ROTn.js.html

Can someone please help?

Thanks a lot!

IsaacL
  • 660
  • 4
  • 13
  • 25

2 Answers2

2

Simply copy the needed functions into your JavaScript. At the appropriate place, do

<script type="text/javascript">
 document.write(ROT47("string"));
</script>

necessary functions:

//
// ROTn.js -- ROT13 and ROT14
//
// JavaScript-code   implementing   ROT13  and   ROT47.    Useful  to   obscure
// email-adresses and telephone numbers from inquisitorial site crawlers.
//
// Usage:
//   <script type="text/javascript">
//     ROT13('<n uers="znvygb:vasb@ivfhnypb.qr">vasb@ivfhnypb.qr</n>');
//   </script>
//
// Resources:
//     http://de.wikipedia.org/wiki/ROT13
//     http://www.drweb.de/magazin/codieren-und-verschlusseln-mit-javascript/
//
////////////////////////////////////////////////
// (C) 2010 Andreas  Spindler. Permission to use, copy,  modify, and distribute
// this software and  its documentation for any purpose with  or without fee is
// hereby  granted.   Redistributions of  source  code  must  retain the  above
// copyright notice and the following disclaimer.
//
// THE SOFTWARE  IS PROVIDED  "AS IS" AND  THE AUTHOR DISCLAIMS  ALL WARRANTIES
// WITH  REGARD   TO  THIS  SOFTWARE   INCLUDING  ALL  IMPLIED   WARRANTIES  OF
// MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
// SPECIAL,  DIRECT,   INDIRECT,  OR  CONSEQUENTIAL  DAMAGES   OR  ANY  DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
// OF  CONTRACT, NEGLIGENCE  OR OTHER  TORTIOUS ACTION,  ARISING OUT  OF  OR IN
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
// 
// $Writestamp: 2010-06-09 13:07:07$
// $Maintained at: www.visualco.de$

function ROTn(text, map) {
  // Generic ROT-n algorithm for keycodes in MAP.
  var R = new String()
  var i, j, c, len = map.length
  for(i = 0; i < text.length; i++) {
    c = text.charAt(i)
    j = map.indexOf(c)
    if (j >= 0) {
      c = map.charAt((j + len / 2) % len)
    }
    R = R + c
  }
  return R;
}

function ROT47(text) {
  // Hides all ASCII-characters from 33 ("!") to 126 ("~").  Hence can be used
  // to obfuscate virtually any text, including URLs and emails.
  var R = new String()
  R = ROTn(text,
  "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~")
  return R;
}
Marcel Korpel
  • 21,536
  • 6
  • 60
  • 80
  • Thanks a lot! And sorry for the dumb question, but do I put the functions in a separate .js file, or in the html file? Thanks! – IsaacL Aug 04 '10 at 19:45
  • @Isaac: You can do both, but it is considered best practice to put them in a separate file (e.g., because the separate file *can* be more aggressively cached than the HTML source, i.e., when your server settings are correct; but many questions about that exist already on SO). Or even better: to put *all* JS in a separate file. You can just create a new node containing the email address from within JavaScript and insert that into the DOM, if you know what all this means (`document.write` is forbidden when using XHTML). – Marcel Korpel Aug 04 '10 at 20:04
  • I can put the JS in a separate file -I know how to do that :) I just don't understand what you mean with the last sentence - You can just create a new node containing the email address from within JavaScript and insert that into the DOM, if you know what all this means (document.write is forbidden when using XHTML). Can you explain? Thanks!! – IsaacL Aug 04 '10 at 20:10
  • @Isaac: I can, but at MDN they're better in it: https://developer.mozilla.org/en/Gecko_DOM_Reference/Introduction. In short: Just use `var node = document.createElement("a");`, populate it, like `node.href = ROT47("mailto:blabla");` and append it using `parentNode.appendChild(node);`. And remember: you're always free to ask new questions. Just one side note: please use the [beginner] tag, so people know with whom they deal with; that will prevent lots of stupid answers. – Marcel Korpel Aug 04 '10 at 20:16
  • Didn't see that tag :) And I know HTML pretty well, but just getting into JS. Point is, if I use your code above, am I OK? Thanks a lot, and sorry for all the questions... – IsaacL Aug 04 '10 at 20:38
  • @Isaac: You mean the code in my answer? Usually, you should be Ok. And no problem about the questions, if I don't feel like answering them, I just don't do it. ;) Good luck! – Marcel Korpel Aug 05 '10 at 11:02
  • I wasn't sure if when you were saying the thing about node, if I should use that instead... For now, the original code works great, and I think I'm going to stick with that. Thanks a lot for all the help!! – IsaacL Aug 06 '10 at 04:21
1

Here is another short one (rot47.min.js) to encode/decode using ROT47 algorithm

function rot47(x)
{var s=[];for(var i=0;i<x.length;i++)
{var j=x.charCodeAt(i);if((j>=33)&&(j<=126))
{s[i]=String.fromCharCode(33+((j+ 14)%94));}
else
{s[i]=String.fromCharCode(j);}}
return s.join('');}

source: https://rot47.net

justyy
  • 5,831
  • 4
  • 40
  • 73