Friday, 3 May 2019

Is this a possible way to encrypt a binary file with openpgp.js and how to save the file so it can be also handle 'manual' - in Filebrowser?

I'm trying to read a binary file, to encrypt/decrypt and save/write it. Comparing the original file with the resulting file of the decryption leads to the same filehash. So I think it's a correct/possible way.

If I try to decrypt the file manually in Windows with GnuPG the it is stated that the file is verified but no usable file is saved. So my question is: How do I have to store the file that it could be used 'manually' also.

Used sources

My code is based on following post: https://commscentral.net/tech/?post=64

-> example code of post on GitHub

But I read that for dealing with a binary file/payload:

openpgp.message.fromBinary()

should be used. -> https://github.com/openpgpjs/openpgpjs/issues/204#issuecomment-43065260

My code

imports

const fs = require('fs');
const util = require('util');
const readFile = util.promisify(fs.readFile);
const writeFile = util.promisify(fs.writeFile);
const openpgp = require('openpgp') // use as CommonJS, AMD, ES6 module or via window.openpgp

encryption

const encryptFuntion = async() => {
    openpgp.initWorker({ path:'openpgp.worker.js' }) // set the relative web worker path
    const pubkey = await readFile('pub.asc','utf8');
    const passphrase = `password`;
    const privkey = await readFile('priv.asc','utf8');
    const privKeyObj = (await openpgp.key.readArmored(privkey)).keys[0];
    await privKeyObj.decrypt(passphrase);
    const file = await readFile('test.txt');
    const fileArray = new Uint8Array(file);

    const options = {
        message: openpgp.message.fromBinary(fileArray),
        publicKeys: (await openpgp.key.readArmored(pubkey)).keys,
        privateKeys: [privKeyObj],
        armor:false
    };
    const encryptionResponse = await openpgp.encrypt(options);
    const encryptedFile = encryptionResponse.message.packets.write();
    await writeFile('test.txt.gpg',encryptedFile);
};

decryption

const decryptFuntion = async () => {
    openpgp.initWorker({path: 'openpgp.worker.js'}) // set the relative web worker path
    const passphrase = `password`;
    const privkey = await readFile('priv.asc', 'utf8');
    const pubkey = await readFile('pub.asc', 'utf8');
    const privKeyObj = (await openpgp.key.readArmored(privkey)).keys[0];
    await privKeyObj.decrypt(passphrase);
    const file = await readFile('test.txt.gpg');
    const fileArray = new Uint8Array(file);
    options = {
        message: await openpgp.message.read(file), // parse encrypted bytes
        privateKeys: [privKeyObj],              // decrypt with password
        publicKeys: (await openpgp.key.readArmored(pubkey)).keys,
        format: 'binary'                          // output as Uint8Array
    };
    const decryptionResponse = await openpgp.decrypt(options);
    const decryptedFile = decryptionResponse.data;
    await writeFile('test-decrypted.txt', decryptedFile);
};



from Is this a possible way to encrypt a binary file with openpgp.js and how to save the file so it can be also handle 'manual' - in Filebrowser?

No comments:

Post a Comment