加密pdf不适用于PdfBox Android
我用加密一个pdf像描述。。。但它不起作用 我用Foxit和AdobeReader检查结果。AdobeReader说我的文件已损坏,但它显示了密码对话框。但是我可以试试我想要的,因为它也不能解密我的文件 如果我将keyLength设置为256,我会得到上面描述的结果,但我也尝试了其他2个keyLength值,但文件没有加密 我是不是错过了什么,或者加密就是不能在Android上使用这个库 这是我的密码加密pdf不适用于PdfBox Android,android,pdf,encryption,pdfbox,Android,Pdf,Encryption,Pdfbox,我用加密一个pdf像描述。。。但它不起作用 我用Foxit和AdobeReader检查结果。AdobeReader说我的文件已损坏,但它显示了密码对话框。但是我可以试试我想要的,因为它也不能解密我的文件 如果我将keyLength设置为256,我会得到上面描述的结果,但我也尝试了其他2个keyLength值,但文件没有加密 我是不是错过了什么,或者加密就是不能在Android上使用这个库 这是我的密码 static { Security.insertProviderAt
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();
}
}
在回答了我的问题后,我再试了一次,似乎我从来没有使用过其他的keyLength值或遗漏了其他内容 所以不要用这种说法
spp.setEncryptionKeyLength(keyLength);
然后用40或仅使用
spp.setEncryptionKeyLength(128);
但现在一切都很顺利!!:-) Android端口基于1.8.x版本,而不是一些2.0.x版本。因此,请看,它告诉您仅支持密钥长度40和128。因此,无论你花256美元得到什么,都不应该使用。此外,Android端口使用SpongyCastle而不是BouncyCastle,因此请尝试使用该安全提供商。谢谢。。。但正如我所说:我也试过40和128,但都不起作用。它还使用海绵城堡。。。(我想:-)因为我在活动的顶部使用了[static{Security.insertProviderAt(new org.spongycastle.jce.provider.BouncyCastleProvider(),1);}]。。。(我将更新上面的代码)请注意,这些是独立的项目。。。我注意到android源代码库没有加密/解密的单元测试:-(