Here is an implementation of ROT13, a kind of weak encryption scheme, in javascript.
I believe that it can be further enhanced to be more efficient. Can anyone give me ideas?Code:function encodeROT13(){ var frm = document.forms[0]; var strencode; var strencoderet; var basenum; strencode = frm.txtEncode.value; strencoderet = ""; for(i = 0; i < strencode.length; i++){ if( (strencode.charCodeAt(i) >= 97 && strencode.charCodeAt(i) <= 122 ) || (strencode.charCodeAt(i) >= 65 && strencode.charCodeAt(i) <= 90 )) { basenum = (strencode.charCodeAt(i) > 90)? 97 : 65; strencoderet += String.fromCharCode( ((strencode.charCodeAt(i) - basenum + 14) % 26 ) + basenum ) } else { strencoderet += strencode.charAt(i); } } frm.txtEncodeRet.value = strencoderet; } function decodeROT13(){ var frm = document.forms[0]; var strdecode; var strdecoderet; var basenum; strdecode = frm.txtDecode.value; strdecoderet = ""; for(i = 0; i < strdecode.length; i++){ if( (strdecode.charCodeAt(i) >= 97 && strdecode.charCodeAt(i) <= 122 ) || (strdecode.charCodeAt(i) >= 65 && strdecode.charCodeAt(i) <= 90 )) { basenum = (strdecode.charCodeAt(i) > 90)? 97 : 65; if (((strdecode.charCodeAt(i) - basenum - 13)) % 26 > 0) { strdecoderet += String.fromCharCode( ((strdecode.charCodeAt(i) - basenum - 14) % 26 ) + basenum ) } else { strdecoderet += String.fromCharCode( strdecode.charCodeAt(i) + 12 ) } } else { strdecoderet += strdecode.charAt(i); } } frm.txtDecodeRet.value = strdecoderet; }




Reply With Quote