OpenPGPJS:解密消息时出错:找不到对称加密的会话密钥包

时间:2017-03-17 17:34:43

标签: encryption openpgp.js

下面是我的简单代码(试图了解PGP的工作原理):

requirejs(['./openpgp'], function(openpgp) {

    openpgp.initWorker({
        path: './openpgp.worker.js'
    });

    openpgp.config.aead_protect = true;


    var fileContents = "Hello world!!";

    var encryptionCode = function(e) {
        var options;
        var encrypted;
        var myKey = {};

        myKey.pubkey = 
`-----BEGIN PGP PUBLIC KEY BLOCK-----

mQENBFjMFRwBCACtu5EG8GusNpr5O4wrb3fykjPegUh9KD/ErWrplmXcbtp6T/XH
FPkc5cyMme7PxxlcFy3fshMDPg0adTGi8/TPygmO9THtjHYqynge0hxDyafP39xE
2DKkyuLyV3KxNiUfr+NUB3MQHktqiS/pPK1MJUQdHjYPHax5QT1bMgffx36tM5vo
d3rdu4AMmLgnSlakmWus/hHMFUgXdp4Q0LWf7uVTXTwbnTkfLQOumj+1SiYV40BL
rxgiyW172uqy6/hqK9JgOvAUjWZiO9aNyexENzA8Y+Oyy3OWX91CKTYiE2eQKhFK
YlTf/AFgrnFd0wgAFkZ0dWj51x7kH1a1Ks8/ABEBAAG0DXJhdmlAdGVzdC5jb22J
ARwEEAECAAYFAljMFRwACgkQc0zV1uezYVSN8Qf+NYyVvKK2HbF+Qn7j+Ed0esK2
OS6xVqdlFfUSAUq1ER13ffc4Darj9Z2LJuOvQ4M11IYlorXGdtiay55oT/xoljDr
HHIYzlQ4h6OzOJP6IUxIS0nqtKZCLhvvEYL/vOb/AeOMSo/ZWHD1RjCnpv0vVXki
4U+kAI8+hfI/QQrVco83xWhH0yiO/HH0eHVLkAiAraqv0zTSX6ttpnYDyK+mwLHt
bgWzzjUi1KZrNtR2CLU8nCUjyHLau1bkZnysq7mAAn69VEVAQ833WnKwBG7uIsym
lcZiCtOtIq40kP1wysObkgfLkD37LW3VpzY2i4+wiWa7i8tBYMvm58BYTCLguA==
=Xm9f
-----END PGP PUBLIC KEY BLOCK-----
`;

        myKey.privkey = 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P7skxC1zHdL0C00BFxG3QpDMKlFqozE2al7rizU0TtpGPWeOFRjThLuG+4wE
k+zj0EYSxsye7JRkpktMaX0DKk5dtSJn8lcFPR425E+clCwAWyd6Nhf0xEGWbRtL
D2OUWU//FQpDNS74+1EWWiEvfkuz9hZDk19KmpB6amiu4nuyzQJvWB8+gNiSNPZs
af95YSRueaPUYwMFTEhXVZGj1ZX0fM6dfxi4BE/JM7QNcmF2aUB0ZXN0LmNvbYkB
HAQQAQIABgUCWMwVHAAKCRBzTNXW57NhVI3xB/41jJW8orYdsX5CfuP4R3R6wrY5
LrFWp2UV9RIBSrURHXd99zgNquP1nYsm469DgzXUhiWitcZ22JrLnmhP/GiWMOsc
chjOVDiHo7M4k/ohTEhLSeq0pkIuG+8Rgv+85v8B44xKj9lYcPVGMKem/S9VeSLh
T6QAjz6F8j9BCtVyjzfFaEfTKI78cfR4dUuQCICtqq/TNNJfq22mdgPIr6bAse1u
BbPONSLUpms21HYItTycJSPIctq7VuRmfKyruYACfr1URUBDzfdacrAEbu4izKaV
xmIK060irjSQ/XDKw5uSB8uQPfstbdWnNjaLj7CJZruLy0Fgy+bnwFhMIuC4
=Qx/K
-----END PGP PRIVATE KEY BLOCK-----`;

        options = {
            data: fileContents,
            publicKeys: openpgp.key.readArmored(myKey.pubkey).keys
        };

        openpgp.encrypt(options).then(function(ciphertext) {
            myKey.encrypted = ciphertext.data;
            console.log("Encrypted: ", myKey.encrypted);

            options = {
                message: openpgp.message.readArmored(myKey.encrypted),     // parse armored message
                privateKeys: openpgp.key.readArmored(myKey.privkey).keys[0],
                "password": "abcd@1234"
            };

            openpgp.decrypt(options).then(function(plaintext) {
                console.log("plaintext", plaintext);
                return plaintext.data;
            });

        });

    }(this);

});

我已从在线网站生成公钥和私钥。它仅用于测试目的。

我可以看到字符串已经正确编码(console.log)。但在解密时,它会抛出异常错误解密消息:找不到对称加密的会话密钥包

我做错了什么?请帮帮我。

1 个答案:

答案 0 :(得分:1)

最终,我发现,我必须解密我的私钥才能解密邮件。

以下代码帮助我完成了加密/解密演示:

options = {
    message: openpgp.message.readArmored(myKey.encrypted),
    privateKeys: openpgp.key.readArmored(myKey.privkey).keys[0].decrypt('abcd@1234'),
    "password": "abcd@1234"
};

openpgp.decrypt(options).then(function(plaintext) {
    console.log("Encrypted message got decrypted: ", plaintext.data);
    return plaintext.data;
});

感谢。