Skip to content

Instantly share code, notes, and snippets.

@d5
Created January 6, 2012 03:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save d5/1568870 to your computer and use it in GitHub Desktop.
Save d5/1568870 to your computer and use it in GitHub Desktop.
AES-256 Encryption AJAX Demo using Micro-API
<html>
<head>
<title>Micro-API Demo #1</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
<script type="text/javascript">
jQuery.support.cors = true;
function cancel_event_default(e) {
if(e.preventDefault) e.preventDefault();
else e.returnValue = false;
}
function run_async(url, params, on_success, on_error) {
jQuery.ajax({
url: url,
type: 'POST',
dataType: 'json',
data: params,
crossDomain: true,
success: on_success,
error: on_error
});
}
$(document).ready(function() {
run_async('https://micro-api.appspot.com/randbytes', {'length': 48 },
function(data){
$('#aes-iv').val(data.value.slice(0, 32));
$('#aes-key').val(data.value.slice(32, 96));
$('#aes-enc-button').removeAttr('disabled');
$('#aes-input').removeAttr('disabled');
});
$('#aes-enc-button').click(function(evt) {
cancel_event_default(evt);
$('#aes-enc-button').attr('disabled', 'disabled');
$('#aes-input').attr('disabled', 'disabled');
run_async('https://micro-api.appspot.com/utf8enc', {'value': $('#aes-input').val() }, function(data){
run_async(
'https://micro-api.appspot.com/aes256enc',
{
"iv": $('#aes-iv').val(),
"key": $('#aes-key').val(),
"value": data.value
},
function(data){
$('#aes-iv').val(data.iv);
$('#aes-input-length').val(data.input_length);
$('#aes-input').val(data.value);
$('#aes-dec-button').removeAttr('disabled');
});
});
});
$('#aes-dec-button').click(function(evt) {
cancel_event_default(evt);
$('#aes-dec-button').attr('disabled', 'disabled');
run_async(
"https://micro-api.appspot.com/aes256dec",
{
"iv": $('#aes-iv').val(),
"key": $('#aes-key').val(),
"value": $('#aes-input').val(),
"length": $('#aes-input-length').val()
},
function(data) {
var decoded = '';
run_async('https://micro-api.appspot.com/utf8dec', {'value': data.value }, function(data) {
$('#aes-input').val(data.value);
$('#aes-enc-button').removeAttr('disabled');
$('#aes-input').removeAttr('disabled');
});
});
});
});
</script>
</head>
<body>
<h1>AES-256 Encryption (AJAX)</h1>
<form>
<i>IV</i><br/>
<input id="aes-iv" type="text" style="width: 500px;"></input><br/>
<i>Key</i><br/>
<input id="aes-key" type="text" style="width: 500px;"></input><br/>
<i>Value</i><br/>
<textarea id="aes-input" type="text" style="width: 500px;" rows="5" disabled="disabled">Daniel Kang</textarea><br/>
<i>Length</i><br/>
<input id="aes-input-length" type="text" value="0" style="width: 500px;" readonly="readonly"/><br/>
<input id="aes-enc-button" type="button" value="Encrypt" disabled="disabled"/>
<input id="aes-dec-button" type="button" value="Decrypt" disabled="disabled"/>
</form>
</body>
</html>
@d5
Copy link
Author

d5 commented Jan 6, 2012

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