const openpgp = require('openpgp');

const fs = require("fs");

var privateKeyArmored = "";
var publicKeyArmored = "";
const passphrase = `123456`;

const encryptedAndSignedMessage = `-----BEGIN PGP MESSAGE-----
Version: BCPG v1.65

hQEMA/At0Xo4+2SSAQf8DyG0Py7KXj6dQX+80Dlwo/XwiuRhZOlUy31BQyMO5T/N
kBIRqlKIpjr03b7UTEb8LDrg+rBSEh2BBoW98BrdLiNTXMRgrmHPkmwVzClUVV4F
2X54Lf4KhhQfFbZePB42ItkkEj+FV2h/sMNjIXwEDon0Xkq3hiwzOSDCjWi/DOV6
lfDLGJo7KZ5x7+eUr+mRstQ2TDQrB2xx3/CLY6FwNiT0gjo5/YpvFm5PDn7Fbi7B
uzooXp+KCDVAMwrUqWQ8GgLK5EMyM6m+wHpi/9X9P0daVv4yXDwrGuhroYJwglpl
VMSXkQyhSZDh22cvNCvzfRlg56zG+yd/OcizfHELuYUBDAMPF0yE3OShgwEIAJyT
isG8uOoMzXRzkKVi8uCiB1P0KXTErAUdUridvBr4xBmBLVL3XN821nl6ccAyfUEp
qhMmYVDkAt6KRcBGDI2sATXLeYZ2wZU/4VIklnyVPtJh8+1Ab25+BhcdVBOMJK1h
FFPIl/9cb3FpIx7Zh0Wo+55HSf/a+Eoxp82WGM2hKq3PURNY8nMaDBpKWeqVjoSO
C1KE5+pLeFNUoizVdPrTxygT658vRQKvwrcvFTPIrkHhuHWus+TtDKmOvhs4yo1o
C43wRbCc6b+eVL93VjsZlu4FGZ/NQlzGkLBMYUa7HYRAh30Jd/9OlzSAlHx4YrgR
ILyyXSkoam0kdHflyOKFAQwDkTdJX0fDaGUBCACOZNM3t+jvDz4rqc2gvX+gdnOf
uNpjemXn5M8SmaYVzs5xZ1j2JzivNkv9JzhRpdaqGl5A836VbYjw8lnrp07rZMRm
e9VheT++BPta2CcYq2mge6pLi6UPpIlpKNfv6FxWVU0ESW362Px3jHAyM7RE5Ggf
DUGk0fwi7kTrTOnkRmtR9EpgaSng4YRq6kHXVcKXXhD2XxeYKMeYGO4hvV8CJ9nA
RffjOW2csOiV30l4ecvtczEe/Q/ch1QZK3prJt80qfhA8bxfmfYjP++A1ol7w/Lt
qVR6P4aWkXgDy8FjPK+apUwl8C7LUQBKnrohGBDAyVBCcjb1RfY2IDpY+xA3ycDK
sv83gm1reqzdrFttHeiObwME1QYwk1nclqchhe6Ph4lxcOu6JGjtDQTlOZTjt2Gu
m7Tn4N80PhCh4e9Nd2JdgDO6Gc8dYOJ8HdjQy1qrp5pUhjW6aWHlRdeqIx3F+j3e
Uk2HJX8eZf2fTBBXTJMNfzMxb9oCoerL/mj/TF0EN/McOenBnaVBMs0Dji+zMcZk
1LxeLy3KGnMGTFfpDKoKjLqMidH04RKSNnwbcAVwv9Opn4XU1fjCIe5d0qe+YcdQ
Ef+drWAm5eytxWE/pSom2sCZDB/58MSOwoQlXZ1lEHbx5dwqfnwQ8KkLxcfW0DVT
F4bIActCYwiFXbX9cpjNSbtq8s7IGNGeG6kg8aPQLUPyyf3xTrxtrCtinP+yiKR+
HNO4/Gfk4wiozcxH+VCCUPxvZhevsQBpq5pR5xnUXZmMWzTR4xd8vrX7zaXdDM1O
9bTwf1nOLuOKU8JXmXdzq3kkfRi8/Vd6N6KD8jyIAKRbQFpMaDKzdQ1iJ+fZ9kT5
JRLHbNzQcjMS4g==
=AwHJ
-----END PGP MESSAGE-----`;

loadPGP();

(async () => {
    const publicKey = await openpgp.readKey({ armoredKey: publicKeyArmored });

    const privateKey = await openpgp.decryptKey({
        privateKey: await openpgp.readKey({ armoredKey: privateKeyArmored }),
        passphrase
    });

    const message = await openpgp.readMessage({
        armoredMessage: encryptedAndSignedMessage // parse armored message
    });

    let configObj = { allowUnauthenticatedMessages: true, allowInsecureDecryptionWithSigningKeys: true };

    // decryption will fail if all signatures are invalid or missing
    const { data: decrypted, signatures } = await openpgp.decrypt({
        message,
        privateKeys: privateKey, // for decryption
        expectSigned: true,
        publicKeys: publicKey, // for verification (mandatory with expectSigned=true)
        config: configObj
    });
    console.log(decrypted); // 'Hello, World!'
})();


async function loadFile(fileName) {
    var dataEncrypt = "";
    try {
      dataEncrypt = fs.readFileSync(fileName, 'utf8')
      console.log("Data Encrypt: " + dataEncrypt);
    } catch (err) {
      console.error(err)
    }
    return dataEncrypt;
    console.log("Load data successfully!!!");
}

async function loadPGP() {
    privateKeyArmored = fs.readFileSync('partner-sec.asc', 'utf8');
    publicKeyArmored = fs.readFileSync('mservice-pub.asc', 'utf8');
    console.log("Load PGP key successfully!!!");
}
