开发者

JavaScript encryption

the script has to search a string inside the webpage. but that script should not display what string that it is searching. I mean the search string should be in encrypted format or any other format. but without that search string the webpage should not be displayed or it should display an error on page.

I am going to 开发者_C百科develop a plugin. If anybody using that plugin in their webpage they must and should place my name or my website name in that page.

is it possible, if so how to encrypt my text (srikanth) inside the script and how to search that string inside the page.

how many possibilities are there to place my name in a webpage with javascript or jquery but it should not visible as it is when anybody check it in source code


There is no way of hiding your name. If the browser can see it, then so can any user.

You can encrypt your name anyway you like. But of course it needs to be decrypted client-side to actually do the search. So anyone with a javascript debugger could uncover your name in moments.

Or slightly more obscurity you could hash your name server-side, in javascript hash the page contents, and then do your search. Given a decent hash the chance of collisions will be small. However, with a debugger you could still figure out the search string no problem. And to be honest this just sounds absurd.

Whatever you're trying to achieve needs to be re-thought.


Anyone can view Javascript source code, therefore it's not really possible to encrypt something using Javascript in a way that is secure. You can obfuscate it, often in horrible ways, but it's always possible to reverse.

If you can, do anything requiring a modicum of security on the server.


As you cant really encrypt a piece of text you can obfuscate the search and do a check.

<!doctype html public "-//w3c//dtd html 3.2 final//en">
<html>
<head>
<!-- some simple styling so the div element does not appear, 
you could equally use a hidden form field and not require the styling -->
<style>#h3llo{display:none;}</style>
</head>
<body>
<div>Hello Some simple text
<form action="#" method="post" onSubmit="dosearch();return false;">
    <input type="text" id="searchfield" />
    <input type="button" name="submit" value="search" onClick="dosearch();return false;" />
</form>
</div>
<div id="h3llo"></div>
<script>
function d2h(d) {return d.toString(16);}
function h2d(h) {return parseInt(h,16);}
// converts the input string into hex vals
function Str2Hex(inputvars) {
  var tmp = inputvars;
  var str = '';
  for (var i=0; i<tmp.length; i++) {
    c = tmp.charCodeAt(i);
    str += '\\x' + d2h(c);
  }
  return str;
}
// converts the input field h3llo back to a string
function Hex2Str() {
  var tmp = document.getElementById('h3llo').innerHTML;
  var arr = tmp.split('\x');
  var str = '';
  for (var i=1; i<arr.length; i++) {
    c = String.fromCharCode(h2d(arr[i]));
    str += c;
  }
  return str.trim();
}
// fills the h3llo field with the encoded string
function populate(inputvars){document.getElementById('h3llo').innerHTML = Str2Hex(inputvars);}
// checks that the submitted search string matches the encoded string
function check(inputval){if(Hex2Str().toString() != inputval.toString()){ alert("Warning: '" + Hex2Str().toString() + "' != '" + inputval.toString() + "'");}else{alert("success");}}
// the action that fills the hidden field and checks the encoded value is the same
function dosearch(){var sval = String(document.getElementById('searchfield').value);populate(sval); check(sval);}
</script>
</body>
</html>

If someone viewed the source they wouldn't at first glance be able to see the search string, though as it has mentioned before this would be easy to reverse it would obfuscate from the casual viewer. Also If the encoded data was hidden by css as in this example or a hidden form field it would never appear on the page or source un-encoded.


I am developing a library to simplify my daily tasks, and one of which is encryption. I am using caesar encryption for element encryptions.

you may download the minified file and include it in your html.

The usage is like:

<!DOCTYPE HTML>
<html>

<head>
  <title>
    Element Encryption
  </title>
  <script type="text/javascript" src="https://raw.githubusercontent.com/Amin-Matola/domjs/master/dom-v1.0.0/dom.min.js"></script>
</head>

<body>
  <h1>Let's encrypt the element data</h1>
  <p>You may encrypt this paragraph data</p>
  <p>This is another paragraph you may encrypt too</p>

  <footer>
    <!--------- You may include your js here ------------->
    <script type="text/javascript">
          d("p").encrypt(text = "", depth = 10);
    </script>

  </footer>
</body>

</html>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜