php - 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);
});
解决方案
推荐阅读
- php - php fopen() 没有使用 Symfony/Component/Process 类在同一进程中创建的文件的此类文件或目录
- c - 由中断服务例程切换的 volatile 变量的更改未反映在 main() 中
- linux - 使用内存映射IO时调用ioread函数有什么好处
- python - python pandas读取空格分隔的数据
- android - TypeError: null is not an object (evalating 'n.default.configureProps') React Native Jitsi Meet
- java - 如何在 Mockito 中模拟 CallableStatement
- python - 如何运行并行线程以在视频流的每一帧上应用函数?
- c - 在 for 循环之后的 Scanf 函数不起作用
- algorithm - 如何从代码中获取流程图或算法?
- c# - 当左 div 元素变长时,如何确保我的左 div 元素不会改变我的右 div 元素的位置?