Java 加密解密PowerPoint文档

在日常办公时,我们常会用PowerPoint幻灯片来制作设计方案、年终总结等。这类幻灯片中的内容大多会涉及一些重要的数据信息,为了避免泄露造成损失,我们时常需要对其进行加密保护。本教程将演示如何在Java程序中给PowerPoint文档加密、设置幻灯片为只读、修改加密文档的密码以及解密文档。

测试环境

在运行代码前,需要搭建测试环境。在电脑上安装JDK和IntelliJ IDEA,然后通过E-iceblue中文官网下载Free Spire.Presentation for Java控件,解压后将lib文件夹下的Spire.Presentation.jar手动导入IDEA。

代码示例

加密保护PowerPoint文档

import com.spire.presentation.*;

public class Encrypt {
    public static void main(String[] args) throws Exception {
        //加载PowerPoint示例文档
        Presentation presentation = new Presentation();
        presentation.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.pptx");

        //设置密码加密文档
        presentation.encrypt("abc123");

        //保存结果文档
        presentation.saveToFile("output/encrypt_output.pptx", FileFormat.PPTX_2013);
        presentation.dispose();
    }
}

运行以上代码后,需输入设置的密码才能打开PowerPoint文档进行查看和编辑操作。如下图所示

设置PowerPoint幻灯片为只读

Free Spire.Presentation for Java还提供了Protect方法来保护文档。使用该保护方式后,用户需输入密码才能对文档内容进行编辑、打印等一系列操作,而无密码用户只能选择在只读模式下查看文档。

import com.spire.presentation.*;
public class ReadOnly {
    public static void main(String[] args) throws Exception {
        //加载PowerPoint示例文档
        Presentation presentation = new Presentation();
        presentation.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.pptx");

        //设置密码保护文档
        presentation.protect("hij789");

        //保存结果文档
        presentation.saveToFile("output/setDocumentReadOnly_output.pptx", FileFormat.PPTX_2013);
        presentation.dispose();
    }
}

修改加密文档的密码

Free Spire.Presentation for Java支持使用RemoveEncryption方法来解除加密,再调用加密方法设置新密码来加密文档。

import com.spire.presentation.*;
public class ModifyPassword {
    public static void main(String[] args) throws Exception {
        //加载受密码保护的PowerPoint示例文档
        Presentation presentation = new Presentation();
        presentation.loadFromFile("C:\\Users\\Test1\\Desktop\\encrypt_output.pptx", "abc123");

        //移除密码
        presentation.removeEncryption();

        //重新设置新密码保护文档
        presentation.protect("def456");

        //保存结果文档
        presentation.saveToFile("output/modifyPasswordOfEncryptedPPT.pptx", FileFormat.PPTX_2013);
    }
}

解密文档

import com.spire.presentation.*;

public class RemoveEncryption {
    public static void main(String[] args) throws Exception {
        //加载受密码保护的PowerPoint示例文档
        Presentation presentation = new Presentation();
        presentation.loadFromFile("C:\\Users\\Test1\\Desktop\\encrypt_output.pptx", "abc123");

        //移除密码
        presentation.removeEncryption();

        //保存结果文档
        presentation.saveToFile("output/removeEncryption.pptx", FileFormat.PPTX_2013);
    }
}

相关文章

100个Java工具类之74:压缩与解压利器类ZipUtils

日常数据处理需求中,文件压缩与解压是必不可少,而ZipUtils就是一个非常实用的工具类,他拥有文件和文件夹的压缩及解压功能。并且支持将多个文件和目录压缩为一个ZIP文件,并从ZIP文件中解压文件。Z...

《调教命令行07》压缩解压(有64KB彩蛋)

原创:小姐姐味道(微信公众号ID:xjjdog),欢迎分享,转载请保留出处。任何不保留此声明的转载都是抄袭。压缩,是一件非常神奇的事情。很久很久之前,就接触过一些64KB大小的电影,你花半小时都看不完...

Java代码保护方法之四:JVMTI实现Java源码保护

大家好,我叫小丁,一名小小程序员。今天继续介绍Java代码保护的第四种方案:JVMTI。采用ClassFinal和自定义类加载器这两种策略来保护Java代码时,它们面临的一个共同的主要挑战在于:加解密...

服务器上修改基于springboot的jar包文件

问题描述现在基于springboot的服务器jar包体积越来越大, 当我们在特殊场景下,比如压力测试或者排查问题想临时修改下jar包里的内容验证下, 是不是非要重新打包上传文件呢? 答案当然是否。解决...

做了个Java打包工具,可以双击启动了!

日常工作主要使用Java进行开发,业余时间也热衷于技术研究,喜欢用Java的GUI库Swing开发一些实用的小工具。但是用Swing开发软件相比C/C++的一个很大的劣势就是,Java打包出来的文件不...