2014-06-23 6 views
5

저는 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(); 

} 
} 

답변

7

ArmoredOutputStreamBase64 유사한 인코딩을 사용합니다. 이메일을 통해 데이터를 보내거나 사이트에 게시하거나 다른 텍스트 미디어에 게시하려는 경우이 작업을 수행합니다.

보안상의 차이는 없습니다. 그래도 a slight expansion of the message size이 있습니다. 선택은 실제로 출력으로 무엇을하고 싶은지에 달려 있습니다.

3

ASCII 갑옷은 이진 데이터 표현을 ASCII 전용 텍스트로 의미하는 일반적인 용어입니다. 기술적으로 이진 데이터는 ascii-armor에 많이 있지만 암호 관련 필드에서는 the PEM format이 널리 퍼져 있습니다 (serverfault에서 this and related questions 확인).

PEM은 기본적으로 -----BEGIN SOMETHING----------END SOMETHING----- 구분 기호로 둘러싸인 Base64 인코딩 이진 데이터와 이진 콘텐츠에 대한 일부 메타 정보를 포함 할 수있는 추가 헤더 집합입니다.