DNK Gif

Dot Net Knowledge

Labels

Sunday, 6 September 2015

Encrypt/ Decrypt using Javascript

Encryption:

window.btoa(string) method

Example

var encryptedString=btoa("Hello World!");
alert(encryptedString); ==>> SGVsbG8gV29ybGQh  (base-64 encoded string)

Decryption:

Example

var decryptedString=atob("SGVsbG8gV29ybGQh");
alert(encryptedString); ==>> Hello World!

Example:

<!DOCTYPE html>
<html>
<body>

<p>Click the button to decode a base-64 encoded string.</p>

<button onclick="myFunction()">Try it</button>

<p><strong>Note:</strong> The atob() method is not supported in IE9 and earlier.</p>

<p id="demo"></p>

<script>
function myFunction() {
    var str = "Hello World!";
    var enc = window.btoa(str);
    var dec = window.atob(enc);

    var res = "Encoded String: " + enc + "<br>" + "Decoded String: " + dec;
    document.getElementById("demo").innerHTML = res;
}
</script>

</body>
</html>

No comments:

Post a Comment