Hi Vencenzo,
I don't know if this will help, but I'm using SJCL to encrypt (and decrypt in Chrome) files like this:
listeners: {
change: function(filefield, values, options) {
var files = filefield.extractFileInput().files
for (var i = 0, f; f = files[i]; i++) {
var reader = new FileReader()
reader.onloadend = (function(theFile) {
return function(e) {
var form = filefield.up('form')
$('#attachments').append('<li><em>' + theFile.name + '</em>, ' + theFile.size + ' bytes</li>')
var encryption_key = JSON.parse(form.encryption_key)
var encrypted_file = sjcl.encrypt(encryption_key, reader.result)
var encrypted_name = sjcl.encrypt(encryption_key, theFile.name)
if (form.encrypted_attachments === undefined) {
form.encrypted_attachments = []
}
form.encrypted_attachments.push(JSON.stringify({
'name': encrypted_name,
'data': encrypted_file
}))
};
})(f);
reader.readAsBinaryString(f)
}
}
}
There's a bit of ExtJS stuff in there, but the key parts are that I'm reading the file in using FileReader and sticking it in my form (to be submitted to the server) just as the string that sjcl.encrypt returns (I'm also encrypting the file name). The decryption is handled using webkit's framework (I haven't found a good way to handle it in Firefox or IE yet, and I suspect I won't until they add support for the FileSystem object):
success: function(response) {
var json = JSON.parse(response.responseText)
var filename = sjcl.decrypt(JSON.parse(decryption_key),
json.attachment.name)
var filedata = sjcl.decrypt(JSON.parse(decryption_key), json.attachment.data)
window.webkitStorageInfo.requestQuota(PERSISTENT, 1024 * 1024 * 5,
function(grantedBytes) {
window.requestFileSystem(window.PERSISTENT, grantedBytes,
function(fs) {
fs.root.getFile(filename, {
create: true
},
function(fileEntry) {
fileEntry.createWriter(function(fileWriter) {
fileWriter.onwriteend = function(e) {
console.log('Write completed.')
}
fileWriter.onerror = function(e) {
console.log('Write failed: ' + e.toString())
}
var byteArray = new Uint8Array(filedata.length)
for (var i = 0; i < filedata.length; i++) {
byteArray[i] = filedata.charCodeAt(i) & 0xff
}
var builder = new BlobBuilder()
builder.append(byteArray.buffer)
fileWriter.write(builder.getBlob('application/octet-stream'))
window.open(fileEntry.toURL())
},
errorHandler)
},
errorHandler)
},
errorHandler)
},
function(e) {
console.log('Error', e)
})
}
I'm not using slices of Blobs, but perhaps some of the stuff in there can help you.