Crypt and decrypt your email with JavaScript

If you want to publish an email on your site but you want to protect it, I have a simple solution Sorriso

 

HTML

<!DOCTYPE html>
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="crypto.js"></script>
    <script src="uncrypto.js"></script>
</head>
<body>
    <h1>JavaScript eMail Encrypter</h1>

    <p>You can encrypt mailto: links on a website, so that spiders can't detect them, with a simple javascript.</p>
    <p>Example: <a href="javascript:linkTo_UnCryptMailto('nbjmup;spttjojAftjb/dp');">rossini [at] esia [dot] co</a></p>

    <form>
        <div>
            <div class="container">
                <div class="desc">enter your eMail address:</div>
                <div><input type="text" name="emailField" size="40" maxlength="255" /></div>
            </div>

            <div class="container">
                <div class="desc"><input type="button" name="ecrypt" value="Crypt eMail Address" onclick="CryptMailto()" /></div>
                <div>&#160;&#160;</div>
                <div>

                    <br />
                    <br />

                    <div class="container">
                        <div class="desc">crypted eMail:</div>
                        <div><input type="text" name="cyptedEmailField" size="40" maxlength="255" /></div>
                    </div>

                    <div class="container">
                        <div class="desc">html code:</div>
                        <div><textarea name="HTMLCyptedEmailField" cols="50" rows="8"></textarea></div>
                    </div>
                </div>
            </div>
        </div>
    </form>
</body>
</html>

Crypt (js)

function CryptMailto() {
    var n = 0;
    var r = "";
    var s = "mailto:" + document.forms[0].emailField.value;
    var e = document.forms[0].emailField.value;

    e = e.replace(/@/, " [at] ");
    e = e.replace(/\./g, " [dot] ");

    for (var i = 0; i < s.length; i++) {
        n = s.charCodeAt(i);
        if (n >= 8364) {
            n = 128;
        }
        r += String.fromCharCode(n + 1);
    }

    document.forms[0].cyptedEmailField.value = r;
    document.forms[0].HTMLCyptedEmailField.value = "<a href=\"javascript:linkTo_UnCryptMailto('" + r + "');\">" + e + "</a>";
}

Decrypt (js)

function UnCryptMailto(s) {
    var n = 0;
    var r = "";
    for (var i = 0; i < s.length; i++) {
        n = s.charCodeAt(i);
        if (n >= 8364) {
            n = 128;
        }
        r += String.fromCharCode(n - 1);
    }
    return r;
}

function linkTo_UnCryptMailto(s) {
    var a = document.createElement('a');
    a.href = UnCryptMailto(s);
    document.body.appendChild(a); // Add to the DOM
    a.click();
    document.body.removeChild(a); // Remove it back
}

 

Happy coding!

One thought on “Crypt and decrypt your email with JavaScript

  1. Pingback: Homepage

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.