2010-08-04 57 views
1

我發現下面的代碼來解碼ROT13字符串,並把它變成一個網頁使用JavaScript:需要一個ROT47 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> 

我需要一個類似的JavaScript代碼來ROT47解碼。

我發現了一些函數,但我不知道如何將它放到同一個腳本中,所以它會將解碼後的字符串放入網頁中。

其中之一就是在這裏 - http://www.visualco.de/ROTn.js.html

是否有人可以幫忙嗎?

非常感謝!

+0

哇,很多無用的'new String()'實例在那裏...... – 2010-08-04 19:35:35

回答

3

只需將所需的功能複製到您的JavaScript中即可。在適當的位置,做

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

必要的功能:

// 
// 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:[email protected]">[email protected]</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, 
    "!\"#$%&'()*+,-./:;<=>[email protected][\\]^_`abcdefghijklmnopqrstuvwxyz{|}~") 
    return R; 
} 
+0

謝謝很多! 對於愚蠢的問題抱歉,但我把功能放在一個單獨的.js文件或HTML文件中? 謝謝! – IsaacL 2010-08-04 19:45:32

+0

沒關係......我得到它的工作。 非常感謝! – IsaacL 2010-08-04 20:02:14

+0

@Isaac:你可以同時執行這兩個操作,但最好將它們放在一個單獨的文件中(例如,因爲單獨的文件*可以比HTML源文件更積極地緩存,即當你的服務器設置正確時;但關於這方面的許多問題已經存在)。或者甚至更好:將* all * JS放在單獨的文件中。如果您知道所有這些意思(使用XHTML時禁止使用「document.write」),您可以在JavaScript中創建一個包含電子郵件地址的新節點,並將其插入到DOM中。 – 2010-08-04 20:04:21

0

下面是使用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('');} 

源另一個短一個(rot47.min.js)編碼/解碼: https://rot47.net