0
我使用https://github.com/TomRoush/PdfBox-Android來加密pdf所描述的here ......但它不起作用。加密PDF與PdfBox-Android不兼容
我用Foxit和AdobeReader檢查結果。 AdobeReader說我的文件已損壞,但Foxit向我顯示密碼對話框。但是,然後我可以嘗試我想要的福昕也無法解密我的文件。
如果我設置keyLength = 256我得到了上述描述,但我也嘗試了其他2個keyLength值,但是文件未加密。
我錯過了什麼,或者是加密只是不使用Android上的這個庫?
這裏是我的代碼
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();
}
}
該Android端口基於1.8.x版本,而不是某些2.0.x.因此,查看[1.8.x加密食譜條目](https://pdfbox.apache.org/1.8/cookbook/encryption.html),它告訴你僅支持40和128的密鑰長度。因此,不管你得到什麼256,都不應該使用。此外,Android端口使用SpongyCastle而不是BouncyCastle,所以請嘗試使用該安全提供程序。 – mkl
謝謝你......但正如我所說:我也嘗試過40和128,但它不起作用。它也使用SpongyCastle ...(我想:-),因爲我使用[靜態{Security.insertProviderAt(new org.spongycastle.jce.provider.BouncyCastleProvider(),1); }]在我的活動頂部...(我會更新我的代碼上面) – HowardS
請注意,這些是單獨的項目...我注意到,Android源代碼庫沒有加密單元測試/解密:-( –