Skip to content

PGP encryption (file, string, bytes) ​

ts
function PGPEncryptFile(inFile: string, outFile: string, recipients: PGPKey | string | Array, options?: object): boolean;
function PGPEncryptString(text: string, recipients: PGPKey | string | Array, options?: object): PGPDataResult;
function PGPEncryptBytes(data: Uint8Array | number[] | string, recipients: PGPKey | string | Array, options?: object): PGPDataResult;

NOTE

PGPEncryptFile(inFile, outFile, publicKeyFile) works in every version. Key objects, several recipients, the options, and the string and byte array functions are available as of Syncplify Server! v8.12.0.

Encrypts data so that only the holders of the recipients' private keys can read it. With SignWith the data is signed in the same step, so the recipient can also check that it comes from you.

recipients is a key object, the path of a public key file, or an array of either. Every recipient can decrypt the message.

PGPEncryptFile ​

Encrypts inFile into outFile. The output is ASCII armored unless Armor is false. The name and the modification time of the input file are stored inside the message, as gpg does.

Return valueExplanation
trueoutFile was written
falseEncryption failed and outFile was not written; PGPLastResult().ErrorMsg() says why
ts
// Shortest form: one recipient, given as a public key file
PGPEncryptFile("/data/out/payroll.csv", "/data/out/payroll.csv.pgp", "/data/keys/partner.asc");
ts
// Two recipients, signed, binary output as many partners expect
var partner = PGPKey.FromFile("/data/keys/partner.asc");
var ours    = PGPKey.FromSecret("pgp-private-key", "pgp-passphrase");

var ok = PGPEncryptFile("/data/out/payroll.csv", "/data/out/payroll.csv.gpg",
                        [partner, ours], { SignWith: ours, Armor: false });
if (!ok) {
  Log.Error("encryption failed: " + PGPLastResult().ErrorMsg());
}

TIP

Nobody but the recipients can decrypt a message, and that includes you. Add your own key to recipients, as above, when you need to read later what you sent.

PGPEncryptString ​

Encrypts a string. Data() of the result is the ASCII armored message, as a string.

ts
var partner = PGPKey.FromFile("/data/keys/partner.asc");
var order   = { batch: 1842, total: "15234.10" };

var res = PGPEncryptString(JSON.stringify(order), partner);
if (res.Ok()) {
  WriteTextToFile("/data/out/order.json.asc", res.Data());
} else {
  Log.Error("encryption failed: " + res.ErrorMsg());
}

PGPEncryptBytes ​

Encrypts binary data. data can be:

  • a byte array, as BodyAsBytes() of an HTTP response and ReadFileAsBytes() of a VFS return it,
  • a Uint8Array or an ArrayBuffer,
  • an array of whole numbers from 0 to 255, or
  • a string, which is taken as UTF-8.

Anything else throws. Data() of the result is a byte array holding a binary message, or an armored one with { Armor: true }.

ts
var partner = PGPKey.FromFile("/data/keys/partner.asc");
var res = PGPEncryptBytes(new Uint8Array([0x50, 0x4b, 0x03, 0x04]), partner);
if (res.Ok()) {
  Log.Info("encrypted message of " + res.Data().length + " bytes");
}

Options ​

OptionApplies toExplanation
SignWithallA private key, as a key object or a key file path, that signs the data inside the encryption
ArmorFile, Bytestrue for ASCII armored text, false for binary. Default true for files and false for byte arrays. Strings are always armored.
PassphraseallEncrypts to a passphrase instead of to keys, like gpg --symmetric. Pass null as recipients. Cannot be combined with keys or with SignWith.
Compressalltrue compresses the data before encrypting it
FileNameallFile name stored inside the message. Files store the name of the input file by default.
LegacyArmorallOnly for a receiver that runs a Syncplify product older than v8.12: see Scripts written before v8.12
ts
// A passphrase instead of keys
var ok = PGPEncryptFile("/data/out/payroll.csv", "/data/out/payroll.csv.pgp", null,
                        { Passphrase: GetSecret("shared-passphrase") });

When encryption fails ​

A recipient key that is expired, revoked or not able to encrypt makes the whole call fail, with a message that names the key and the reason. A recipient is never skipped silently.