有两个问题。这PGPDataValidationException
是由于使用不同的密码短语进行加密和解密造成的。如果您使用了正确的密码,那么您会发现 Bouncy Castle 示例代码的功能并不完整。
触发器可能不是您想要的。调用pgp_sym_encrypt
应该看起来更像这样:
create or replace function project_function()
returns trigger as
$BODY$
begin
IF (TG_OP = 'UPDATE') THEN
NEW.title = armor(pgp_sym_encrypt(NEW.title, 'my-secret-passphrase', 'cipher-algo=aes256, compress-algo=2' ));
return NEW;
END IF;
end;
$BODY$
language plpgsql;
to 的三个输入参数pgp_sym_encrypt
是要加密的文本、从中派生密码密钥的密码短语和选项。在您的问题中,您省略了密码短语。
其次,BouncyCastle 示例代码假定纯文本已被压缩。我已将 RFC1950 压缩 (ZLIB) 添加到pgp_sym_encrypt
.
通过对触发器的这些更改,我得到:
postgres=# update projects set title = 'My secret compressed title.';
UPDATE 1
postgres=# \t off
postgres=# select * from projects;
title
-----BEGIN PGP MESSAGE-----
ww0ECQMCuN3MyfrWhBt50lcBGbUtjOlTBxGpAFCl7aYEybhhXRJodDsikWxdLmOsXnE6vWr9mwd7
dGy7N1eE5VFmwI5N29eCNhEvG5U4YmVC7fV1A1sBeoJMtsO/nz2mi2jbFiZHlzo=
=s6uI
-----END PGP MESSAGE-----
(1 row)
postgres=#
将其输入到 Java 程序中:
String value = "-----BEGIN PGP MESSAGE-----\n"
+ "\n"
+ "ww0ECQMCuN3MyfrWhBt50lcBGbUtjOlTBxGpAFCl7aYEybhhXRJodDsikWxdLmOsXnE6vWr9mwd7\n"
+ "dGy7N1eE5VFmwI5N29eCNhEvG5U4YmVC7fV1A1sBeoJMtsO/nz2mi2jbFiZHlzo=\n"
+ "=s6uI\n"
+ "-----END PGP MESSAGE-----\n";
String key = "my-secret-passphrase";
byte[] byt = value.getBytes(StandardCharsets.UTF_8);
Security.addProvider(new BouncyCastleProvider());
System.out.println(new String(ByteArrayHandler.decrypt(byt, key.toCharArray()), StandardCharsets.UTF_8));
产生输出:
My secret compressed title.
完全如愿。
如果您不想在加密之前压缩纯文本,那么您可以查看示例 PBEFileProcessor 因为它同时处理压缩和未压缩的数据,或者您可以只使用以下代码:
public static byte[] decrypt(
byte[] encrypted,
char[] passPhrase
) throws IOException, PGPException {
JcaPGPObjectFactory pgpF = new JcaPGPObjectFactory(PGPUtil.getDecoderStream(new ByteArrayInputStream(encrypted)));
// Find the encrypted data list. The first object might be a PGP marker packet, or the actual data list
PGPEncryptedDataList enc;
Object o = pgpF.nextObject();
if (o instanceof PGPEncryptedDataList) {
enc = (PGPEncryptedDataList) o;
} else {
enc = (PGPEncryptedDataList) pgpF.nextObject();
}
// Do the decryption
PGPPBEEncryptedData pbe = (PGPPBEEncryptedData) enc.get(0);
InputStream clear = pbe.getDataStream(new JcePBEDataDecryptorFactoryBuilder(
new JcaPGPDigestCalculatorProviderBuilder().setProvider("BC").build()).setProvider("BC").build(passPhrase)
);
// Process the decrypted data. It may be compressed, or it may be literal
JcaPGPObjectFactory pgpFact = new JcaPGPObjectFactory(clear);
o = pgpFact.nextObject();
if (o instanceof PGPCompressedData) {
// Need to decompress the data
PGPCompressedData cData = (PGPCompressedData) o;
pgpFact = new JcaPGPObjectFactory(cData.getDataStream());
o = pgpFact.nextObject();
}
// We should have the literal data now, so convert it into bytes
PGPLiteralData ld = (PGPLiteralData) o;
return Streams.readAll(ld.getInputStream());
}
最后,在 postgresql 中解密时,您不需要指定纯文本是否被压缩,也不需要指定如何加密,因为 PGP 数据指定了这一点,因此您可以这样做:
select pgp_sym_decrypt(dearmor(title), 'my-secret-passphrase') from projects;