admin管理员组

文章数量:1639675

首先,需要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) {
        

本文标签: 加密文件文件PGP