Email to HEX

by M Shameer

HTML

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<h2>Encode/Decode Email:</h2>
<input tyle="text" />
<p id="email"></p>
<button id="encode">
Encode email address
</button>

<button id="decode">
decode email address
</button>

CSS

input{
  padding:10px;
  width:80%;
}

JavaScript

/*function encodeCfEmail(email) {
    const key = 0x21;
    const encoded = [key];
    for (let i = 0; i < email.length; i++) {
        encoded.push(email.charCodeAt(i) ^ key);
    }
    //return encoded.map(b => ('0' + b.toString(16)).slice(-2)).join('').toUpperCase();
    $("#email").text( encoded.map(b => ('0' + b.toString(16)).slice(-2)).join('').toUpperCase() );
}
*/
  function encodeCfEmail(email, key = 0x21) {
    let hex = key.toString(16).padStart(2, '0');
    for (let i = 0; i < email.length; i++) {
      let xorByte = email.charCodeAt(i) ^ key;
      hex += xorByte.toString(16).padStart(2, '0');
    }
    //return hex.toUpperCase();
    $("#email").text(hex.toUpperCase())
  }

  function decodeCfEmail(encodedHex) {
    var data = [];
    for (var i = 0; i < encodedHex.length; i += 2) {
      data.push(parseInt(encodedHex.substr(i, 2), 16));
    }
    var key = data[0];
    var email = '';
    for (var i = 1; i < data.length; i++) {
      email += String.fromCharCode(data[i] ^ key);
    }
    $('#email').text(email);
    //return email;
  }



$( document ).ready(function() {

  $( 'button#encode' ).click(function() {
		encodeCfEmail( $('input').val() )
    
  });
  
    $( 'button#decode' ).click(function() {
		decodeCfEmail( $('input').val() )
    
  });

});