저는 BouncyCastle과 pgp를 처음 사용합니다. 인터넷에서 많은 기사와 샘플을 보았습니다. 거의 모든 암호화 샘플에 아래 코드가 포함되어 있습니다.BouncyCastle을 사용할 때 ArmoredOutputStream을 사용하여 OutputStream을 장식하는시기와 이유
if (armor)
out = new ArmoredOutputStream(out);
내 로컬 테스트에 갑옷과 무 장갑 모두가 함께 사용 된 것으로 보입니다. 내가 봤지만 주위에 몇 가지 유용한 발견 ArmoredOutputStream의 javadoc 이것은 기본 출력 스트림을 보여줍니다.
그래서 차이점은 무엇이며 언제 사용합니까?
전체 코드 샘플 : 바이너리 인쇄 할 수없는 바이트 친화적 인 무언가를 텍스트로 변환되도록
public static void encryptFile(String decryptedFilePath,
String encryptedFilePath,
String encKeyPath,
boolean armor,
boolean withIntegrityCheck)
throws Exception{
OutputStream out = new FileOutputStream(encryptedFilePath);
FileInputStream pubKey = new FileInputStream(encKeyPath);
PGPPublicKey encKey = readPublicKeyFromCollection2(pubKey);
Security.addProvider(new BouncyCastleProvider());
if (armor)
out = new ArmoredOutputStream(out);
// Init encrypted data generator
PGPEncryptedDataGenerator encryptedDataGenerator =
new PGPEncryptedDataGenerator(PGPEncryptedData.CAST5, withIntegrityCheck, new SecureRandom(),"BC");
encryptedDataGenerator.addMethod(encKey);
OutputStream encryptedOut = encryptedDataGenerator.open(out, new byte[BUFFER_SIZE]);
// Init compression
PGPCompressedDataGenerator compressedDataGenerator = new PGPCompressedDataGenerator(PGPCompressedData.ZIP);
OutputStream compressedOut = compressedDataGenerator.open(encryptedOut);
PGPLiteralDataGenerator literalDataGenerator = new PGPLiteralDataGenerator();
OutputStream literalOut = literalDataGenerator.open(compressedOut, PGPLiteralData.BINARY, decryptedFilePath, new Date(), new byte[BUFFER_SIZE]);
FileInputStream inputFileStream = new FileInputStream(decryptedFilePath);
byte[] buf = new byte[BUFFER_SIZE];
int len;
while((len = inputFileStream.read(buf))>0){
literalOut.write(buf,0,len);
}
literalOut.close();
literalDataGenerator.close();
compressedOut.close();
compressedDataGenerator.close();
encryptedOut.close();
encryptedDataGenerator.close();
inputFileStream.close();
out.close();
}
}