2017-03-02 2 views
0

here과 같은 PDF를 암호화하기 위해 https://github.com/TomRoush/PdfBox-Android을 사용하지만 작동하지 않습니다.PDF 파일 암호화가 PdfBox-Android에서 작동하지 않습니다.

Foxit 및 AdobeReader로 결과를 확인합니다. AdobeReader는 내 파일이 손상되었지만 Foxit이 암호 대화 상자를 표시한다고 말합니다. 하지만 내가 원하는 것을 시도해 볼 수 있습니다. Foxit도 내 파일의 암호를 해독 할 수 없습니다.

키 길이를 256으로 설정하면 위의 설명과 비슷하지만 다른 2 개의 keyLength 값을 시도했지만 파일이 암호화되지 않았습니다.

나는 뭔가를 놓쳤는가 아니면 안드로이드에서이 lib와 함께 작동하지 않는 암호화입니까 ??

여기 Tom here에서 내 질문에 대답 한 후 내 코드

static { 
     Security.insertProviderAt(new org.spongycastle.jce.provider.BouncyCastleProvider(), 1); 
    } 

    public void createPdf() { 

    File root   = android.os.Environment.getExternalStorageDirectory(); 
    String path   = root.getAbsolutePath() + "/Download/crypt.pdf"; 


    int      keyLength = 256; 
    AccessPermission   ap   = new AccessPermission(); 
    StandardProtectionPolicy spp   = new StandardProtectionPolicy("12345", "", ap); 

    spp.setEncryptionKeyLength(keyLength); 
    spp.setPermissions(ap); 

    BouncyCastleProvider provider = new BouncyCastleProvider(); 
    Security.addProvider(provider); 

    PDFont  font  = PDType1Font.HELVETICA; 
    PDDocument document = new PDDocument(); 
    PDPage  page  = new PDPage(); 

    document.addPage(page); 

    try { 

     PDPageContentStream contentStream = new PDPageContentStream(document, page); 

     // Write Hello World in blue text 
     contentStream.beginText(); 
     contentStream.setNonStrokingColor(15, 38, 192); 
     contentStream.setFont(font, 12); 
     contentStream.newLineAtOffset(100, 700); 
     contentStream.showText("Hello World"); 
     contentStream.endText(); 
     contentStream.close(); 

     // Save the final pdf document to a file 
     document.protect(spp); 
     document.save(path); 
     document.close(); 

    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 
+0

Android 포트는 2.0.x가 아닌 1.8.x 버전을 기반으로합니다. 따라서, 키 길이 40과 128 만 지원된다는 [1.8.x 암호화 요리 책 항목] (https://pdfbox.apache.org/1.8/cookbook/encryption.html)을보십시오. 256을 위해 얻는 무엇 이건은, 그러므로, 이용되면 안된다. 또한 Android 포트는 BouncyCastle 대신 SpongyCastle을 사용하므로 해당 보안 제공 업체를 사용해보십시오. – mkl

+0

고맙습니다 ...하지만 제가 말했듯이 : 나는 또한 40과 128을 시도했지만 효과가 없습니다. 또한 SpongyCastle을 사용합니다 ... (i 생각 :-) [static { Security.insertProviderAt (new org.spongycastle.jce.provider.BouncyCastleProvider(), 1); }] 위에서 내 코드를 업데이트합니다. – HowardS

+0

이것들이 별도의 프로젝트라는 것을 알고 ... android 소스 코드 저장소에 암호화에 대한 단위 테스트가 없다는 것을 알아 챘습니다./암호 해독 :-( –

답변

0

이다 내가 다시 시도했고 내가 다른 keyLength 값 또는 다른 놓친 뭔가를 사용한 적이처럼 보인다.

그래서 그냥 ...하지만 지금은 최종 모든게에서 잘 작동

spp.setEncryptionKeyLength(128); 

을 사용하여 다음

spp.setEncryptionKeyLength(keyLength); 

당신이 40로 암호화 계산서를 사용하지 마십시오! :-)

관련 문제