PGP加密文件和解密文件

首先,需要jar包如下: cryptix-jce-api.jar cryptix-jce-provider.jar cryptix-message-api.jar cryptix-openpgp-provider.jar cryptix-pki-api.jar然后需要写两个class,一个是PGP加密解密功能的类,一个是给其他类使用的工具类。 使用的时候调用工具类PGPEncryp...
摘要由CSDN通过智能技术生成

首先,需要jar包如下:
cryptix-jce-api.jar
cryptix-jce-provider.jar
cryptix-message-api.jar
cryptix-openpgp-provider.jar
cryptix-pki-api.jar

然后需要写两个class,一个是PGP加密解密功能的类,一个是给其他类使用的工具类。
使用的时候调用工具类PGPEncryptDecrypt就好了。

工具类PGPEncryptDecrypt :

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import cryptix.pki.KeyBundle;

public class PGPEncryptDecrypt {

    //解密文件路径
    private static String decryptFilePath = "";
    //解密后新生成的文件
    private static String decrypt_New = "";
    //解密后生成的新文件
    private static File decryptFile_New = null;
    //须解密文件
    private static File decryptFile = null;
    //须解密文件名
    private static String decryptFileName = "";

    //须加密文件
    private static File encryptFile = null;
    //加密后生成的新文件
    private static File encryptFile_New = null;
    //公钥证书
    private static File pubKey = null;
    // 私钥证书
    private static File prikey = null;
    // 证书密码
    private static String password = "";
    //原始文件路径
    private  static String plaintextPath = "";
    //原始文件文件名
    private static String plaintextFileName = "";
    //加密文件路径
    private static String encryptFilePath = "";
    //加密后新生成的文件
    private static String encrypt_newFileName = "";
    //证书文件路径
    private static String certFilePath = "";
    //公钥名
    private static String publicKeyFileName = "";
    //私钥名
    private static String privateKeyFileName = "";

    //将日期以 yyyyMMdd 的格式输出
    private static final SimpleDateFormat compactDateFormatter = new SimpleDateFormat("yyyyMMdd");
    private static String formatCompactDate(Date date) {
        if(date==null){
            return "";
        }
        return compactDateFormatter.format(date);
    }

    ///////////////////////////////////////////加密用////////////////////////////////////////////////

    //签名并加密文件,并且将签名加密后的文件内容写入到新的文件中
    public static void signEncrypt(String pswd,String orifilepath,String orifilename,String encryfilepath,String encryptnewFileName,String certpath,String pubkeyname,String prikeyname){
        System.out.println("encrypt--------------------");
        password = pswd;
        plaintextPath = orifilepath;
        plaintextFileName = orifilename;
        encryptFilePath = encryfilepath;
        encrypt_newFileName = encryptnewFileName;
        certFilePath = certpath;
        publicKeyFileName = pubkeyname;
        privateKeyFileName = prikeyname;
        encryptFile = new File(plaintextPath,plaintextFileName);
        pubKey = new File(certFilePath,publicKeyFileName);
        prikey = new File(certFilePath,privateKeyFileName);
        encryptFile_New = new File(encryptFilePath,encrypt_newFileName);

        if(!encryptFile.exists()){
            System.out.println("Encrypt file is not exists!");
            throw new RuntimeException("Encrypt file is not exists!");
        }
        String encode = getFilecharset(new File(plaintextPath,plaintextFileName));
        System.out.println("encode=>"+encode);
        System.out.println("++++++++++++++++++"+plaintextPath+plaintextFileName);
        System.out.println("++++++++++++++++++"+encryptFilePath+encrypt_newFileName);
        byte [] encrypts = pgpEncryptAndSign(IOUtil.read((plaintextPath+plaintextFileName) ,encode), pubKey, prikey, password);
        IOUtil.writeAsText( (encryptFilePath+encrypt_newFileName), new String(encrypts),encode);
    }

    /** 使用PGP证书签名和加密原始文件
     * 
     * @param plain
     *            原始文件
     * @param publicFile
     *            公钥
     * @param privateFile
     *            私钥
     * @param password
     *            私钥密码
     * @return 返回签名加密后的密文
     */
    private static byte[] pgpEncryptAndSign(byte[] plain, File publicKeyFile,File privateKeyFile, String password) {
        
  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 37
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 37
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值