问题
我正在使用Bouncy Castle java使用PGP加密文件。该程序旨在运行到SAP PI 7.0。当我对解密程序运行PI的输出(加密文件)时,我得到一个空的解密文件,没有错误。
因此,我尝试在Eclipse中使用相同的参数加密相同的源文件。这一次,我能够正确地解密输出,以检索我的原始文件。同样的代码,同样的输入。
在某些时候,我注意到PI输出比Eclipse的输出短几个字节 (通常是32个字节)。我想这就是为什么解密输出为空而没有错误的原因。我不知道加密文件中可能缺少什么,因为两个PGP消息具有相同的形状。
有什么建议我能解决它吗?PGP消息中是否有丢失的部分?在PI环境中,我是否错过了库或配置点?
技术细节
- 弹跳城堡**
- 版本1.47
- JDK:1.3
- Jar可在http://polydistortion.net/bc/download/下载:jce-jdk13-147.jar,bcpg-jdk13-147.jar
- 当地环境**
- 简体中文(简体)
- 操作系统:Windows 7 SP1
- JDK版本:1.3
- SAP PI环境 *
- PI版本:7.0
- Java运行时:Java(TM)2运行时环境标准版(版本= 1.4.2_14-b05)
- Java虚拟机:Java HotSpot(TM)64位服务器VM(版本= 1.4.2_14-b05)
- 操作系统:Windows 2003(版本= 5.2,arch = amd64)
- JDK版本:1.3
DSA密钥对
- 公钥**
- ----开始PGP公钥块----
版本:BCPG v1.47
mQGhBFUMSHMRBADrMi7kuXATsOdtvG2TBhe + U + SRp7fCEJKhd4Xs27HsNZ82c954 sHXiAXT1g6chdD + R8ZYWL8LQ2Jsu69YpzdRWgLkR4BK0mvKtrW84gVjRC9jgCgmw VETvFU3b5wzJYOSX0@OhCQK7F8bptrNAhhmnR + cTfYnzRLnXlwTKG71KBwCg8nbw dccq7kyASZxbWz11d +3yvHEEE AL8THYETkpsGu/AWN6P3ffl6qXpX7LLPnccrnSHT M4MKuRVnRZUlBRAK4koWwkYgzmYNKcbJLk85 0814Im/Tl2 + 1 + HpYPuX7Cp + Hqyo2Y3VVZMA +1LlU + uXsMRQ6ce8GW7iuo31EVGxuSichgIb6G4k 9zfR3J + IP3kiGeLcd akZuA/jgGXGkCsPJwRXefIn79OyMMjGbb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ixmq6aXDozsQyMutAjg5sJOLdhW9dQN/@C9EVNnWF/Q/COB19QaX/ayyzQ8 + IJUwwLU4vajZopYq9Gg6H9WksAFQWQOfuhrvf6rAzjSeSR mjecXm +0Rkk 06waxP3ZvAgjqQGiQ + 3PS6DSZNDpbT2x1j8Fl92m7hr5Bo5d73CGrl Qeyh6VxEviSw2bLSLcQvvB + KYBCxY +6t445/r4ONR gFZPT8YlAnQv8fw + otkHjnE fQZxVdb0rtMQ5uGVilLKSjOirjPKBApg5d3m0rOtpQvLyF4Fz@xxM88aaeAP4RFg 7JWcGx1zdk3SQKM9p9bZ/pvNFiorJPTLgw9j1 + FnQasegnqJd8n6GetH + BnWDl BL YnFwo9RDIkw5v + uG9ZzWQfSgVg3QrWoxQQ9kCs0ro89zLLitdkfXM9aQRJ/dhYKj pJrLglg jGBDbG6Vv/JmxFdhe5bRRHBSFGkNfQQFTQ1//VyIPPZZtGOx317s4QmEo RDxLHgH1fzr41LsXvX2PpbLHXxTpIY7J + pe6yZlmzeXLLKF0ZqXnjkg2MX2x/+ Se IJn8ufW3q8LgBCcqiEYEGBECAAYFAlUMSHUACgkQ/23CaO3j0CeVYACfcP5ocfsB fCDlTfnmAv + CISHwpdcAn3TSeD6ftizjUcNBxQwnjqfKN3T8
= CPWK - ----结束PGP公钥块----
- 私钥(密码:“测试12345!")**
- ----开始PGP私钥块----
版本:BCPG v1.47
lQHoBFUMSMRBADrMi7kuXATsOdtvG2TBhe + U + SRp7fCEJKhd4Xs27HsNZ82c954 sHXiAXT1g6chdD + R8ZYWL8LQ2Jsu69YpzdRWgLkR4BK0mvKtrW84gVjRC9jgCgmw VETvFU3b5wzJYOSX0@OhCQK7F8bptrNAhhmnR + cTfYnzRLnXlwTKG71KBwCg8nbw dccq7kyASZxbWz11d +3yvHEEE AL8THYETkpsGu/AWN6P3ffl6qXpX7LLPnccrnSHT M4MKuRVnRZUlBRAK4koWwkYgzmYNKcbJLk85 0814Im/Tl2 + 1 + HpYPuX7Cp + Hqyo2Y3VVZMA +1LlU + uXsMRQ6ce8GW7iuo31EVGxuSichgIb6G4k 9zfR3J + IP3kiGeLcd akZuA/jgGXGkCsPJwRXefIn79OyMMjGbb0FwLJLUat5M2XDoYr2AsZ6xKsybdJBM XFbwOsn6yNqhcK gIHn1kA/WgwM6L4AQQrw2KN/kue/p + qmcws2SdBzlmIcLcWbwl lQlrbp3UHxGW2oagv5myocardia@BUElF1HOQaV7tYi2mYvRAW3P8oQ/gkDAjEsU2VRQwZQ YM1E4ZLx + XLpfEac9ewO5vxMJEEWnxymXYM1E4ZLx 3T6xlTkWAQC/nlWr5tpvckYiQgr5F3hn 1lAW/Wu3UPvjA4a0DnRlc3RAZ21haWwuY29tiEYEExECAA@FAlUMSHQACgkQ/23C aO3j0CcKuACeP995b0xqEvCsWA3JbodGkLSh0RIAnRPPLpmzcC + FsyXyMcL9 RFLy LY/bnQZABFUMSHQQEAD/////////////////8kP2qIhaMI0xMZii4DcHNEpAk4IimfMdAIL vqY7E5siUUoIeY40BN3vlRmzzTpDG zArCm3yXxQ3T + E1bW1RwkXkhbV2Yl5 + xvRM QummN +1rC/9ctvQGt +3uOGv7Womfpa6fJBF8Sx/@SShmUezkWz3CAHy4oWO/BZja SDYcVdOaaRY/qP0kz1 + DZV0j3KOtlhxi81YghVK7ntUpB3CWl@1nDDVOSryYBPF0 bAjKGCF8MpBeRi42zjvjnncsGA6GA5sng6LsB6KPtcVd8G9MUsneK8v2lVg XGDmV SXzqlWrlFdImGJj6BRAVco5aiqrELa0zFw0EUHozqFUhq98cumTs + 4UEWNvvCorq cVddBgx9s5cPhabh5Mer9a6M2wkz1x6MlOBKJWGdzuPSJhrS7mvxL/oG2YoIZNh2 AnM + yGpkUh8rGBd7IAy74RdXemFdbHcJiMC62UbiCOJPoHTlqzFD21v84P0QjkuC 0SCpIQgBGnI8 EqeH5teIcZoQvbpbJpnDJxhq90I8GpRoNLYVC9olg + nKKtRM6Nu7wtsE3o75Lo78FB ++ yqYofE FlHTm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Unk02nQurC8 + L1/6xeoJv6/tCT00cHkWf + Fem3FSATZUA2yy4JT1yK7KFNgNSRF5b + k MLNiz4VMW + 5ym31YqVguD9JmD02qkgX4U5ATLnwvGXFaW4JRsODbX7I/08tRgcUe x + fURZwJF86rt@JxrCKJHWTd0GYb1/iZlRs0D6AZdlwKN3 + 6Dge/04d9g9BQ9giq HOmXFk5jE22wTOzq9VUES6HzBHCpq32GxYdQ7/4ZTLbQpw3whPsHcAeNmidxsGyG QmP/Xs757dnDsAmL9TQ1Ph1Uftc xuPHiPIsMBM44VMglBacfOnEDCTl8hHiNOKl + V + 8sg5PBd2OTJVKC95Oe4NIgYo1MCNZhDRNCyut1h1eMHkcQZfCxXvPTOBDR7HXz lcfHnxwkYQC3mSK5hMPT3IYSSihY6gRUrD62jj4w6X5HhAo5sRjcisT0lXYjtKx3 PDgfqHQU0CnmKqovVIwUuYYNn9aYM4hGBBgRAgAGBQJVDEh1AAoJEP9twmjt49An IWAAn3D + aHH7AXwg5U355gL/giEh8KXXAJ900ng + n7Ys41HDQcUMJ46nyjd0/A ===
= kuac
- ----结束PGP私钥块-----
加密输出
- Eclipse**
- ----开始PGP消息-----
版本:BCPG v1.47
hQQOA0fjQmjVGrtrEA/+ JTCsjY1frycHtMDeTPYfmsRX/nnVuTTvB2c9ZkK3GJq + TF2kMXhG4x j8XO5c7DtIehKwIAMvvirpX456mnObGMUQMtguXvsCMuY766SmgRx6 GebHU0v166xkN/9ZmHBs@xti7P1qFAJe4XVAhNXb + odUGrnSPNfQY4gUvvP6DM7a azaCQygElNfhBP26XODKD8h2xJqyNxb@9HodEmVevrhvBPQFZvAcA0KLFX1hvWG9Us D4eOrZp0UL4KoabDtHcTvUILRqmgslX9oMMrEoPdEdkvWJTeZMf8Yf8S0aNoUtAB Rpi49n2kTiQcX5x313stxSaP9mcmyKsMAlXn0BgnWUS/w4GGGB6GW ki1OirHi2DG 3Q0LOMWlYrLinXq9C +3cHCSiBYhF6Z + D74L97YGNMtHPfZ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IJERXffT + BL86S8hmhLXSkhQPmnRt ijYx6cAJNizLtzJzgrGbjL7o4s3Q8 + QFF2rqL3GcdySzdxM61l wVGYJ1MFyJqBCN ibLbz3u +8wovZ5F/0kOt54X4bG7YF31UrlcFam8b4cpHa3vdKJXRfXRwGRwG D82PCWo HDn33MeIkYOdQtBNDkNMl2DF6GcAaD2Z6R4F1S/h + ywW6HggD1OhKPiCu/xn8Ojy WWR3k Liqia0Tdz7dlBFBHxcNQM +1NtiZ9HAU + D71Evyx3R/ezBUrDiHnnZSujk1E Pvh1l5DH0vN788 VwDOBjKZRVx7Y7xlWl/oO6wlpLZ/SK0XN + x3jjeReDjnD7FPU/RtBddqgbfMap80VQ +5QXXG4za JKrkSj3jW4zW + W4 + msbMfS5MGzkFLkIHh9i5WCI JYsD31JQOb + rTr/UlCJWBdIC9FSlIKlvMPpHmBB + v853O/SYaBm3 Gme3or/xB5y9 Y58vK3M9bI9848qkivG2Q2XO + B4URSOTr8GTh8qZKDOYQ12kaWhC3Gc7rx M + N04c 6itSVNMX9ohUgLMidLShx5HcWPcdnb396nQCtP0gyDS30ksB6AJzvhVaNTlcyGYy bysww@n5fLq + AAI5vBdDiUFjTOX051y/jDKG1B26txgQLo0gCHiQnYYhtHQXe2kev wGSuO2 + 0v41 e9每单位=
= OL09
- ---结束PGP消息----
Java代码
- BcPGP. java**
public class BcPGP {
private static final int BUFFER_SIZE = 1 << 16; // should always be power of
// 2
private static final int KEY_FLAGS = 27;
private static final int[] MASTER_KEY_CERTIFICATION_TYPES = new int[] { PGPSignature.POSITIVE_CERTIFICATION,
PGPSignature.CASUAL_CERTIFICATION, PGPSignature.NO_CERTIFICATION, PGPSignature.DEFAULT_CERTIFICATION };
public static PGPPublicKey readPublicKey(InputStream in) throws IOException, PGPException {
PGPPublicKeyRingCollection keyRingCollection = new PGPPublicKeyRingCollection(PGPUtil.getDecoderStream(in));
//
// we just loop through the collection till we find a key suitable for
// encryption, in the real
// world you would probably want to be a bit smarter about this.
//
PGPPublicKey publicKey = null;
//
// iterate through the key rings.
//
Iterator rIt = keyRingCollection.getKeyRings();
while (publicKey == null && rIt.hasNext()) {
PGPPublicKeyRing kRing = (PGPPublicKeyRing) rIt.next();
Iterator kIt = kRing.getPublicKeys();
while (publicKey == null && kIt.hasNext()) {
PGPPublicKey key = (PGPPublicKey) kIt.next();
if (key.isEncryptionKey()) {
publicKey = key;
}
}
}
if (publicKey == null) {
throw new IllegalArgumentException("Can't find public key in the key ring.");
}
if (!isForEncryption(publicKey)) {
throw new IllegalArgumentException("KeyID " + publicKey.getKeyID() + " not flagged for encryption.");
}
return publicKey;
}
public static PGPSecretKey readSecretKey(InputStream in) throws IOException, PGPException {
PGPSecretKeyRingCollection keyRingCollection = new PGPSecretKeyRingCollection(PGPUtil.getDecoderStream(in));
//
// We just loop through the collection till we find a key suitable for
// signing.
// In the real world you would probably want to be a bit smarter about
// this.
//
PGPSecretKey secretKey = null;
Iterator rIt = keyRingCollection.getKeyRings();
while (secretKey == null && rIt.hasNext()) {
PGPSecretKeyRing keyRing = (PGPSecretKeyRing) rIt.next();
Iterator kIt = keyRing.getSecretKeys();
while (secretKey == null && kIt.hasNext()) {
PGPSecretKey key = (PGPSecretKey) kIt.next();
if (key.isSigningKey()) {
secretKey = key;
}
}
}
// Validate secret key
if (secretKey == null) {
throw new IllegalArgumentException("Can't find private key in the key ring.");
}
if (!secretKey.isSigningKey()) {
throw new IllegalArgumentException("Private key does not allow signing.");
}
if (secretKey.getPublicKey().isRevoked()) {
throw new IllegalArgumentException("Private key has been revoked.");
}
if (!hasKeyFlags(secretKey.getPublicKey(), KeyFlags.SIGN_DATA)) {
throw new IllegalArgumentException("Key cannot be used for signing.");
}
return secretKey;
}
/**
* Load a secret key ring collection from keyIn and find the private key
* corresponding to keyID if it exists.
*
* @param keyIn
* input stream representing a key ring collection.
* @param keyID
* keyID we want.
* @param pass
* passphrase to decrypt secret key with.
* @return
* @throws IOException
* @throws PGPException
* @throws NoSuchProviderException
*/
public static PGPPrivateKey findPrivateKey(InputStream keyIn, long keyID, char[] pass) throws IOException, PGPException,
NoSuchProviderException {
PGPSecretKeyRingCollection pgpSec = new PGPSecretKeyRingCollection(PGPUtil.getDecoderStream(keyIn));
return findPrivateKey(pgpSec.getSecretKey(keyID), pass);
}
/**
* Load a secret key and find the private key in it
*
* @param pgpSecKey
* The secret key
* @param pass
* passphrase to decrypt secret key with
* @return
* @throws PGPException
*/
public static PGPPrivateKey findPrivateKey(PGPSecretKey pgpSecKey, char[] pass) throws PGPException {
if (pgpSecKey == null)
return null;
PBESecretKeyDecryptor decryptor = new BcPBESecretKeyDecryptorBuilder(new BcPGPDigestCalculatorProvider()).build(pass);
return pgpSecKey.extractPrivateKey(decryptor);
}
/**
* decrypt the passed in message stream
*/
public static void decryptFile(InputStream in, OutputStream out, InputStream keyIn, char[] passwd) throws Exception {
Security.addProvider(new BouncyCastleProvider());
in = org.bouncycastle.openpgp.PGPUtil.getDecoderStream(in);
PGPObjectFactory pgpF = new PGPObjectFactory(in);
PGPEncryptedDataList enc;
Object o = pgpF.nextObject();
//
// the first object might be a PGP marker packet.
//
if (o instanceof PGPEncryptedDataList) {
enc = (PGPEncryptedDataList) o;
} else {
enc = (PGPEncryptedDataList) pgpF.nextObject();
}
//
// find the secret key
//
Iterator it = enc.getEncryptedDataObjects();
PGPPrivateKey sKey = null;
PGPPublicKeyEncryptedData pbe = null;
while (sKey == null && it.hasNext()) {
pbe = (PGPPublicKeyEncryptedData) it.next();
sKey = findPrivateKey(keyIn, pbe.getKeyID(), passwd);
}
if (sKey == null) {
throw new IllegalArgumentException("Secret key for message not found.");
}
InputStream clear = pbe.getDataStream(new BcPublicKeyDataDecryptorFactory(sKey));
PGPObjectFactory plainFact = new PGPObjectFactory(clear);
Object message = plainFact.nextObject();
if (message instanceof PGPCompressedData) {
PGPCompressedData cData = (PGPCompressedData) message;
PGPObjectFactory pgpFact = new PGPObjectFactory(cData.getDataStream());
message = pgpFact.nextObject();
}
if (message instanceof PGPLiteralData) {
PGPLiteralData ld = (PGPLiteralData) message;
InputStream unc = ld.getInputStream();
int ch;
while ((ch = unc.read()) >= 0) {
out.write(ch);
}
} else if (message instanceof PGPOnePassSignatureList) {
throw new PGPException("Encrypted message contains a signed message - not literal data.");
} else {
throw new PGPException("Message is not a simple encrypted file - type unknown.");
}
if (pbe.isIntegrityProtected()) {
if (!pbe.verify()) {
throw new PGPException("Message failed integrity check");
}
}
}
public static void encryptFile(OutputStream out, InputStream is, PGPPublicKey encKey, boolean armor,
boolean withIntegrityCheck, int compressType, int encryptType) throws IOException, NoSuchProviderException,
PGPException {
System.out.println("Add BouncyCastleProvider");
Security.addProvider(new BouncyCastleProvider());
if (armor) {
out = new ArmoredOutputStream(out);
}
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
PGPCompressedDataGenerator comData = new PGPCompressedDataGenerator(compressType);
// setup temp file to hold source data - this is necessary when using
// PGPUtil.writeFileToLiteralData which uses a file
System.out.println("Creating a temp file...");
File tempfile = File.createTempFile("pgp", null);
FileOutputStream fos = new FileOutputStream(tempfile);
int read = 0;
byte[] inBytes = new byte[1024];
while ((read = is.read(inBytes)) != -1) {
fos.write(inBytes, 0, read);
}
fos.flush();
fos.close();
System.out.println("Temp file created at ");
System.out.println(tempfile.getAbsolutePath());
PGPUtil.writeFileToLiteralData(comData.open(bOut), PGPLiteralData.BINARY, new File(tempfile.getAbsolutePath()));
comData.close();
BcPGPDataEncryptorBuilder dataEncryptor = new BcPGPDataEncryptorBuilder(encryptType);
dataEncryptor.setWithIntegrityPacket(withIntegrityCheck);
dataEncryptor.setSecureRandom(new SecureRandom());
PGPEncryptedDataGenerator encryptedDataGenerator = new PGPEncryptedDataGenerator(dataEncryptor);
encryptedDataGenerator.addMethod(new BcPublicKeyKeyEncryptionMethodGenerator(encKey));
byte[] outBytes = bOut.toByteArray();
OutputStream cOut = encryptedDataGenerator.open(out, outBytes.length);
cOut.write(outBytes);
cOut.close();
out.close();
System.out.println("data encrypted");
}
public static void signEncryptFile(OutputStream out, String fileName, PGPPublicKey publicKey, PGPSecretKey secretKey,
String password, boolean armor, boolean withIntegrityCheck) throws Exception {
// Initialize Bouncy Castle security provider
Provider provider = new BouncyCastleProvider();
Security.addProvider(provider);
if (armor) {
out = new ArmoredOutputStream(out);
}
BcPGPDataEncryptorBuilder dataEncryptor = new BcPGPDataEncryptorBuilder(SymmetricKeyAlgorithmTags.TRIPLE_DES);
dataEncryptor.setWithIntegrityPacket(withIntegrityCheck);
dataEncryptor.setSecureRandom(new SecureRandom());
PGPEncryptedDataGenerator encryptedDataGenerator = new PGPEncryptedDataGenerator(dataEncryptor);
encryptedDataGenerator.addMethod(new BcPublicKeyKeyEncryptionMethodGenerator(publicKey));
OutputStream encryptedOut = encryptedDataGenerator.open(out, new byte[BcPGP.BUFFER_SIZE]);
// Initialize compressed data generator
PGPCompressedDataGenerator compressedDataGenerator = new PGPCompressedDataGenerator(CompressionAlgorithmTags.ZIP);
OutputStream compressedOut = compressedDataGenerator.open(encryptedOut, new byte[BcPGP.BUFFER_SIZE]);
// Initialize signature generator
PGPPrivateKey privateKey = findPrivateKey(secretKey, password.toCharArray());
PGPContentSignerBuilder signerBuilder = new BcPGPContentSignerBuilder(secretKey.getPublicKey().getAlgorithm(),
HashAlgorithmTags.SHA1);
PGPSignatureGenerator signatureGenerator = new PGPSignatureGenerator(signerBuilder);
signatureGenerator.init(PGPSignature.BINARY_DOCUMENT, privateKey);
boolean firstTime = true;
Iterator it = secretKey.getPublicKey().getUserIDs();
while (it.hasNext() && firstTime) {
PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator();
spGen.setSignerUserID(false, (String) it.next());
signatureGenerator.setHashedSubpackets(spGen.generate());
// Exit the loop after the first iteration
firstTime = false;
}
signatureGenerator.generateOnePassVersion(false).encode(compressedOut);
// Initialize literal data generator
PGPLiteralDataGenerator literalDataGenerator = new PGPLiteralDataGenerator();
OutputStream literalOut = literalDataGenerator.open(compressedOut, PGPLiteralData.BINARY, fileName, new Date(),
new byte[BcPGP.BUFFER_SIZE]);
// Main loop - read the "in" stream, compress, encrypt and write to the
// "out" stream
FileInputStream in = new FileInputStream(fileName);
byte[] buf = new byte[BcPGP.BUFFER_SIZE];
int len;
while ((len = in.read(buf)) > 0) {
literalOut.write(buf, 0, len);
signatureGenerator.update(buf, 0, len);
}
in.close();
literalDataGenerator.close();
// Generate the signature, compress, encrypt and write to the "out"
// stream
signatureGenerator.generate().encode(compressedOut);
compressedDataGenerator.close();
encryptedDataGenerator.close();
if (armor) {
out.close();
}
}
// public static boolean verifyFile(InputStream in, InputStream keyIn,
// String extractContentFile) throws Exception {
// in = PGPUtil.getDecoderStream(in);
//
// PGPObjectFactory pgpFact = new PGPObjectFactory(in);
// PGPCompressedData c1 = (PGPCompressedData) pgpFact.nextObject();
//
// pgpFact = new PGPObjectFactory(c1.getDataStream());
//
// PGPOnePassSignatureList p1 = (PGPOnePassSignatureList)
// pgpFact.nextObject();
//
// PGPOnePassSignature ops = p1.get(0);
//
// PGPLiteralData p2 = (PGPLiteralData) pgpFact.nextObject();
//
// InputStream dIn = p2.getInputStream();
//
// IOUtils.copy(dIn, new FileOutputStream(extractContentFile));
//
// int ch;
// PGPPublicKeyRingCollection pgpRing = new
// PGPPublicKeyRingCollection(PGPUtil.getDecoderStream(keyIn));
//
// PGPPublicKey key = pgpRing.getPublicKey(ops.getKeyID());
//
// FileOutputStream out = new FileOutputStream(p2.getFileName());
//
// ops.init(new BcPGPContentVerifierBuilderProvider(), key);
//
// while ((ch = dIn.read()) >= 0) {
// ops.update((byte) ch);
// out.write(ch);
// }
//
// out.close();
//
// PGPSignatureList p3 = (PGPSignatureList) pgpFact.nextObject();
// return ops.verify(p3.get(0));
// }
/**
* From LockBox Lobs PGP Encryption tools.
* http://www.lockboxlabs.org/content/downloads
*
* I didn't think it was worth having to import a 4meg lib for three methods
*
* @param key
* @return
*/
public static boolean isForEncryption(PGPPublicKey key) {
if (key.getAlgorithm() == PublicKeyAlgorithmTags.RSA_SIGN || key.getAlgorithm() == PublicKeyAlgorithmTags.DSA
|| key.getAlgorithm() == PublicKeyAlgorithmTags.EC || key.getAlgorithm() == PublicKeyAlgorithmTags.ECDSA) {
return false;
}
return hasKeyFlags(key, KeyFlags.ENCRYPT_COMMS | KeyFlags.ENCRYPT_STORAGE);
}
/**
* From LockBox Lobs PGP Encryption tools.
* http://www.lockboxlabs.org/content/downloads
*
* I didn't think it was worth having to import a 4meg lib for three methods
*
* @param key
* @return
*/
private static boolean hasKeyFlags(PGPPublicKey encKey, int keyUsage) {
if (encKey.isMasterKey()) {
for (int i = 0; i != BcPGP.MASTER_KEY_CERTIFICATION_TYPES.length; i++) {
for (Iterator eIt = encKey.getSignaturesOfType(BcPGP.MASTER_KEY_CERTIFICATION_TYPES[i]); eIt.hasNext();) {
PGPSignature sig = (PGPSignature) eIt.next();
if (!isMatchingUsage(sig, keyUsage)) {
return false;
}
}
}
} else {
for (Iterator eIt = encKey.getSignaturesOfType(PGPSignature.SUBKEY_BINDING); eIt.hasNext();) {
PGPSignature sig = (PGPSignature) eIt.next();
if (!isMatchingUsage(sig, keyUsage)) {
return false;
}
}
}
return true;
}
/**
* From LockBox Lobs PGP Encryption tools.
* http://www.lockboxlabs.org/content/downloads
*
* I didn't think it was worth having to import a 4meg lib for three methods
*
* @param key
* @return
*/
private static boolean isMatchingUsage(PGPSignature sig, int keyUsage) {
if (sig.hasSubpackets()) {
PGPSignatureSubpacketVector sv = sig.getHashedSubPackets();
if (sv.hasSubpacket(BcPGP.KEY_FLAGS)) {
if ((sv.getKeyFlags() & keyUsage) == 0) {
return false;
}
}
}
return true;
}
}
- FileCopyPGP**
public class FileCopyPGP {
static final String PUBLIC_KEY_PATH = "D:\\PGP\\public.asc";
static final String PRIVATE_KEY_PATH = "D:\\PGP\\secret.asc";
static final String PASSWORD = "TestPass12345!";
static final String SOURCE_PATH = "D:\\PGP\\run\\Source.txt";
static final String ENCRYPTED_PATH = "D:\\PGP\\run\\Encrypted.txt";
static final String DECRYPTED_PATH = "D:\\PGP\\run\\Decrypted.txt";
public static void encrypt(InputStream is, OutputStream os) {
try {
PGPPublicKey encKey = BcPGP.readPublicKey(new FileInputStream(new File(PUBLIC_KEY_PATH)));
long start = System.currentTimeMillis();
System.out.println("Encryption started");
BcPGP.encryptFile(os, is, encKey, true, true, CompressionAlgorithmTags.UNCOMPRESSED,
SymmetricKeyAlgorithmTags.AES_256);
System.out.println("Encryption ended successfully in " + (System.currentTimeMillis() - start) + "ms");
} catch (IOException e) {
System.out.println(e.getMessage());
} catch (PGPException e) {
System.out.println(e.getMessage());
} catch (NoSuchProviderException e) {
System.out.println(e.getMessage());
}
}
public static void decrypt(InputStream is, OutputStream os) {
try {
long start = System.currentTimeMillis();
System.out.println("Decryption started");
BcPGP.decryptFile(is, os, new FileInputStream(new File(PRIVATE_KEY_PATH)), PASSWORD.toCharArray());
System.out.println("Decryption ended successfully in " + (System.currentTimeMillis() - start) + "ms");
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public static void main(String[] args) throws FileNotFoundException {
encrypt(new FileInputStream(new File(SOURCE_PATH)), new FileOutputStream(new File(ENCRYPTED_PATH)));
decrypt(new FileInputStream(new File(ENCRYPTED_PATH)), new FileOutputStream(new File(DECRYPTED_PATH)));
}
}
1条答案
按热度按时间sdnqo3pr1#
在进一步研究这个主题时,我发现了这个SCN post。它启发了我做三个改变来使它工作:
bcprov-ext-jdk14-145.jar
和bcpg-jdk14-145.jar
帖子中提到将密钥放入导入的存档中;这意味着如果需要的话很难更换,而且在每个环境(DEV,QA,LIVE...)中也很难使用不同的。我上面提供的代码使用PI值Map来设置每个环境中公钥的可配置完整文件路径。
在下面的类中需要解决的一个重要问题是在创建
PGPPublicKeyRingCollection
之前使用PGPUtil.getDecoderStream
,如下所述:http://www.coderanch.com/t/600592/Security/Bouncy-Castle-API-invalid-header