首页 > 解决方案 > OpenPGP 加密在系统之间不起作用

问题描述

我对 OpenPGP 比较陌生,但我目前正在尝试使用OpenPGP.js在用户设备上加密消息,并在我的服务器上使用OpenPGP PHP解密该消息。双方现在都可以加密和解密自己的消息 - 问题:一旦我在客户端加密消息,将其发送到服务器并尝试在那里解密它不起作用,反之亦然。

有什么可以检查问题的地方吗?

我尝试使用在线 PGP 加密和解密服务,它可以毫无问题地解码服务器端(OpenPGP PHP)消息,并在客户端加密消息上给出“校验和不匹配”错误,所以我认为它可能是客户端。如果这很重要,我正在使用 localstorage 在客户端存储私钥和公​​钥,在服务器端存储 .asc 文件。公钥交换工作正常。

服务器端:加密:

$key = OpenPGP_Message::parse(OpenPGP::unarmor($public, "PGP PUBLIC KEY BLOCK"));
$data = new OpenPGP_LiteralDataPacket($string, array('format' => 'u', 'filename' => 'stuff.txt'));
$encrypted = OpenPGP_Crypt_Symmetric::encrypt($key, new OpenPGP_Message(array($data)));
$enc = OpenPGP::enarmor($encrypted->to_bytes(), "PGP MESSAGE");
$enc =  wordwrap($enc, 64, "\n", 1);
return $enc;

解密:

$keyEncrypted = OpenPGP_Message::parse(OpenPGP::unarmor($>private, 'PGP PRIVATE KEY BLOCK'));
        $text = "";
        foreach($keyEncrypted as $p) {
            if(!($p instanceof OpenPGP_SecretKeyPacket)) continue;
            $key = OpenPGP_Crypt_Symmetric::decryptSecretKey($pass, $p);
            $msg = OpenPGP_Message::parse(OpenPGP::unarmor($encrypted, 'PGP MESSAGE'));
            $decryptor = new OpenPGP_Crypt_RSA($key);
            $decrypted = $decryptor->decrypt($msg);

            $text = $decrypted->packets[0]->data;
        }
        return $text;

客户:加密:

const options = {
                data: string,
                publicKeys: openpgp.key.readArmored(key).keys
            }
            openpgp.encrypt(options).then(ciphertext => {
                encrypted = ciphertext.data;
                callback(encrypted);
            })

解密:

const privKeyObj = openpgp.key.readArmored(storage.get("crypt.private")).keys[0]
        await privKeyObj.decrypt(storage.get("crypt.pass"))

        const options = {
            message: openpgp.message.readArmored(encrypted),     // parse armored message
            publicKeys: openpgp.key.readArmored(storage.get("crypt.public")).keys,    // for verification (optional)
            privateKeys: [privKeyObj]                            // for decryption
        }

        openpgp.decrypt(options).then(plaintext => {
            console.log(plaintext.data);
            callback(plaintext.data);
        });

标签: phpencryptionmessageopenpgpopenpgp.js

解决方案


推荐阅读