itextpdf 加密解密

itextpdf 加密解密itextpdf加密解密

大家好,又见面了,我是你们的朋友全栈君。


itextpdf 加密解密

                

                       

**************

相关类与接口

        

PdfWriter

public class PdfWriter extends PdfOutputStream {
    private static final byte[] obj = ByteUtils.getIsoBytes(" obj\n");
    private static final byte[] endobj = ByteUtils.getIsoBytes("\nendobj\n");
    protected WriterProperties properties;
    PdfObjectStream objectStream;
    private Map<PdfIndirectReference, PdfIndirectReference> copiedObjects;
    private SmartModePdfObjectsSerializer smartModeSerializer;
    protected boolean isUserWarnedAboutAcroFormCopying;

********
构造方法

    public PdfWriter(File file) throws FileNotFoundException {
    public PdfWriter(OutputStream os) {
    public PdfWriter(OutputStream os, WriterProperties properties) {
    public PdfWriter(String filename) throws FileNotFoundException {
    public PdfWriter(String filename, WriterProperties properties) throws FileNotFoundException {

********
其他方法

    public PdfWriter setSmartMode(boolean smartMode) {
    public PdfWriter setCompressionLevel(int compressionLevel) {

    public boolean isFullCompression() {
    public int getCompressionLevel() {

    PdfObjectStream getObjectStream() {
    void flushCopiedObjects(long docId) {

    protected void initCryptoIfSpecified(PdfVersion version) {
    protected void flushObject(PdfObject pdfObject, boolean canBeInObjStm) {
    protected PdfObject copyObject(PdfObject obj, PdfDocument documentTo, boolean allowDuplicating) {
    protected void writeToBody(PdfObject pdfObj) {
    protected void writeHeader() {
    protected void flushWaitingObjects(Set<PdfIndirectReference> forbiddenToFlush) {
    protected void flushModifiedWaitingObjects(Set<PdfIndirectReference> forbiddenToFlush) {

    private void markArrayContentToFlush(PdfArray array) {
    private void markDictionaryContentToFlush(PdfDictionary dictionary) {
    private void markObjectToFlush(PdfObject pdfObject) {
    private static boolean checkTypeOfPdfDictionary(PdfObject dictionary, PdfName expectedType) {

              

WriterProperties

public class WriterProperties {
    protected int compressionLevel = -1;
    protected Boolean isFullCompression = null;
    protected boolean smartMode = false;
    protected boolean addXmpMetadata;
    protected boolean addUAXmpMetadata = false;
    protected PdfVersion pdfVersion;
    protected EncryptionProperties encryptionProperties = new EncryptionProperties();
    protected PdfString initialDocumentId;
    protected PdfString modifiedDocumentId;


********
构造方法

    public WriterProperties() {
    }


********
其他方法

    public WriterProperties useSmartMode() {
    public WriterProperties addXmpMetadata() {
    public WriterProperties addUAXmpMetadata() {

    public WriterProperties setPdfVersion(PdfVersion version) {
    public WriterProperties setCompressionLevel(int compressionLevel) {
    public WriterProperties setInitialDocumentId(PdfString initialDocumentId) {
    public WriterProperties setModifiedDocumentId(PdfString modifiedDocumentId) {
    public WriterProperties setFullCompressionMode(boolean fullCompressionMode) {
    public WriterProperties setPublicKeyEncryption(Certificate[] certs, int[] permissions, int encryptionAlgorithm) {
    public WriterProperties setStandardEncryption(byte[] userPassword, byte[] ownerPassword, int permissions, int encryptionAlgorithm) {

    boolean isStandardEncryptionUsed() {
    boolean isPublicKeyEncryptionUsed() {

              

EncryptionConstants

public final class EncryptionConstants {
    public static final int STANDARD_ENCRYPTION_40 = 0;
    public static final int STANDARD_ENCRYPTION_128 = 1;
    public static final int ENCRYPTION_AES_128 = 2;
    public static final int ENCRYPTION_AES_256 = 3;
    public static final int DO_NOT_ENCRYPT_METADATA = 8;
    public static final int EMBEDDED_FILES_ONLY = 24;
    public static final int ALLOW_PRINTING = 2052;
    public static final int ALLOW_MODIFY_CONTENTS = 8;
    public static final int ALLOW_COPY = 16;
    public static final int ALLOW_MODIFY_ANNOTATIONS = 32;
    public static final int ALLOW_FILL_IN = 256;
    public static final int ALLOW_SCREENREADERS = 512;
    public static final int ALLOW_ASSEMBLY = 1024;
    public static final int ALLOW_DEGRADED_PRINTING = 4;
    static final int ENCRYPTION_MASK = 7;

    private EncryptionConstants() {
    }
}

                      

                        

PdfReader

public class PdfReader implements Closeable {
    public static final PdfReader.StrictnessLevel DEFAULT_STRICTNESS_LEVEL;
    private static final String endstream1 = "endstream";
    private static final String endstream2 = "\nendstream";
    private static final String endstream3 = "\r\nendstream";
    private static final String endstream4 = "\rendstream";
    private static final byte[] endstream;
    private static final byte[] endobj;
    protected static boolean correctStreamLength;
    private boolean unethicalReading;
    private boolean memorySavingMode;
    private PdfReader.StrictnessLevel strictnessLevel;
    private PdfIndirectReference currentIndirectReference;
    protected PdfTokenizer tokens;
    protected PdfEncryption decrypt;
    protected PdfVersion headerPdfVersion;
    protected long lastXref;
    protected long eofPos;
    protected PdfDictionary trailer;
    protected PdfDocument pdfDocument;
    protected PdfAConformanceLevel pdfAConformanceLevel;
    protected ReaderProperties properties;
    protected boolean encrypted;
    protected boolean rebuiltXref;
    protected boolean hybridXref;
    protected boolean fixedXref;
    protected boolean xrefStm;

*********
构造方法

    public PdfReader(InputStream is) throws IOException {
    public PdfReader(InputStream is, ReaderProperties properties) throws IOException {

    public PdfReader(File file) throws FileNotFoundException, IOException {
    public PdfReader(String filename) throws IOException {
    public PdfReader(String filename, ReaderProperties properties) throws IOException {

    public PdfReader(IRandomAccessSource byteSource, ReaderProperties properties) throws IOException {
    PdfReader(IRandomAccessSource byteSource, ReaderProperties properties, boolean closeStream) throws IOException {


*********
其他方法

    public byte[] computeUserPassword() {  //计算用户密码

    public void close() throws IOException {
    public void setCloseStream(boolean closeStream) {
    public PdfReader.StrictnessLevel getStrictnessLevel() {
    public PdfReader setMemorySavingMode(boolean memorySavingMode) {
    public PdfReader setUnethicalReading(boolean unethicalReading) {
    public PdfReader setStrictnessLevel(PdfReader.StrictnessLevel strictnessLevel) {


    public long getLastXref() {
    public int getCryptoMode() {
    public long getFileLength() {
    public long getPermissions() {
    public byte[] getOriginalFileId() {
    public byte[] getModifiedFileId() {
    public RandomAccessFileOrArray getSafeFile() {
    public PdfAConformanceLevel getPdfAConformanceLevel() {

    public boolean hasXrefStm() {
    public boolean hasFixedXref() {
    public boolean hasHybridXref() {
    public boolean hasRebuiltXref() {

    public boolean isEncrypted() {
    public boolean isCloseStream() {
    public boolean isOpenedWithFullPermission() {

    public byte[] readStreamBytes(PdfStream stream, boolean decode) throws IOException {
    public byte[] readStreamBytesRaw(PdfStream stream) throws IOException {
    public InputStream readStream(PdfStream stream, boolean decode) throws IOException {

    public static byte[] decodeBytes(byte[] b, PdfDictionary streamDictionary) {
    public static byte[] decodeBytes(byte[] b, PdfDictionary streamDictionary, Map<PdfName, IFilterHandler> filterHandlers) {


    protected void readPdf() throws IOException {
    protected void readObjectStream(PdfStream objectStream) throws IOException {
    protected PdfObject readObject(PdfIndirectReference reference) {
    protected PdfObject readObject(boolean readAsDirect) throws IOException {
    protected PdfObject readReference(boolean readAsDirect) {
    protected PdfObject readObject(boolean readAsDirect, boolean objStm) throws IOException {
    protected PdfName readPdfName(boolean readAsDirect) {
    protected PdfDictionary readDictionary(boolean objStm) throws IOException {
    protected PdfArray readArray(boolean objStm) throws IOException {
    protected void readXref() throws IOException {
    protected PdfDictionary readXrefSection() throws IOException {
    protected boolean readXrefStream(long ptr) throws IOException {
    protected void fixXref() throws IOException {
    protected void rebuildXref() throws IOException {

    boolean isMemorySavingMode() {

    private void readDecryptObj() {
    private static PdfTokenizer getOffsetTokeniser(IRandomAccessSource byteSource, boolean closeStream) throws IOException {
    private PdfObject readObject(PdfIndirectReference reference, boolean fixXref) {
    private void checkPdfStreamLength(PdfStream pdfStream) throws IOException {
    private PdfObject createPdfNullInstance(boolean readAsDirect) {

    static {
        DEFAULT_STRICTNESS_LEVEL = PdfReader.StrictnessLevel.LENIENT;
        endstream = ByteUtils.getIsoBytes("endstream");
        endobj = ByteUtils.getIsoBytes("endobj");
        correctStreamLength = true;
    }


*********
内部枚举:StrictnessLevel

    public static enum StrictnessLevel {
        CONSERVATIVE(5000),
        LENIENT(3000);

        private final int levelValue;

        private StrictnessLevel(int levelValue) {
        public boolean isStricter(PdfReader.StrictnessLevel compareWith) {


*********
静态内部类:ReusableRandomAccessSource

    protected static class ReusableRandomAccessSource implements IRandomAccessSource {
        private ByteBuffer buffer;

        public ReusableRandomAccessSource(ByteBuffer buffer) {

        public int get(long offset) {
        public int get(long offset, byte[] bytes, int off, int len) {

        public long length() {
        public void close() {

                  

ReaderProperties

public class ReaderProperties {
    protected byte[] password;
    protected Key certificateKey;
    protected Certificate certificate;
    protected String certificateKeyProvider;
    protected IExternalDecryptionProcess externalDecryptionProcess;
    protected MemoryLimitsAwareHandler memoryLimitsAwareHandler;

    public ReaderProperties() {
    }

    public ReaderProperties setPassword(byte[] password) {
    public ReaderProperties setPublicKeySecurityParams(Certificate certificate, Key certificateKey, String certificateKeyProvider, IExternalDecryptionProcess externalDecryptionProcess) {
    public ReaderProperties setPublicKeySecurityParams(Certificate certificate, IExternalDecryptionProcess externalDecryptionProcess) {
    public ReaderProperties setMemoryLimitsAwareHandler(MemoryLimitsAwareHandler memoryLimitsAwareHandler) {

    private void clearEncryptionParams() {
  
  

             

                   

**************

示例

        

public class Test4 {

    private static final String source = "./source/1.pdf";

    private static final String dest = "./security/1.pdf";
    private static final String dest2 = "./security/2.pdf";
    private static final String dest3 = "./security/3.pdf";

    public static void fun() throws Exception{  //设置文件拥有者、用户密码,
                                                //读取文件时需使用文件拥有者、或者用户密码
        PdfDocument pdfDocument = new PdfDocument(new PdfReader(source),
                new PdfWriter(dest, new WriterProperties()
                        .setStandardEncryption(
                                "123456".getBytes(StandardCharsets.UTF_8),
                                "1qaz2w".getBytes(StandardCharsets.UTF_8),
                                EncryptionConstants.ALLOW_PRINTING,
                                EncryptionConstants.ENCRYPTION_AES_128
                        )
                ));

        pdfDocument.close();
    }

    public static void fun2() throws Exception{  //只设置文件拥有者密码,不设置用户密码,
                                                 //读取文件时不需要使用密码
        PdfDocument pdfDocument = new PdfDocument(new PdfReader(source),
                new PdfWriter(dest2, new WriterProperties()
                        .setStandardEncryption(
                                null,
                                "1qaz2w".getBytes(StandardCharsets.UTF_8),
                                EncryptionConstants.ALLOW_PRINTING,
                                EncryptionConstants.ENCRYPTION_AES_128
                        )
                ));

        pdfDocument.close();
    }

    public static void fun3() throws Exception{  //通过文件拥有密码获取用户密码
        PdfDocument pdfDocument = new PdfDocument(new PdfReader(dest,
                new ReaderProperties().setPassword("1qaz2w".getBytes(StandardCharsets.UTF_8))));

        byte[] passwordBytes = pdfDocument.getReader().computeUserPassword();
        System.out.println(passwordBytes!=null?new String(passwordBytes):"null");
    }

    public static void fun4() throws Exception{  //不设置用户密码,可直接读取文件
        PdfDocument pdfDocument = new PdfDocument(new PdfReader(dest2)
                .setUnethicalReading(true), new PdfWriter(dest3));

        pdfDocument.close();
    }

    public static void main(String[] args) throws Exception{
        fun();
        fun2();

        fun3();
        fun4();
    }
}

         

fun:同时设置拥有者、用户密码,查看需输入密码(拥有者、用户密码均可)

                 itextpdf 加密解密

                 itextpdf 加密解密 

                   

fun2:只设置拥有者密码、不设用户密码,可不输入密码直接查看文件

                 itextpdf 加密解密

                             

                            

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/149815.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

  • 中间件技术及双十一实践·EagleEye篇「建议收藏」

    中间件技术及双十一实践·EagleEye篇「建议收藏」EagleEye——分布式调用的跟踪者转自:http://www.tuicool.com/articles/fuYz2i综述阿里巴巴电子商务平台现在是一个由很多个应用集群组成的非常复杂的分布式系统。这些应用里面主要有处理用户请求的前端系统和有提供服务的后端系统等,各个应用之间一般有RPC调用和异步消息通讯两种手段,RPC调用会产生一层调一层的嵌套,一个消息发布出来更会被多

  • 树莓派连接wifi个人热点和远程windows远程登录

    树莓派连接wifi个人热点和远程windows远程登录1.安装完系统后在该目录下新建wpa_supplicant.conf文件填入以下信息country=CNctrl_interface=DIR=/var/run/wpa_supplicantGROUP=netdevupdate_config=1network={ssid="WiFi-A"psk="12345678"key_mgmt=WPA-PSKpriority=1}…

  • android activity自动跳转,Android activity跳转之finish()方法的作用

    android activity自动跳转,Android activity跳转之finish()方法的作用最近在看郭神的《第一行代码(第2版)》,温故知新,看到有很多地方在页面跳转后还加了一句:finish();,原来像下面代码这样用Intent从Aactivity跳到Bactivity:Intentintent=newIntent(A.this,B.class);startActivity(intent);当成功跳到Bactivity的时候再点击返回键,就再次返回到了…

  • IDEA使用ideaVim, 配置自定义vim快捷键[通俗易懂]

    IDEA使用ideaVim, 配置自定义vim快捷键[通俗易懂]C:\Users\Administrator文件夹下创建_ideavimrc,我这里用的是win系统需要安装ideaVim插件letmapleader=”sethlsearchsetincsearchsetignorecasesetsmartcasesetshowmodesetnumbersetrelativenumbersetscrolloff=…

  • 一分钟教会你固态硬盘数据恢复方法

    不少人都知道,固态硬盘上丢失了数据是很难恢复的,但是也有一些数据丢失的情况,是可以尝试恢复数据的,比如说误删除分区、分区变为RAW状态。针对这些情况,今天呢,小编给大家分享一下固态硬盘数据恢复方法,我们需要借助一款好用的数据恢复软件——易我数据恢复,该软件可深度扫描磁盘数据,根据删除标记、文件目录信息和文件内容等三种方式分别检索文件,搜寻更全面。(详情访问:https://www.easeus.com.cn/data-recovery-software/data-recovery-wizard.html

  • Pandas个人操作练习(1)创建dataframe及插入列、行操作

    Pandas个人操作练习(1)创建dataframe及插入列、行操作使用pandas之前要导入包:importnumpyasnpimportpandasaspdimportrandom#其中有用到random函数,所以导入一、dataframe创建pandas.DataFrame(data=None,index=None,columns=None,dtype=None,copy=False)data:numpy…

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号