⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 digitalenvelop.java

📁 说明: 1、里面有什么: 1.1、org.bouncycastle.*下的所有软件是bouncycastle组织开发的软件包 1.2、org.infosecurity.*下的软件包括
💻 JAVA
字号:
package org.infosecurity.cryptography;

/**
 * <p>Title: 数字信封类 </p>
 * <p>Description: 数字信封类 </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: 中信信息安全组织(CISO)</p>
 * @author 张荣华
 * @version 1.0.2003.1028
 */

import org.infosecurity.cryptography.*;
import org.infosecurity.cryptography.*;
import java.io.*;
import java.security.SecureRandom;
public class DigitalEnvelop {
    //=========================================================
    // 定义错误类型,以有意义的常量代替数字
    public final static int S_OK                  = 1;
                                              /* 成功          */
    public final static int S_FILENOTFOUND_ERROR  = -1;
                                              /* 文件没找到     */
    public final static int S_IO_EXCEPTION        = -2;
                                              /* IO异常        */
    public final static int S_DATALENGTH_EXCEPTION = -3;
                                              /* RSA公钥加密异常*/
    public final static int S_PWD_FILE_ERROR = -4;
                                              /* 不是密码文件   */
    //==========================================================
    // 定义变量名
    CRsa          rsa = new CRsa();
    FileEncryptor fe  = null;
    SecureRandom  sr  = new SecureRandom();
    public DigitalEnvelop()
    {
    }
    /**
     *  对数据封装
     *  @param strInFileName  输入文件名
     *  @param strOutFileName 输出文件名
     *  @param puk            公钥
     *  @param strPwdFileName 密码被加密后存贮文件名
     *  @return 成功 S_OK,失败 其它
     */
    public int MakeSeal(String strInFileName,   /* 输入文件名            */
                        String strOutFileName,  /* 输出文件名            */
                        RSAPublicKey puk,       /* 公钥                 */
                        String strPwdFileName)  /* 密码被加密后存贮文件名  */
    {
        byte[] pwd              = new byte[8];
        byte[] iv               = new byte[8];
        byte[] cipher           = null;
        FileInputStream  fis    = null;
        FileOutputStream fos    = null;
        FileOutputStream fosPwd = null;
        sr.nextBytes(pwd);
        fe                      = new FileEncryptor(pwd);
        try {
            fis                 = new FileInputStream(strInFileName);
        }
        catch (FileNotFoundException ex) {
            return S_FILENOTFOUND_ERROR;
        }
        try {
            fos                 = new FileOutputStream(strOutFileName);
        }
        catch (FileNotFoundException ex) {
            try {
                fis.close();
                fos.close();
            }
            catch (IOException ex1) {}
            finally{
                return S_FILENOTFOUND_ERROR;
            }
        }
        try {
            fosPwd              = new FileOutputStream(strPwdFileName);
        }
        catch (FileNotFoundException ex) {
            try {
                fosPwd.close();
                fis.close();
                fos.close();
            }
            catch (IOException ex1) {}
            finally{
                return S_FILENOTFOUND_ERROR;
            }
        }
        sr.nextBytes(iv);
        try {
            fe.EncryptCBC(iv,fis,fos);
        }
        catch (IOException ex) {
            try {
                fis.close();
                fos.close();
            }
            catch (IOException ex1) { }
            finally
            {
                return S_IO_EXCEPTION;
            }
        }
        finally
        {
            try {
                fis.close();
                fos.close();
            }
            catch (IOException ex1) {
                return S_IO_EXCEPTION;
            }
        }
        rsa.setPublicKey(puk);
        try {
            cipher = rsa.PublicKeyEncrypt(pwd,8);
        }
        catch (DataLengthException ex) {
            return S_DATALENGTH_EXCEPTION;
        }
        try {
            fosPwd.write(cipher,0,cipher.length);
        }
        catch (IOException ex) {}
        finally
        {
            try {
                fosPwd.close();
            }
            catch (IOException ex2) {
                return S_IO_EXCEPTION;
            }
        }
        return S_OK;
    }
    /**
     *  对数据解封装
     *  @param strInFileName  输入文件名(密文)
     *  @param strOutFileName 输出文件名
     *  @param puk            公钥
     *  @param strPwdFileName 密码被加密后存贮文件名
     *  @return 成功 S_OK,失败 其它
     */
    public int OpenSeal(String strInFileName,   /* 输入文件名            */
                        String strOutFileName,  /* 输出文件名            */
                        RSAPrivateKey pvk,      /* 公钥                 */
                        String strPwdFileName)  /* 密码文件的文件名       */
    {

        byte[]           pwd     = null;
        byte[]           cipher  = new byte[512];
        FileInputStream  fis     = null;
        FileOutputStream fos     = null;
        FileInputStream  fisPwd  = null;
        int              real_length = 0;
        int              excp    = -1;
        /*    处理文件的输入/输出异常              */
        try {
            fis                 = new FileInputStream(strInFileName);
        }
        catch (FileNotFoundException ex) {
            return S_FILENOTFOUND_ERROR;
        }
        try {
            fos                 = new FileOutputStream(strOutFileName);
        }
        catch (FileNotFoundException ex) {
            try {
                fis.close();
                fos.close();
            }
            catch (IOException ex1) {}
            finally{
                return S_FILENOTFOUND_ERROR;
            }
        }
        try {
            fisPwd = new FileInputStream(strPwdFileName);
        }
        catch (FileNotFoundException ex) {
            try {
                fisPwd.close();
                fis.close();
                fos.close();
            }
            catch (IOException ex1) {}
            finally{
                return S_FILENOTFOUND_ERROR;
            }
        }
        /* 读密码 */
        try {
            real_length = fisPwd.read(cipher);
        }
        catch (IOException ex) {
            excp=1;
        }
        finally
        {
            try {
                fisPwd.close();
            }
            catch (IOException ex) {
                return S_IO_EXCEPTION;
            }
            if(excp==1)
                return S_IO_EXCEPTION;
        }
        rsa.setPrivateKey(pvk);

        try {
            byte [] real_data = new byte[real_length];
            System.arraycopy(cipher,0,real_data,0,real_length);
            pwd = rsa.PrivateKeyDecrypt(real_data,real_length);
        }
        catch (DataLengthException ex) {
            try {
                fis.close();
                fos.close();
            }
            catch (IOException ex1) {}
            return S_DATALENGTH_EXCEPTION;
        }
        if(pwd.length!=8){
            try {
                fis.close();
                fos.close();
            }
            catch (IOException ex1) {}
            return S_PWD_FILE_ERROR;
        }
        fe = new FileEncryptor(pwd);
        try {
            fe.DecryptCBC(fis,fos);
        }
        catch (IOException ex) {
            try {
                fis.close();
                fos.close();
            }
            catch (IOException ex1) {}
            return S_IO_EXCEPTION;
        }
        finally
        {
            try {
                fis.close();
                fos.close();
            }
            catch (IOException ex1)
            {
                return S_IO_EXCEPTION;
            }
        }
        return S_OK;
    }
    /**
     *  数字信封测试程序
     *  @author 张荣华
     *
     */
    public static void main(String[] args) {
        DigitalEnvelop de = new DigitalEnvelop();
        // 生成公私钥对
        //de.rsa.generateKeyPair(512);
        RSAPublicKey  puk = null;
        RSAPrivateKey pvk = null;
        // 读入公私钥
        try {
            puk=de.rsa.readPuk("E:\\JavaProj\\temp\\puk.key");
            pvk=de.rsa.readPvk("E:\\JavaProj\\temp\\pvk.key");
        }
        catch (Exception ex) {
            System.out.println("读公/私钥出现异常!");
        }
        if(puk==null)
        {
            System.out.println("puk为空!");
            return ;
        }
        if(pvk==null)
        {
            System.out.println("pvk为空!");
            return ;
        }
        long time1 = System.currentTimeMillis();
        // 数据封装
//        de.MakeSeal("E:\\JavaProj\\temp\\plaintext.txt",
//                    "E:\\JavaProj\\temp\\ciphertext.txt",
//                    puk,
//                    "E:\\JavaProj\\temp\\EVPpwd.txt"
//                    );
        // 数据解封装
        de.OpenSeal("E:\\JavaProj\\temp\\ciphertext.txt",
                    "E:\\JavaProj\\temp\\resulttext.txt",
                    pvk,
                    "E:\\JavaProj\\temp\\EVPpwd.txt"
                    );
        long dif = System.currentTimeMillis()-time1;
        long tm = dif/1000;
        long  min = tm/60;
        long  sec = tm%60;

        System.out.println("共花费时间:"+dif+"毫秒("+min+"分"+sec+"秒)");
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -