Java免费文件安全加密解密

官网地址:C#/VB.Net Excel, Word, PowerPoint, PDF Component - Welcome to e-iceblue Company Ltd

 Java集成Excel加解密示例

配置文件

文件枚举

package com.boonya.spring.mybatis.plus.enums;

import lombok.Getter;

@Getter
public enum FileType {

    EXCEL(".xls","Excel"),
    EXCEL_X(".xls","Excel X"),
    JSON(".json","JSON"),
    PDF(".pdf","PDF"),
    WORD(".doc","WORD"),
    WORD_X(".docx","WORD X"),
    ;

    private String type;
    private String desc;

    private FileType(String type,String desc){
        this.type = type;
        this.desc = desc;
    }
}

抽象定义

package com.boonya.spring.mybatis.plus.config;

import com.boonya.spring.mybatis.plus.enums.FileType;
import com.spire.xls.Workbook;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import java.io.File;

@Configuration
public abstract class AbstractEncryptDecryptConfig {

    /**
     * 临时保存路径
     */
    @Value("${file.encrypt.save.path}")
    protected static final String TEMP_URL = "";

    /**
     * 加密保存路径
     */
    @Value("${file.encrypt.save.path}")
    protected static final String ENCRYPT_URL = "";

    /**
     * 解密保存路径
     */
    @Value("${file.decrypt.save.path}")
    protected static final String DECRYPT_URL = "";

    /**
     * 保存操作后的文件
     *
     * @param wb
     * @param targetPath
     */
    protected void save(Workbook wb, String targetPath) {
        File file = new File(targetPath);
        if (!file.exists()) {
            wb.saveToFile(targetPath);
        }
    }

    public String getEncryptFilePath(String fileName, FileType type) {
        return ENCRYPT_URL + File.separator + fileName + type.getType();
    }

    public String getDecryptFilePath(String fileName, FileType type) {
        return DECRYPT_URL + File.separator + fileName + type.getType();
    }

    /**
     * 加密
     *
     * @param fileName
     * @param securityPwd
     */
    public abstract void encrypt(String fileName, String securityPwd);

    /**
     * 解密
     *
     * @param fileName
     * @param securityPwd
     */
    public abstract void decrypt(String fileName, String securityPwd);
}

加密解密

Workbook

package com.boonya.spring.mybatis.plus.config;

import com.boonya.spring.mybatis.plus.enums.FileType;
import com.spire.xls.Workbook;
import org.springframework.stereotype.Component;
import java.io.File;

@Component
public class WorkbookEncryptDecrypt extends AbstractEncryptDecryptConfig {

    /**
     * 加密Excel
     *
     * @param fileName
     * @param securityPwd
     */
    public void encrypt(String fileName, String securityPwd) {
        //加载Excel文档
        Workbook wb = new Workbook();
        wb.loadFromFile(TEMP_URL + File.separator + fileName);
        //加密工作簿
        wb.protect(securityPwd);
        //保存文档
        String targetPath = getEncryptFilePath(fileName, fileName.contains(FileType.EXCEL.getType()) ? FileType.EXCEL : FileType.EXCEL_X);
        save(wb, targetPath);
    }

    /**
     * 解密Excel
     *
     * @param fileName
     * @param securityPwd
     */
    public void decrypt(String fileName, String securityPwd) {
        //加载文档
        Workbook wb = new Workbook();
        wb.setOpenPassword(securityPwd);
        wb.loadFromFile(ENCRYPT_URL + File.separator + fileName);
        //解除密码保护
        wb.unProtect();
        //保存文档
        String targetPath = getDecryptFilePath(fileName, fileName.contains(FileType.EXCEL.getType()) ? FileType.EXCEL : FileType.EXCEL_X);
        save(wb, targetPath);
        wb.dispose();
    }
}

Worksheet

package com.boonya.spring.mybatis.plus.config;

import com.boonya.spring.mybatis.plus.enums.FileType;
import com.spire.xls.FileFormat;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
import org.springframework.stereotype.Component;
import java.io.File;
@Component
public class WorkSheetEncryptDecrypt extends AbstractEncryptDecryptConfig{

    /**
     * 加密Excel
     * @param fileName
     * @param securityPwd
     */
    public void  encrypt(String fileName,String securityPwd){
        //加载Excel文档
        Workbook wb = new Workbook();
        wb.loadFromFile(TEMP_URL + File.separator + fileName);
        //获取工作表,加密
        Worksheet sheet = wb.getWorksheets().get(0);
        sheet.protect(securityPwd);
        //保存文档
        String targetPath = getEncryptFilePath(fileName ,fileName.contains(FileType.EXCEL.getType()) ? FileType.EXCEL : FileType.EXCEL_X);
        wb.saveToFile(targetPath, FileFormat.Version2013);
        wb.dispose();
    }

    /**
     * 解密Excel
     * @param fileName
     * @param securityPwd
     */
    public void  decrypt(String fileName,String securityPwd){
        //加载文档
        Workbook wb = new Workbook();
        wb.loadFromFile(ENCRYPT_URL + File.separator + fileName);
        //获取工作表
        Worksheet sheet = wb.getWorksheets().get(0);
        //解除工作表的密码保护
        sheet.unprotect(securityPwd);
        //保存文档
        String targetPath = getDecryptFilePath(fileName , fileName.contains(FileType.EXCEL.getType()) ? FileType.EXCEL : FileType.EXCEL_X);
        wb.saveToFile(targetPath);
        wb.dispose();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
软件介绍 本工具是对java class文件进行加密保护防止反编译的工具!本工具全面支持linux/unix/windows操作系统。 继推出v1.0版本后,获得了用户大量的支持与的反馈,我们再次推出本v2.0版,对加密算法进行了更大的改进,安全性大大提升! 众所周知,java编译后的class文件是一种中间字节字文件, 很容易被反编译工具反编译,而传统的java源代码保护方法基本都是采用混淆的方式, 但这样会带来很多麻烦,而且也不能真正保护class文件, 本工具是对class文件进行加密,采用jni的方式解密运行, 加密算法达到256位,加密后的class文件不可能被破解,反编译工具也对加密后的class文件无能为力。 运行方式: 运行时,要能正确的加载加密后的class文件, 必须使用我们提供的动态链接库classloader.dll(windows操作系统)或者libclassloader.so(Linux、Unix操作系统)。 执行java时带上参数-agentlib:\classloader 注意此处不要后缀名.dll(或者.so)。 如: 我把classloader.dll放在C:\目录下; 运行加密后的class文件命令如下: windows下执行javajava -agentlib:C:\classloader Sample Linux、Unix等系列操作系统下执行javajava -agentlib:/home/classloader Sample 或者把libclassloader.so拷贝到如“/home/yzj/jdk1.6.0_23/jre/lib/i386/”这jdk的运行目录下, 然后执行java如:java -agentlib:classloader Sample 当然如果class文件加密,这样运行也不会出错! 应用场合: 独立的应用程序,运行java时,带上参数-agentlib:\classloader Tomcat、Jboss等Java application server修改启动脚本, 把执行java的命令行后面加上参数-agentlib:\classloader 适应环境: 操作系统:所有操作系统,Windows系统、Linux/Unix,只是运行时, 使用的动态链接库文件不一样而已,其它配置完全一样。 jdk必须1.5以上。 备注:如果下载站下载下来的程序有运行bug,请从上面两个下载地址更新软件。并给我们留言!谢谢... 升级提示:v2.1相比v2.0修改了一个注册bug。
文件加密解密算法(Java源码) java,file,算法,加密解密,java源码 package com.crypto.encrypt; import java.security.SecureRandom; import java.io.*; import javax.crypto.spec.DESKeySpec; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.Cipher; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.security.spec.InvalidKeySpecException; import javax.crypto.NoSuchPaddingException; import javax.crypto.BadPaddingException; import javax.crypto.IllegalBlockSizeException; import java.lang.reflect.Constructor; import java.security.spec.KeySpec; import java.lang.reflect.InvocationTargetException; public class EncryptData { private String keyfile=null; public EncryptData() { } public EncryptData(String keyfile) { this.keyfile=keyfile; } /** * 加密文件 * @param filename String 源路径 * @param filenamekey String 加密后的路径 */ public void createEncryptData(String filename,String filenamekey) throws IllegalStateException, IllegalBlockSizeException, BadPaddingException, NoSuchPaddingException, InvalidKeySpecException, NoSuchAlgorithmException, InvalidKeyException, IOException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, ClassNotFoundException, IllegalStateException, IllegalBlockSizeException, BadPaddingException, NoSuchPaddingException, InvalidKeySpecException, NoSuchAlgorithmException, InvalidKeyException, IOException { //验证keyfile if(keyfile==null || keyfile.equals("")) { throw new NullPointerException("无效的key文件路径"); } encryptData(filename,filenamekey); } /** * 加密文件 * @param filename String 原始的类文件 * @param encryptfile String 加密后的类文件 * @throws IOException * @throws InvalidKeyException * @throws NoSuchAlgorithmException * @throws InvalidKeySpecException * @throws NoSuchPaddingException * @throws NoSuchAlgorithmException * @throws BadPaddingException * @throws IllegalBlockSizeException * @throws IllegalStateException */ private void encryptData(String filename,String encryptfile) throws IOException, InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, NoSuchAlgorithmException, BadPaddingException, IllegalBlockSizeException, IllegalStateException, ClassNotFoundException, SecurityException, NoSuchMethodException, InvocationTargetException, IllegalArgumentException, IllegalAccessException, InstantiationException { byte data[]=Util.readFile(filename); // 执行加密操作 byte encryptedClassData[] = getencryptData(data); // 保存加密后的文件,覆盖原有的类文件。 Util.writeFile(encryptedClassData,encryptfile); } /** * 直接获得加密数据 * @param bytes byte[] * @throws IllegalStateException * @throws IllegalBlockSizeException * @throws BadPaddingException * @throws InvalidKeyException * @throws NoSuchPaddingException * @throws InvalidKeySpecException * @throws NoSuchAlgorithmException * @throws InstantiationException * @throws IllegalAccessException * @throws IllegalArgumentException * @throws InvocationTargetException * @throws NoSuchMethodException * @throws SecurityException * @throws ClassNotFoundException * @throws IOException * @return byte[] */ public byte[] createEncryptData(byte[] bytes) throws IllegalStateException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException, NoSuchPaddingException, InvalidKeySpecException, NoSuchAlgorithmException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, ClassNotFoundException, IOException { bytes=getencryptData(bytes); return bytes; } private byte[] getencryptData(byte[] bytes) throws IOException, ClassNotFoundException, SecurityException, NoSuchMethodException, InvocationTargetException, IllegalArgumentException, IllegalAccessException, InstantiationException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, IllegalStateException { // 产生一个可信任的随机数源 SecureRandom sr = new SecureRandom(); //从密钥文件key Filename中得到密钥数据 byte[] rawKeyData = Util.readFile(keyfile); // 从原始密钥数据创建DESKeySpec对象 Class classkeyspec=Class.forName(Util.getValue("keyspec")); Constructor constructor = classkeyspec.getConstructor(new Class[]{byte[].class}); KeySpec dks = (KeySpec) constructor.newInstance(new Object[]{rawKeyData}); // 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(Util.getAlgorithm()); SecretKey key = keyFactory.generateSecret(dks); // Cipher对象实际完成加密操作 Cipher cipher = Cipher.getInstance(Util.getAlgorithm()); // 用密钥初始化Cipher对象 cipher.init(Cipher.ENCRYPT_MODE, key, sr); // 执行加密操作 bytes = cipher.doFinal(bytes); // 返回字节数组 return bytes; } /** * 设置key文件路径 * @param keyfile String */ public void setKeyFile(String keyfile) { this.keyfile=keyfile; } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值