java的加密和解密

package com.ysdq.cms.util;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key;

/**
 * 加密
 * @author sutong
 * @date 2018-06-26 11:34:00
 */
public class AESUtil {

    private static final String KEY = "#VmGRi";

    private static final Key KEY_SPEC = new SecretKeySpec(KEY.getBytes(), "AES");

    /**
     * 加密
     * @param str
     * @return
     */
    public static String encryt(String str) {
        // 根据密钥,对Cipher对象进行初始化,ENCRYPT_MODE表示加密模式
        try {
            Cipher c = Cipher.getInstance("AES");
            c.init(Cipher.ENCRYPT_MODE, KEY_SPEC);
            byte[] src = str.getBytes();
            // 加密,结果保存进cipherByte
            byte[] cipherByte = c.doFinal(src);
            return Base64.encodeBase64String(cipherByte);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 解密
     * @param deCodeStr
     * @return
     */
    public static String decrypt(String deCodeStr) {
        // 根据密钥,对Cipher对象进行初始化,DECRYPT_MODE表示解密模式
        try {
            if (null == deCodeStr){
                return null;
            }
            byte[] buff = Base64.decodeBase64(deCodeStr);
            Cipher c;
            c = Cipher.getInstance("AES");
            c.init(Cipher.DECRYPT_MODE, KEY_SPEC);
            byte[] cipherByte = c.doFinal(buff);
            return new String(cipherByte);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}

 

Java 提供了许多加密解密 PDF 文档的库。其中一些库是: 1. Apache PDFBox:它是一个开源库,可以用来创建、修改和提取 PDF 文档。它还提供了加密解密 PDF 文档的功能。 2. iText:它是一个广泛使用的 PDF 库,可以用来创建、修改和提取 PDF 文档。它还提供了加密解密 PDF 文档的功能。 以下是使用 Apache PDFBox 加密解密 PDF 文档的示例代码: 1. 加密 PDF 文档: ``` import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.encryption.AccessPermission; import org.apache.pdfbox.pdmodel.encryption.StandardProtectionPolicy; import java.io.File; import java.io.IOException; public class PDFEncryptor { public static void main(String[] args) throws IOException { // 加载 PDF 文档 PDDocument document = PDDocument.load(new File("example.pdf")); // 设置访问权限 AccessPermission ap = new AccessPermission(); ap.setCanPrint(false); // 设置加密策略 StandardProtectionPolicy spp = new StandardProtectionPolicy("password", "ownerpassword", ap); spp.setEncryptionKeyLength(128); // 应用加密策略 document.protect(spp); // 保存加密后的 PDF 文档 document.save("example-encrypted.pdf"); document.close(); } } ``` 2. 解密 PDF 文档: ``` import org.apache.pdfbox.pdmodel.PDDocument; import java.io.File; import java.io.IOException; public class PDFDecryptor { public static void main(String[] args) throws IOException { // 加载加密的 PDF 文档 PDDocument document = PDDocument.load(new File("example-encrypted.pdf"), "password"); // 解密 PDF 文档 document.setAllSecurityToBeRemoved(true); // 保存解密后的 PDF 文档 document.save("example-decrypted.pdf"); document.close(); } } ``` 这些示例代码使用 Apache PDFBox 库加密解密 PDF 文档。您可以根据需要使用其他库。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值