I am relatively new to OpenPGP but I am currently trying to encrypt a message on a users device using OpenPGP.js and decrypt that message using OpenPGP PHP on my server. Both sides can encrypt and decrypt their own messages now - the problem: As soon as I encrypt a message on the client side, send it to the server and try to decrypt it there it doesn't work, same the other way around.
Are there any points that I can check what the problem is?
I tried using an online PGP en- and decryption service and it decodes the server side (OpenPGP PHP) messages without problems and gives a "checksum mismatch" error on client side encrypted messages so I think that it's probably the client side. I am using localstorage to store private and public keys on the client side and .asc files on the server side if that matters. The public key exchanging is working correctly.
SERVER SIDE: ENCRYPT:
$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;
DECRYPT:
$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;
CLIENT: ENCRYPT:
const options = {
data: string,
publicKeys: openpgp.key.readArmored(key).keys
}
openpgp.encrypt(options).then(ciphertext => {
encrypted = ciphertext.data;
callback(encrypted);
})
DECRYPT:
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);
});