Skip to content

Instantly share code, notes, and snippets.

@thoraxe
Last active August 9, 2018 18:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thoraxe/465b9e1418b6270d5dc6 to your computer and use it in GitHub Desktop.
Save thoraxe/465b9e1418b6270d5dc6 to your computer and use it in GitHub Desktop.
$(document).ready(
function() {
$("#cryptojs-encrypt-button").on("click", readSingleFile);
function readSingleFile(event) {
var file = $('#files')[0].files[0];
if (file) {
var reader = new FileReader();
reader.onload = function(file) {
// grab the password the user supplied
var key = $('#encrypt-password').val();
// encrypt using the supplied password
var encrypted = CryptoJS.AES.encrypt(reader.result, key);
// put the values on the page for later use
$("#cryptojs-encrypted").text(encrypted);
};
reader.onerror = function(event) {
console.error("File could not be read! Code "+ event.target.error.code);
};
reader.readAsBinaryString(file);
}
}
$("#sjcl-encrypt-button").on("click", readSJCLFile);
function readSJCLFile(event) {
var file = $('#files')[0].files[0];
if (file) {
var reader = new FileReader();
reader.onload = function(file) {
// grab the password the user supplied
var key = $('#encrypt-password').val();
// encrypt using the supplied password
var encrypted = sjcl.encrypt(key, reader.result);
// put the values on the page for later use
$("#sjcl-encrypted").text(encrypted);
};
reader.onerror = function(event) {
console.error("File could not be read! Code "+ event.target.error.code);
};
reader.readAsBinaryString(file);
}
}
$("#cryptojs-decrypt-button").click( function () {
var encrypted_contents = $("#cryptojs-encrypted").text();
var unlock_key = $("#encrypt-password").val();
var decrypted = CryptoJS.AES.decrypt(encrypted_contents, unlock_key)
var contents = decrypted.toString(CryptoJS.enc.Utf8);
var blob = new Blob([contents], {type : 'application/octet-stream'});
var objectURL = URL.createObjectURL(blob);
a = $('a#cryptojs-decrypted-link');
a.attr('href',''+objectURL);
a.attr('download', 'file');
a.attr('data-downloadurl','application/octet-stream:blah.txt:'+objectURL);
$('a#cryptojs-decrypted-link').text("Click to download CryptoJS decrypted file");
});
$("#sjcl-decrypt-button").click( function () {
var encrypted_contents = $("#sjcl-encrypted").text();
var unlock_key = $("#encrypt-password").val();
var decrypted = sjcl.decrypt(unlock_key, encrypted_contents);
var blob = new Blob([decrypted], {type : 'application/octet-stream'});
var objectURL = URL.createObjectURL(blob);
a = $('a#sjcl-decrypted-link');
a.attr('href',''+objectURL);
a.attr('download', 'file');
a.attr('data-downloadurl','application/octet-stream:blah.txt:'+objectURL);
$('a#sjcl-decrypted-link').text("Click to download SJCL decrypted file");
});
}
);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>allinone encryption/decryption binary test</title>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/core.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/lib-typedarrays.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/enc-base64.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/enc-utf16.js"></script>
<script src="https://raw.githubusercontent.com/bitwiseshiftleft/sjcl/master/sjcl.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script>
</head>
<body>
<form id="fileform">
<div id="filepicker">
<input type="file" id="files" name="files[]" multiple />
<output id="list"></output>
</div>
<div id="password-area">
<label for="encrypt-password">Encryption Password: </label>
<input type="text" id="encrypt-password" name="Encryption Password" />
</div>
<div id="submitter">
<input type="button" value="CryptoJS Encrypt" id="cryptojs-encrypt-button" />
<input type="button" value="SJCL Encrypt" id="sjcl-encrypt-button" />
<input type="reset" value="Reset" id="reset-button" />
</div>
</form>
<div id="encrypted-data">
<p id="cryptojs-encrypted">CryptoJS results go here</p>
<p id="sjcl-encrypted">SJCL results go here</p>
</div>
<form id="decrypt-form">
<div>
<input type="button" value="CryptoJS Decrypt" id="cryptojs-decrypt-button">
<input type="button" value="SJCL Decrypt" id="sjcl-decrypt-button">
</div>
</form>
<div id="decrypted-links">
<p>
<a id="cryptojs-decrypted-link"></a>
<a id="sjcl-decrypted-link"></a>
</p>
</div>
<script src="crypt.js"></script>
</body>
</html>
# the original file
$ xxd 1pix.gif
0000000: 4749 4638 3961 0100 0100 8000 00ff ffff GIF89a..........
0000010: ffff ff21 fe11 4372 6561 7465 6420 7769 ...!..Created wi
0000020: 7468 2047 494d 5000 2c00 0000 0001 0001 th GIMP.,.......
0000030: 0000 0202 4401 003b ....D..;
# the decrypted results from both CryptoJS and SJCL
$ xxd 1pixtest.gif
0000000: 4749 4638 3961 0100 0100 c280 0000 c3bf GIF89a..........
0000010: c3bf c3bf c3bf c3bf c3bf 21c3 be11 4372 ..........!...Cr
0000020: 6561 7465 6420 7769 7468 2047 494d 5000 eated with GIMP.
0000030: 2c00 0000 0001 0001 0000 0202 4401 003b ,...........D..;
@juanlive
Copy link

juanlive commented Aug 9, 2018

I have the same problem! If you discover something about it, please tell!

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