Skip to content

qie.pgpSignEncryptFile

Signature: qie.pgpSignEncryptFile(inputFile, outputFile, publicKey, privateKey, privateKeyPassphrase, asciiArmored*, integrityCheck*)

Returns: Boolean succeeded - true if encryption was successful

Encrypts inputFile using PGP encryption with the supplied public key, a signature using the supplied private key will be generated. The output is written to the outputFile.

Note

The privateKey and publicKey must be generated from a system that can generate valid PGP key pairs.

Parameters

Type Name Description Default
String, File inputFile the file to be encrypted
String, File outputFile the output file containing the encrypted contents
String, File, or byte[] publicKey the PGP public key to use for encryption
String, File, or byte[] privateKey the PGP private key to use for signature generation
String privateKeyPassphrase password to unlock the private key for signature generation
Boolean asciiArmored* (optional) use PGP ascii armored, the encrypted text will be in standard ascii false
Boolean integrityCheck* (optional) use PGP packet integrity check false

Example

// Examples of how to use pgpSignEncryptFile
// Example: inputFile as String path, outputFile as String path,
// publicKey as String path, privateKey as String path
var successOnEncryption = qie.pgpSignEncryptFile(
   "/tmp/foot.dcm",           // inputFile - the file to be encrypted
   "/tmp/encryptedFoot.txt",  // outputFile - the encrypted output file
   "/tmp/pgpPublic.key",      // publicKey - PGP public key used for encryption
   "/tmp/pgpPrivate.key",     // privateKey - PGP private key used for signing
   "changeit",                // privateKeyPassphrase - password for the private key
   true,                      // asciiArmored - output ASCII armored text
   true                       // integrityCheck - enable PGP packet integrity check
);

if (!successOnEncryption) {
   qie.error("PGP sign-and-encrypt operation failed");
}

var successOnDecryption = qie.pgpDecryptFileWithVerify(
   "/tmp/encryptedFoot.txt",
   "/tmp/decryptedFoot.dcm",
   "/tmp/pgpPrivate.key",
   "changeit",
   "/tmp/pgpPublic.key"
);

if (!successOnDecryption) {
   qie.error("PGP decrypt-and-verify operation failed");
}

// /tmp/decryptedFoot.dcm should now match the original /tmp/foot.dcm
// The PGP signature should also be successfully verified.


// Example: inputFile as File, outputFile as File,
// publicKey as File, privateKey as File
var inputFile = qie.newFile("/tmp/foot.dcm");
var encryptedFile = qie.newFile("/tmp/encryptedFoot-file.txt");
var decryptedFile = qie.newFile("/tmp/decryptedFoot-file.dcm");
var pgpPublicKeyFile = qie.newFile("/tmp/pgpPublic.key");
var pgpPrivateKeyFile = qie.newFile("/tmp/pgpPrivate.key");

var successOnEncryptionWithFiles = qie.pgpSignEncryptFile(
   inputFile,           // inputFile - File object to encrypt
   encryptedFile,       // outputFile - File object for encrypted output
   pgpPublicKeyFile,    // publicKey - File object for public key
   pgpPrivateKeyFile,   // privateKey - File object for private key
   "changeit",          // privateKeyPassphrase
   true,                // asciiArmored
   true                 // integrityCheck
);

if (!successOnEncryptionWithFiles) {
   qie.error("PGP sign-and-encrypt operation failed");
}

var successOnDecryptionWithFiles = qie.pgpDecryptFileWithVerify(
   encryptedFile,
   decryptedFile,
   pgpPrivateKeyFile,
   "changeit",
   pgpPublicKeyFile
);

if (!successOnDecryptionWithFiles) {
   qie.error("PGP decrypt-and-verify operation failed");
}


// Example: inputFile as String path, outputFile as String path,
// publicKey as byte[], privateKey as byte[]
var pgpPublicKeyBytes = qie.readFile("/tmp/pgpPublic.key");
var pgpPrivateKeyBytes = qie.readFile("/tmp/pgpPrivate.key");

var successOnEncryptionWithBytes = qie.pgpSignEncryptFile(
   "/tmp/foot.dcm",                 // inputFile - the file to be encrypted
   "/tmp/encryptedFoot-bytes.txt",  // outputFile - the encrypted output file
   pgpPublicKeyBytes,               // publicKey - byte[] public key
   pgpPrivateKeyBytes,              // privateKey - byte[] private key
   "changeit",                      // privateKeyPassphrase
   true,                            // asciiArmored
   true                             // integrityCheck
);

if (!successOnEncryptionWithBytes) {
   qie.error("PGP sign-and-encrypt operation failed");
}

var successOnDecryptionWithByteKeys = qie.pgpDecryptFileWithVerify(
   "/tmp/encryptedFoot-bytes.txt",
   "/tmp/decryptedFoot-bytes.dcm",
   pgpPrivateKeyBytes,
   "changeit",
   pgpPublicKeyBytes
);

if (!successOnDecryptionWithByteKeys) {
   qie.error("PGP decrypt-and-verify operation failed");
}