Skip to content

Instantly share code, notes, and snippets.

@xeoncross
Last active August 21, 2021 08:08
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save xeoncross/5969760 to your computer and use it in GitHub Desktop.
Save xeoncross/5969760 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>
@tsuchiLo
Copy link

tsuchiLo commented Nov 16, 2016

I download the html file and run by firefox. It is not work.

@arifjusoh
Copy link

it seems link to http://crypto-js.googlecode.com/svn/tags/3.1.2/ is no longer available

@betoramiz
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment