I have the following code that uses Crypto-JS to encrypt any file.
It works fine and as it should.
However, the produced encrypted file is HUGE in comparison with the original file!
Example: if I use a file that is 2.99MB and encrypt it, the produced encrypted file will be 5.3MB.
Is there anything i can do to keep the encrypted file as small as possible?
This is my entire code:
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Get Directory</title>
<!-- Update your jQuery version??? -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<!--
https://cdnjs.com/libraries/crypto-js
-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/aes.js"></script>
<!--[if lt IE 9]>
<script src="https://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<script> // type="text/javascript" is unnecessary in html5
// Short version of doing `$(document).ready(function(){`
// and safer naming conflicts with $
jQuery(function($) {
$('#file-input').on('change', function() {
// You can't use the same reader for all the files
// var reader = new FileReader
$.each(this.files, function(i, file) {
// Uses different reader for all files
var reader = new FileReader
reader.onload = function() {
// reader.result refer to dataUrl
// theFile is the blob... CryptoJS wants a string...
var encrypted = CryptoJS.AES.encrypt(reader.result, '12334');
var ecr = encrypted.toString();
var blob = new Blob([ecr], {
"type": "text/plain"
});
var link = document.createElement("a");
link.download = "Encrypteddocument";
link.href = URL.createObjectURL(blob);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
//alert(encrypted);
}
reader.readAsDataURL(file)
$('#thelist').append('FILES: ' + file.name + '<br>')
})
})
});
</script>
</head>
<body>
<input type="file" id="file-input">
<div id="thelist"></div>
<input type="button" id="button" value="Save" />
</body>
</html>
any help would be appreciated.
This question might be a duplicated of another question that was asked ages ago but the other question does not have a valid or 'accepted' answer. so this question still stands.
How can we reduce the size of the encrypted file?