I'm trying to use sjcl to encrypt and then decrypt an arraybuffer. I can see in the source that sjcl has a codec for arraybuffer but I just can't figure out how to use it:
const buffer = new ArrayBuffer(64);
const view = new Uint8Array(buffer);
console.log(view.length)
var encrypted = sjcl.encrypt("password", buffer)
console.log(encrypted.length)
var decrypted = sjcl.decrypt("password", encrypted)
console.log(decrypted.length)
The first console.log() prints 64 (which makes sense). The second console.log() prints 150 (the size of the encrypted results)... But the third console.log always prints 0.
The usage and documentation on sjcl is as cryptic as it's purpose but what you want to achieve is possible of course.
First you have to use the arrayBufferCodec codec to translate your ArrayBuffer to sjcl's own internal bitArray representation. Afterwards this needs to be translated once more to a Base64 string which can be fed to the
sjcl.encrypt()
method. The reverse of this procedure will give you back your original ArrayBuffer.Here's an example: