Overview | File | Class | Tree | Index | Help
PREV NEXT | FRAMES NO FRAMES

## encrypt-file.js

#### Summary

Encrypt a file using hybrid encryption mode

Class Summary
EncryptFile |

    /**
     *  ---------
     * |.##> <##.|  Open Smart Card Development Platform (www.openscdp.org)
     * |#       #|
     * |#       #|  Copyright (c) 1999-2006 CardContact Software & System Consulting
     * |'##> <##'|  Andreas Schwier, 32429 Minden, Germany (www.cardcontact.de)
     *  ---------
     *
     *  This file is part of OpenSCDP.
     *
     *  OpenSCDP is free software; you can redistribute it and/or modify
     *  it under the terms of the GNU General Public License version 2 as
     *  published by the Free Software Foundation.
     *
     *  OpenSCDP is distributed in the hope that it will be useful,
     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     *  GNU General Public License for more details.
     *
     *  You should have received a copy of the GNU General Public License
     *  along with OpenSCDP; if not, write to the Free Software
     *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     *
     * @fileoverview Encrypt a file using hybrid encryption mode
     */

    var File = require("scsh/file/File").File;
    var FileEncryptor = require("scsh/file/FileEncryptor").FileEncryptor;
    var PublicKeyFile = require("scsh/file/PublicKeyFile").PublicKeyFile;

    function EncryptFile() {

    }

    EncryptFile.prototype.loadPublicKey = function(fname) {
    	this.pkf = new PublicKeyFile(fname);
    }

    EncryptFile.prototype.selectPublicKey = function() {
    	var fname = typeof(this.pkFileName) != "undefined" ? this.pkFileName : "";
    	fname = Dialog.prompt("Select recipient public key", fname, null, "*.pka");
    	if (!fname) {
    		throw new Error("User abort");
    	}
    	this.loadPublicKey(fname);
    	this.pkFileName = fname;
    }

    EncryptFile.prototype.selectFileToEncrypt = function() {
    	var fname = typeof(this.plainFileName) != "undefined" ? this.plainFileName : "";
    	fname = Dialog.prompt("Select file to encrypt", fname, null, "");
    	if (!fname) {
    		throw new Error("User abort");
    	}

    	this.plainFileName = fname;

    	fname = Dialog.prompt("Save encrypted file as", fname + ".enc", null, "");
    	if (!fname) {
    		throw new Error("User abort");
    	}

    	var f = new File(fname);
    	if (f.exists()) {
    		if (Dialog.prompt("File does already exist. Overwrite ?") == null) {
    			throw new Error("User abort");
    		}
    	}

    	this.encFileName = fname;
    }

    EncryptFile.prototype.encrypt = function() {
    	var fenc = new FileEncryptor(undefined, FileEncryptor.HYBRID);
    	fenc.deriveKey(this.pkf.getChain()[0]);

    	var f = new File(this.plainFileName);
    	var bin = f.readAllAsBinary();
    	f.close();

    	bin = fenc.encrypt(bin);

    	var f = new File(this.encFileName);
    	f.writeAll(bin);
    	f.close();

    	print("Encryption complete.");
    }

    if (typeof(e) == "undefined") {
    	var e = new EncryptFile();
    }

    e.selectPublicKey();
    e.selectFileToEncrypt();
    e.encrypt();

Overview | File | Class | Tree | Index | Help
PREV NEXT | FRAMES NO FRAMES

Documentation generated by JSDoc on Fri Mar 27 16:43:57 2026