PGP透明加解密研究原理图

 

 

最近一直在研究PGP NetShare ,他的驱动是基于文件夹透明加解密,下面是原理图

PGP透明加解密原理

PGP(Pretty Good Privacy)是一种加密和签名数据的标准,它使用了非对称加密算法和对称加密算法。下面是 PGP 加密解密的 Java 代码示例: PGP 加密过程: ```java import java.io.*; import java.security.*; import java.security.spec.InvalidKeySpecException; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; import java.util.Date; import org.bouncycastle.bcpg.*; import org.bouncycastle.openpgp.*; import org.bouncycastle.openpgp.bc.*; import org.bouncycastle.openpgp.jcajce.*; import org.bouncycastle.openpgp.operator.bc.*; import org.bouncycastle.util.io.*; public class PgpEncrypt { public static void encrypt(String inputFile, String publicKeyFile, String outputFile) { try { Security.addProvider(new BouncyCastleProvider()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); PGPCompressedDataGenerator compressedDataGenerator = new PGPCompressedDataGenerator(CompressionAlgorithmTags.ZIP); OutputStream compressedDataStream = compressedDataGenerator.open(baos); PGPLiteralDataGenerator literalDataGenerator = new PGPLiteralDataGenerator(); OutputStream literalDataStream = literalDataGenerator.open(compressedDataStream, PGPLiteralData.BINARY, inputFile, new Date(), new byte[4096]); FileInputStream publicKeyInputStream = new FileInputStream(publicKeyFile); byte[] publicKeyBytes = new byte[publicKeyInputStream.available()]; publicKeyInputStream.read(publicKeyBytes); publicKeyInputStream.close(); X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(publicKeyBytes); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec); PGPEncryptedDataGenerator encryptedDataGenerator = new PGPEncryptedDataGenerator(new JcePGPDataEncryptorBuilder(PGPEncryptedData.CAST5).setWithIntegrityPacket(true).setSecureRandom(new SecureRandom()).setProvider("BC")); encryptedDataGenerator.addMethod(new JcePublicKeyKeyEncryptionMethodGenerator(publicKey).setProvider("BC")); OutputStream encryptedDataStream = encryptedDataGenerator.open(literalDataStream, new byte[4096]); FileInputStream inputStream = new FileInputStream(inputFile); IOUtils.copy(inputStream, encryptedDataStream); inputStream.close(); encryptedDataStream.close(); literalDataStream.close(); literalDataGenerator.close(); compressedDataStream.close(); compressedDataGenerator.close(); FileOutputStream encryptedOutputStream = new FileOutputStream(outputFile); encryptedOutputStream.write(baos.toByteArray()); encryptedOutputStream.close(); } catch (Exception e) { e.printStackTrace(); } } } ``` PGP 解密过程: ```java import java.io.*; import java.security.*; import java.security.spec.InvalidKeySpecException; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; import org.bouncycastle.bcpg.*; import org.bouncycastle.openpgp.*; import org.bouncycastle.openpgp.bc.*; import org.bouncycastle.openpgp.jcajce.*; import org.bouncycastle.util.io.*; public class PgpDecrypt { public static void decrypt(String inputFile, String privateKeyFile, String privateKeyPassword, String outputFile) { try { Security.addProvider(new BouncyCastleProvider()); FileInputStream inputStream = new FileInputStream(inputFile); PGPObjectFactory objectFactory = new PGPObjectFactory(PGPUtil.getDecoderStream(inputStream), new BcKeyFingerprintCalculator()); Object object = objectFactory.nextObject(); PGPEncryptedDataList encryptedDataList = null; if (object instanceof PGPEncryptedDataList) { encryptedDataList = (PGPEncryptedDataList) object; } else { encryptedDataList = (PGPEncryptedDataList) objectFactory.nextObject(); } Iterator<PGPPublicKeyEncryptedData> iterator = encryptedDataList.getEncryptedDataObjects(); PGPPrivateKey privateKey = null; PGPPublicKeyEncryptedData encryptedData = null; while (privateKey == null && iterator.hasNext()) { encryptedData = iterator.next(); PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(IOUtils.toByteArray(new FileInputStream(privateKeyFile))); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PrivateKey privateKeyObj = keyFactory.generatePrivate(pkcs8EncodedKeySpec); if (encryptedData.getKeyID() == privateKeyObj.getEncoded()) { privateKey = new JcePGPKeyConverter().getPGPPrivateKey(encryptedData.getKeyAlgorithm(), privateKeyObj); } } if (privateKey == null) { throw new IllegalArgumentException("Can't find private key with ID " + encryptedData.getKeyID()); } InputStream decryptedDataStream = encryptedData.getDataStream(new JcePublicKeyDataDecryptorFactoryBuilder().setProvider("BC").build(privateKey)); PGPObjectFactory pgpObjectFactory = new PGPObjectFactory(decryptedDataStream, new BcKeyFingerprintCalculator()); Object message = pgpObjectFactory.nextObject(); if (message instanceof PGPCompressedData) { PGPCompressedData compressedData = (PGPCompressedData) message; pgpObjectFactory = new PGPObjectFactory(compressedData.getDataStream(), new BcKeyFingerprintCalculator()); message = pgpObjectFactory.nextObject(); } PGPLiteralData literalData = (PGPLiteralData) message; InputStream literalDataStream = literalData.getInputStream(); FileOutputStream outputStream = new FileOutputStream(outputFile); IOUtils.copy(literalDataStream, outputStream); outputStream.close(); literalDataStream.close(); decryptedDataStream.close(); inputStream.close(); } catch (Exception e) { e.printStackTrace(); } } } ``` 上面的代码使用了 Bouncy Castle 库来实现 PGP 加密解密。需要注意的是,加密和解密时需要用到公钥和私钥,这里的公钥和私钥是以文件形式存储的,需要读取文件并转换成相应的对象使用。另外,解密时还需要输入私钥的密码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值