PGP decryption (file, string, bytes)
function PGPDecryptFile(inFile: string, outFile: string, keys: PGPKey | string | Array | null, options?: object): boolean;
function PGPDecryptString(message: string, keys: PGPKey | string | Array | null, options?: object): PGPMessageResult;
function PGPDecryptBytes(message: Uint8Array | number[] | string, keys: PGPKey | string | Array | null, options?: object): PGPMessageResult;NOTE
PGPDecryptFile(inFile, outFile, privateKeyFile) works in every version. Key objects, several keys, the options, and the string and byte array functions are available as of Syncplify Server! v8.12.0.
Decrypts an OpenPGP message, ASCII armored or binary. With VerifyWith, the message must also carry a valid signature by a key you trust.
keys is a private key object, the path of a private key file, or an array of either. The key the message is addressed to is used. A private key that is protected by a passphrase must be loaded with it: see PGPKey.FromFile and PGPKey.FromSecret.
PGPDecryptFile
Decrypts inFile into outFile.
| Return value | Explanation |
|---|---|
true | outFile was written. PGPLastResult() tells who signed the message. |
false | Decryption failed; PGPLastResult().ErrorMsg() says why |
outFile appears only when the message decrypted completely, its integrity check passed and, with VerifyWith, its signature verified. On failure nothing is written, and a file that already exists at outFile stays as it was.
// Shortest form: a private key file that has no passphrase
PGPDecryptFile("/data/in/report.csv.pgp", "/data/in/report.csv", "/data/keys/ours.privkey");// Require the signature of the partner: without it there is no output file
var ours = PGPKey.FromSecret("pgp-private-key", "pgp-passphrase");
var partner = PGPKey.FromFile("/data/keys/partner.asc");
if (PGPDecryptFile("/data/in/report.csv.pgp", "/data/in/report.csv", ours, { VerifyWith: partner })) {
Log.Info("accepted, signed on " + FormatDateTime("YYYY-MM-DD HH:mm", PGPLastResult().SignedAt()));
} else {
Log.Error("rejected: " + PGPLastResult().ErrorMsg());
}PGPDecryptString
Decrypts an armored message held in a string. Data() of the result is the content, as a string.
var ours = PGPKey.FromSecret("pgp-private-key", "pgp-passphrase");
var res = PGPDecryptString(ReadTextFile("/data/in/order.json.asc"), ours);
if (res.Ok()) {
var order = JSON.parse(res.Data());
Log.Info("batch " + order.batch);
} else {
Log.Error("cannot decrypt the order: " + res.ErrorMsg());
}PGPDecryptBytes
Decrypts a message held as bytes, armored or binary. message accepts the same kinds of value as the data of PGPEncryptBytes. Data() of the result is the content, as a byte array.
var ours = PGPKey.FromSecret("pgp-private-key", "pgp-passphrase");
var partner = PGPKey.FromFile("/data/keys/partner.asc");
var http = new HttpCli().Url("https://api.partner.example.com/v1/export").Timeout(30).Get();
if (!http.Ok() || http.StatusCode() !== 200) {
Log.Error("download failed");
Exit(1);
}
var res = PGPDecryptBytes(http.BodyAsBytes(), ours, { VerifyWith: partner });
if (res.Ok()) {
Log.Info("received " + res.Data().length + " bytes, signed by " + res.SignerFingerprint());
} else {
Log.Error("rejected: " + res.ErrorMsg());
}Options
| Option | Explanation |
|---|---|
VerifyWith | A public key, as a key object or a key file path, or an array of them. A valid signature by one of these keys becomes mandatory: an unsigned message, a signature by any other key or a signature that does not check out makes the call fail, and no content is returned. |
Passphrase | Opens a message that was encrypted to a passphrase. keys may then be null. |
// A message that was encrypted to a passphrase
var ok = PGPDecryptFile("/data/in/shared.csv.pgp", "/data/in/shared.csv", null,
{ Passphrase: GetSecret("shared-passphrase") });TIP
Passphrase also opens files that a remote client uploaded with OTFEAlgoOpenPGP: those are OpenPGP messages encrypted to the OTFE key.
Signatures
- Without
VerifyWith, a signed message still decrypts. The result reportsSigned()and theSignerKeyID()that the signature names, butSignatureValid()staysfalse: nothing was checked, and anybody can write any key ID into a signature. - A message that is signed but not encrypted, as
gpg --signwrites it, is accepted too. Passnullaskeystogether withVerifyWith;Encrypted()of the result is thenfalse. - A signature counts only when the signing key is neither expired nor revoked at the time of the check: see verification.
The stored file name
FileName() of the result is the name the sender stored inside the message. Treat it as a hint and never as a path to write to: the sender chooses it freely.
