Skip to content

Instantly share code, notes, and snippets.

@asbjornu
Forked from xeoncross/cryptojs.html
Created October 28, 2024 14:58
Show Gist options
  • Save asbjornu/bee2e9340616ca79d61d5001eb768f35 to your computer and use it in GitHub Desktop.
Save asbjornu/bee2e9340616ca79d61d5001eb768f35 to your computer and use it in GitHub Desktop.
Simple AES-256 password-based encryption using the CryptoJS Javascript library.
<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<title></title>
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link rel="stylesheet" media="all" href=""/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<!-- Adding "maximum-scale=1" fixes the Mobile Safari auto-zoom bug: http://filamentgroup.com/examples/iosScaleBug/ -->
</head>
<body lang="en">
<form id="test">
<p>
<label>Secret</label><br>
<input id="secret" value="Secret Passphrase">
</p>
<p>
<label>Text</label><br>
<textarea id="text" style="width: 500px; height: 200px">This is the secret message</textarea>
</p>
<input type="submit" id="submit">
</form>
<div id="output"></div>
<!--
http://www.davidebarranca.com/2012/10/crypto-js-tutorial-cryptography-for-dummies/
-->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/core-min.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/enc-utf16-min.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/enc-base64-min.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script>
<script>
/*
// Simple test
var encrypted = '' + CryptoJS.AES.encrypt("Message", "Secret Passphrase");
var decrypted = CryptoJS.AES.decrypt(encrypted, "Secret Passphrase");
console.log(decrypted.toString(CryptoJS.enc.Utf8));
*/
$(function() {
$('#test').on('submit', function() {
var plaintext = $('#text').val();
var secret = $('#secret').val();
var encrypted = '' + CryptoJS.AES.encrypt(plaintext, secret);
console.log(encrypted);
var decrypted = CryptoJS.AES.decrypt(encrypted, secret);
console.log(decrypted.toString(CryptoJS.enc.Utf8));
return false;
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment