Payton Flint's Tech Blog
Menu
  • Home
  • Blog
  • Categories
  • Resources
  • About
  • Contact
Menu

JavaScript – Bot Repellant – Obfuscation

Posted on May 21, 2023June 4, 2023 by paytonflint

For obvious reasons, it is not a good idea to have your contact information displayed in plaintext on the web. Webcrawlers and bots do exist, and will use RegEx to pick emails and phone numbers out of client-side-source like it’s a cakewalk. Now, ideally, your implementation of what I am dubbing “bot-repellant”, would include server-side Recaptcha validation, and server-side storage of your encrypted contact info that would be decrypted upon validation.

But, if you are a lowly peon like myself, and merely have a WordPress site… perhaps you are a low-value target, with low concern regarding the compromise of your obscured text. Perhaps you merely want something that can be implemented via HTML/JavaScript that will render in the browser and obfuscate your information while repelling bots. Obviously, this is not to be used as a means of security- BUT, it obfuscates your information from being collected by automated web-crawlers and bots.

The cipher key is in the client-side source code, so anyone could potentially go through the trouble of deciphering your obscured text, but a bot is merely going to skim past your site and on to the next low-hanging fruit when its RegEx does not pick up contact info in the expected format.

I would consider this an exercise in determining the MVP, or Minimum Viable Product – in this case, good enough may very well be good enough. If this were not my personal information that could easily be obtained via other means by malicious actors, then I would not dream of considering something like this.

I want to make this very clear- this is not a security solution, and, frankly, it is improper implementation. When I say minimum viable product, I do mean minimum. But, one could use the following as a method of obfuscating information that you did not want to be easily scraped by a web-crawler or bot. It will not hold up to any scrutiny, and certainly should not be used for any clients or business. This is something that can be implemented on a simple site that allows you to add your own HTML/JS, and allow some level of obfuscation that will likely deter automated collection. But, it will not secure data from being collected by a malicious actor. However, if such information is low-value anyways, or might be made otherwise made public, then perhaps it could be a reasonable solution for you. Proceed at your own risk.

Here is some JavaScript code that I found on StackOverflow that will allow one to create a ciphertext from plaintext and decipher it back to plaintext using your determined cipher key. Keep in mind, this means you will have to include your cipher key in your client-facing source code, so it’s not a secure solution by any means.

const cipher = salt => {
    const textToChars = text => text.split('').map(c => c.charCodeAt(0));
    const byteHex = n => ("0" + Number(n).toString(16)).substr(-2);
    const applySaltToChar = code => textToChars(salt).reduce((a,b) => a ^ b, code);

    return text => text.split('')
      .map(textToChars)
      .map(applySaltToChar)
      .map(byteHex)
      .join('');
}
    
const decipher = salt => {
    const textToChars = text => text.split('').map(c => c.charCodeAt(0));
    const applySaltToChar = code => textToChars(salt).reduce((a,b) => a ^ b, code);
    return encoded => encoded.match(/.{1,2}/g)
      .map(hex => parseInt(hex, 16))
      .map(applySaltToChar)
      .map(charCode => String.fromCharCode(charCode))
      .join('');
}

// To create a cipher
const myCipher = cipher('<CIPHER-KEY>')

//Then cipher any text:
console.log(myCipher('<PLAINTEXT>'))

//To decipher, you need to create a decipher and use it:
const myDecipher = decipher('<CIPHER-KEY>')
console.log(myDecipher("<CIPHER-TEXT>"))

Additionally, I suggest using Google’s Recaptcha to try to deter bots. However, without server-side validation, this solution is also not to be relied upon. However, it is far better than nothing, so do it anyways. Here is my example which ties Google Recaptcha validation to the use of a submit button that will decipher your cipher text and display it. This means your plaintext is not displayed until Recaptcha validation has occurred, and cannot be extracted from the client-side source code. Again, Recaptcha is vulnerable without server-side validation, so this will not hold up to any scrutiny whatsoever from a malicious actor.

<html>
  <head>
    <script src="https://www.google.com/recaptcha/api.js" async defer></script>
	<script>
		const decipher = salt => {
			const textToChars = text => text.split('').map(c => c.charCodeAt(0));
			const applySaltToChar = code => textToChars(salt).reduce((a,b) => a ^ b, code);
			return encoded => encoded.match(/.{1,2}/g)
			.map(hex => parseInt(hex, 16))
			.map(applySaltToChar)
			.map(charCode => String.fromCharCode(charCode))
			.join('');
		}
		//To decipher, you need to create a decipher and use it:
		const myDecipher = decipher('<CIPHER-KEY>')
		const myPlaintext = (myDecipher("CIPHER-TEXT"))
	</script>
    <script>
      function displayText(event) {
        event.preventDefault(); // Prevent form submission
        var displayArea = document.getElementById("display-area");
        displayArea.textContent= myPlaintext;
      }
    </script>
  </head>
  <body>
    <form action="?" method="POST" onsubmit="return false;">
      <div
        class="g-recaptcha"
        data-sitekey="<YOUR-SITE-KEY>"
        data-theme="dark"
        data-callback="callback"
      ></div>
      <br />
      <button id="submit-button" type="submit" onclick="displayText(event)" disabled>Submit</button>
    </form>
    <script type="text/javascript">
      function callback() {
        const submitButton = document.getElementById("submit-button");
        submitButton.removeAttribute("disabled");
      }
    </script>
    <div id="display-area"></div>
  </body>
</html>

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

About The Author

Author's Portrait

In my journey as a technologist and 11 years of experience as an IT professional, I have found my niche as Director of Infrastructure Services; developing my skillsets in management, scripting, cloud infrastructure, identity management, and networking.

I have experience as a Systems Administrator and Engineer for large enterprises including the DoD, government agencies, and a nuclear-generation site.

I've been blessed to collaborate with engineers at esteemed Fortune 50 corporations, and one of Africa's largest, to ensure successful implementation of my work.

GitHub Button

Credentials

M365 Endpoint Administrator Associate
M365 Fundamentals
Microsoft AZ-900
CompTIA CSIS
CompTIA CIOS
CompTIA Security+
CompTIA Network+
CompTIA A+
  • August 2025
  • April 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
© 2022 Payton Flint | The views and opinions expressed on this website belong solely to the author/owner and do not represent the perspectives of any individuals, institutions, or organizations, whether affiliated personally or professionally, unless explicitly stated otherwise. The content and products on this website are provided as-is with no warranties or guaranties, are for informational/demonstrative purposes only, do not constitute professional advice, and are not to be used maliciously. The author/owner is not responsible for any consequences arising from actions taken based on information provided on this website, nor from the use/misuse of products from this site. All trademarks are the property of their respective owners.