android pdf 文件加密,加密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();

}

}

2017-03-02

HowardS

+0

该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,所以请尝试使用该安全提供程序。 –

+0

谢谢你......但正如我所说:我也尝试过40和128,但它不起作用。它也使用SpongyCastle ...(我想:-),因为我使用[静态{Security.insertProviderAt(new org.spongycastle.jce.provider.BouncyCastleProvider(),1); }]在我的活动顶部...(我会更新我的代码上面) –

+0

请注意,这些是单独的项目...我注意到,Android源代码库没有加密单元测试/解密:-( –

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值