如何使用pgp证书来验证文件的完整性

下载nginx服务器时提供了pgp证书,可以验证完整性。

pgp证书是这样生成的(个人理解):
1. 现找到文件的HASH码,类似于md5
2. 用私钥对其进行加密。
3. 将公钥暴露到网站上。
4. 将证书暴露到网站上。
5. 终端用户导入公钥到本地。
6. 终端用户使用公钥解密证书,拿出hash信息来验证文件的完整性。

参考列表:
http://www.jb51.net/LINUXjishu/156064.html
http://blog.sina.com.cn/s/blog_630bf12f0100nl7i.html


下面是验证文件 nginx-1.6.2.zip的过程:

wangxin@wangxin-VirtualBox:~/桌面$ ls
aalexeev.key  is.key     mdounin.key      nginx-1.6.2.zip.asc  sb.key
glebius.key   maxim.key  nginx-1.6.2.zip  nginx_signing.key
wangxin@wangxin-VirtualBox:~/桌面$ gpg --import *.key
gpg: 密钥 F5806B4D:公钥“Andrew Alexeev <andrew@nginx.com>”已导入
gpg: 密钥 6C7E5E82:公钥“Gleb Smirnoff <glebius@nginx.com>”已导入
gpg: 密钥 A524C53E:公钥“Igor Sysoev <igor@sysoev.ru>”已导入
gpg: 密钥 2C172083:公钥“Maxim Konovalov <maxim@FreeBSD.org>”已导入
gpg: 密钥 A1C052F8:公钥“Maxim Dounin <mdounin@mdounin.ru>”已导入
gpg: 密钥 7BD9BF62:“nginx signing key <signing-key@nginx.com>”未改变
gpg: 密钥 7ADB39A8:公钥“Sergey Budnevitch <sb@waeme.net>”已导入
gpg: 合计被处理的数量:7
gpg:               已导入:6  (RSA: 3)
gpg:              未改变:1
gpg: 没有找到任何绝对信任的密钥
wangxin@wangxin-VirtualBox:~/桌面$ gpg --verify nginx-1.6.2.zip.asc nginx-1.6.2.zip
gpg: 于 2014年09月16日 星期二 21时07分26秒 CST 创建的签名,使用 RSA,钥匙号 A1C052F8
gpg: 完好的签名,来自于“Maxim Dounin <mdounin@mdounin.ru>”
gpg: 警告:这把密钥未经受信任的签名认证!
gpg:          没有证据表明这个签名属于它所声称的持有者。
主钥指纹: B0F4 2533 73F8 F6F5 10D4  2178 520A 9993 A1C0 52F8
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 Bouncy Castle 库实现 PGP 文件加密的 Java 代码示例: ```java import java.io.*; import java.security.*; import java.security.interfaces.RSAPublicKey; import java.util.Iterator; import org.bouncycastle.bcpg.*; import org.bouncycastle.bcpg.sig.*; import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.bouncycastle.openpgp.*; import org.bouncycastle.openpgp.operator.*; import org.bouncycastle.openpgp.operator.bc.*; public class PgpFileEncryptor { private static final int BUFFER_SIZE = 4096; public static void encryptFile(String inputFile, String outputFile, String publicKeyFile) throws IOException, NoSuchProviderException, PGPException, NoSuchAlgorithmException, InvalidKeyException { // 加载 Bouncy Castle 提供程序 Security.addProvider(new BouncyCastleProvider()); // 读取公钥文件 InputStream keyIn = new FileInputStream(publicKeyFile); PGPPublicKeyRingCollection keyRingCollection = new PGPPublicKeyRingCollection(PGPUtil.getDecoderStream(keyIn)); keyIn.close(); // 查找加密公钥 PGPPublicKey encryptionKey = null; Iterator<PGPPublicKeyRing> keyRingIterator = keyRingCollection.getKeyRings(); while (encryptionKey == null && keyRingIterator.hasNext()) { PGPPublicKeyRing keyRing = keyRingIterator.next(); Iterator<PGPPublicKey> keyIterator = keyRing.getPublicKeys(); while (encryptionKey == null && keyIterator.hasNext()) { PGPPublicKey key = keyIterator.next(); if (key.isEncryptionKey()) { encryptionKey = key; } } } // 抛出异常,如果找不到加密公钥 if (encryptionKey == null) { throw new IllegalArgumentException("Can't find encryption key in key ring."); } // 创建输出流 OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile)); // 初始化 PGP 加密器和输出流 PGPEncryptedDataGenerator encryptedDataGenerator = new PGPEncryptedDataGenerator(new JcePGPDataEncryptorBuilder(PGPEncryptedData.CAST5).setWithIntegrityPacket(true).setSecureRandom(new SecureRandom()).setProvider("BC")); encryptedDataGenerator.addMethod(new JcePublicKeyKeyEncryptionMethodGenerator(encryptionKey).setProvider("BC")); OutputStream encryptedOutputStream = encryptedDataGenerator.open(outputStream, new byte[BUFFER_SIZE]); // 创建压缩器和输出流 PGPCompressedDataGenerator compressedDataGenerator = new PGPCompressedDataGenerator(PGPCompressedData.ZIP); OutputStream compressedOutputStream = compressedDataGenerator.open(encryptedOutputStream); // 创建字面数据包和输出流 PGPLiteralDataGenerator literalDataGenerator = new PGPLiteralDataGenerator(); OutputStream literalOutputStream = literalDataGenerator.open(compressedOutputStream, PGPLiteralData.BINARY, inputFile, new Date(), new byte[BUFFER_SIZE]); // 读取输入文件并加密 InputStream inputFileStream = new BufferedInputStream(new FileInputStream(inputFile)); byte[] buffer = new byte[BUFFER_SIZE]; int bytesRead; while ((bytesRead = inputFileStream.read(buffer)) != -1) { literalOutputStream.write(buffer, 0, bytesRead); } // 关闭流 literalOutputStream.close(); literalDataGenerator.close(); compressedOutputStream.close(); compressedDataGenerator.close(); encryptedOutputStream.close(); encryptedDataGenerator.close(); outputStream.close(); inputFileStream.close(); } } ``` 此代码使用 CAST5 对称加密算法进行加密,并使用 PGP 数据格式。它需要提供公钥文件路径、输入文件路径和输出文件路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值