DigitalEnvelop.java
上传用户:lior1029
上传日期:2013-05-07
资源大小:209k
文件大小:9k
源码类别:

CA认证

开发平台:

Java

  1. package org.infosecurity.cryptography;
  2. /**
  3.  * <p>Title: 数字信封类 </p>
  4.  * <p>Description: 数字信封类 </p>
  5.  * <p>Copyright: Copyright (c) 2003</p>
  6.  * <p>Company: 中信信息安全组织(CISO)</p>
  7.  * @author 张荣华
  8.  * @version 1.0.2003.1028
  9.  */
  10. import org.infosecurity.cryptography.*;
  11. import org.infosecurity.cryptography.*;
  12. import java.io.*;
  13. import java.security.SecureRandom;
  14. public class DigitalEnvelop {
  15.     //=========================================================
  16.     // 定义错误类型,以有意义的常量代替数字
  17.     public final static int S_OK                  = 1;
  18.                                               /* 成功          */
  19.     public final static int S_FILENOTFOUND_ERROR  = -1;
  20.                                               /* 文件没找到     */
  21.     public final static int S_IO_EXCEPTION        = -2;
  22.                                               /* IO异常        */
  23.     public final static int S_DATALENGTH_EXCEPTION = -3;
  24.                                               /* RSA公钥加密异常*/
  25.     public final static int S_PWD_FILE_ERROR = -4;
  26.                                               /* 不是密码文件   */
  27.     //==========================================================
  28.     // 定义变量名
  29.     CRsa          rsa = new CRsa();
  30.     FileEncryptor fe  = null;
  31.     SecureRandom  sr  = new SecureRandom();
  32.     public DigitalEnvelop()
  33.     {
  34.     }
  35.     /**
  36.      *  对数据封装
  37.      *  @param strInFileName  输入文件名
  38.      *  @param strOutFileName 输出文件名
  39.      *  @param puk            公钥
  40.      *  @param strPwdFileName 密码被加密后存贮文件名
  41.      *  @return 成功 S_OK,失败 其它
  42.      */
  43.     public int MakeSeal(String strInFileName,   /* 输入文件名            */
  44.                         String strOutFileName,  /* 输出文件名            */
  45.                         RSAPublicKey puk,       /* 公钥                 */
  46.                         String strPwdFileName)  /* 密码被加密后存贮文件名  */
  47.     {
  48.         byte[] pwd              = new byte[8];
  49.         byte[] iv               = new byte[8];
  50.         byte[] cipher           = null;
  51.         FileInputStream  fis    = null;
  52.         FileOutputStream fos    = null;
  53.         FileOutputStream fosPwd = null;
  54.         sr.nextBytes(pwd);
  55.         fe                      = new FileEncryptor(pwd);
  56.         try {
  57.             fis                 = new FileInputStream(strInFileName);
  58.         }
  59.         catch (FileNotFoundException ex) {
  60.             return S_FILENOTFOUND_ERROR;
  61.         }
  62.         try {
  63.             fos                 = new FileOutputStream(strOutFileName);
  64.         }
  65.         catch (FileNotFoundException ex) {
  66.             try {
  67.                 fis.close();
  68.                 fos.close();
  69.             }
  70.             catch (IOException ex1) {}
  71.             finally{
  72.                 return S_FILENOTFOUND_ERROR;
  73.             }
  74.         }
  75.         try {
  76.             fosPwd              = new FileOutputStream(strPwdFileName);
  77.         }
  78.         catch (FileNotFoundException ex) {
  79.             try {
  80.                 fosPwd.close();
  81.                 fis.close();
  82.                 fos.close();
  83.             }
  84.             catch (IOException ex1) {}
  85.             finally{
  86.                 return S_FILENOTFOUND_ERROR;
  87.             }
  88.         }
  89.         sr.nextBytes(iv);
  90.         try {
  91.             fe.EncryptCBC(iv,fis,fos);
  92.         }
  93.         catch (IOException ex) {
  94.             try {
  95.                 fis.close();
  96.                 fos.close();
  97.             }
  98.             catch (IOException ex1) { }
  99.             finally
  100.             {
  101.                 return S_IO_EXCEPTION;
  102.             }
  103.         }
  104.         finally
  105.         {
  106.             try {
  107.                 fis.close();
  108.                 fos.close();
  109.             }
  110.             catch (IOException ex1) {
  111.                 return S_IO_EXCEPTION;
  112.             }
  113.         }
  114.         rsa.setPublicKey(puk);
  115.         try {
  116.             cipher = rsa.PublicKeyEncrypt(pwd,8);
  117.         }
  118.         catch (DataLengthException ex) {
  119.             return S_DATALENGTH_EXCEPTION;
  120.         }
  121.         try {
  122.             fosPwd.write(cipher,0,cipher.length);
  123.         }
  124.         catch (IOException ex) {}
  125.         finally
  126.         {
  127.             try {
  128.                 fosPwd.close();
  129.             }
  130.             catch (IOException ex2) {
  131.                 return S_IO_EXCEPTION;
  132.             }
  133.         }
  134.         return S_OK;
  135.     }
  136.     /**
  137.      *  对数据解封装
  138.      *  @param strInFileName  输入文件名(密文)
  139.      *  @param strOutFileName 输出文件名
  140.      *  @param puk            公钥
  141.      *  @param strPwdFileName 密码被加密后存贮文件名
  142.      *  @return 成功 S_OK,失败 其它
  143.      */
  144.     public int OpenSeal(String strInFileName,   /* 输入文件名            */
  145.                         String strOutFileName,  /* 输出文件名            */
  146.                         RSAPrivateKey pvk,      /* 公钥                 */
  147.                         String strPwdFileName)  /* 密码文件的文件名       */
  148.     {
  149.         byte[]           pwd     = null;
  150.         byte[]           cipher  = new byte[512];
  151.         FileInputStream  fis     = null;
  152.         FileOutputStream fos     = null;
  153.         FileInputStream  fisPwd  = null;
  154.         int              real_length = 0;
  155.         int              excp    = -1;
  156.         /*    处理文件的输入/输出异常              */
  157.         try {
  158.             fis                 = new FileInputStream(strInFileName);
  159.         }
  160.         catch (FileNotFoundException ex) {
  161.             return S_FILENOTFOUND_ERROR;
  162.         }
  163.         try {
  164.             fos                 = new FileOutputStream(strOutFileName);
  165.         }
  166.         catch (FileNotFoundException ex) {
  167.             try {
  168.                 fis.close();
  169.                 fos.close();
  170.             }
  171.             catch (IOException ex1) {}
  172.             finally{
  173.                 return S_FILENOTFOUND_ERROR;
  174.             }
  175.         }
  176.         try {
  177.             fisPwd = new FileInputStream(strPwdFileName);
  178.         }
  179.         catch (FileNotFoundException ex) {
  180.             try {
  181.                 fisPwd.close();
  182.                 fis.close();
  183.                 fos.close();
  184.             }
  185.             catch (IOException ex1) {}
  186.             finally{
  187.                 return S_FILENOTFOUND_ERROR;
  188.             }
  189.         }
  190.         /* 读密码 */
  191.         try {
  192.             real_length = fisPwd.read(cipher);
  193.         }
  194.         catch (IOException ex) {
  195.             excp=1;
  196.         }
  197.         finally
  198.         {
  199.             try {
  200.                 fisPwd.close();
  201.             }
  202.             catch (IOException ex) {
  203.                 return S_IO_EXCEPTION;
  204.             }
  205.             if(excp==1)
  206.                 return S_IO_EXCEPTION;
  207.         }
  208.         rsa.setPrivateKey(pvk);
  209.         try {
  210.             byte [] real_data = new byte[real_length];
  211.             System.arraycopy(cipher,0,real_data,0,real_length);
  212.             pwd = rsa.PrivateKeyDecrypt(real_data,real_length);
  213.         }
  214.         catch (DataLengthException ex) {
  215.             try {
  216.                 fis.close();
  217.                 fos.close();
  218.             }
  219.             catch (IOException ex1) {}
  220.             return S_DATALENGTH_EXCEPTION;
  221.         }
  222.         if(pwd.length!=8){
  223.             try {
  224.                 fis.close();
  225.                 fos.close();
  226.             }
  227.             catch (IOException ex1) {}
  228.             return S_PWD_FILE_ERROR;
  229.         }
  230.         fe = new FileEncryptor(pwd);
  231.         try {
  232.             fe.DecryptCBC(fis,fos);
  233.         }
  234.         catch (IOException ex) {
  235.             try {
  236.                 fis.close();
  237.                 fos.close();
  238.             }
  239.             catch (IOException ex1) {}
  240.             return S_IO_EXCEPTION;
  241.         }
  242.         finally
  243.         {
  244.             try {
  245.                 fis.close();
  246.                 fos.close();
  247.             }
  248.             catch (IOException ex1)
  249.             {
  250.                 return S_IO_EXCEPTION;
  251.             }
  252.         }
  253.         return S_OK;
  254.     }
  255.     /**
  256.      *  数字信封测试程序
  257.      *  @author 张荣华
  258.      *
  259.      */
  260.     public static void main(String[] args) {
  261.         DigitalEnvelop de = new DigitalEnvelop();
  262.         // 生成公私钥对
  263.         //de.rsa.generateKeyPair(512);
  264.         RSAPublicKey  puk = null;
  265.         RSAPrivateKey pvk = null;
  266.         // 读入公私钥
  267.         try {
  268.             puk=de.rsa.readPuk("E:\JavaProj\temp\puk.key");
  269.             pvk=de.rsa.readPvk("E:\JavaProj\temp\pvk.key");
  270.         }
  271.         catch (Exception ex) {
  272.             System.out.println("读公/私钥出现异常!");
  273.         }
  274.         if(puk==null)
  275.         {
  276.             System.out.println("puk为空!");
  277.             return ;
  278.         }
  279.         if(pvk==null)
  280.         {
  281.             System.out.println("pvk为空!");
  282.             return ;
  283.         }
  284.         long time1 = System.currentTimeMillis();
  285.         // 数据封装
  286. //        de.MakeSeal("E:\JavaProj\temp\plaintext.txt",
  287. //                    "E:\JavaProj\temp\ciphertext.txt",
  288. //                    puk,
  289. //                    "E:\JavaProj\temp\EVPpwd.txt"
  290. //                    );
  291.         // 数据解封装
  292.         de.OpenSeal("E:\JavaProj\temp\ciphertext.txt",
  293.                     "E:\JavaProj\temp\resulttext.txt",
  294.                     pvk,
  295.                     "E:\JavaProj\temp\EVPpwd.txt"
  296.                     );
  297.         long dif = System.currentTimeMillis()-time1;
  298.         long tm = dif/1000;
  299.         long  min = tm/60;
  300.         long  sec = tm%60;
  301.         System.out.println("共花费时间:"+dif+"毫秒("+min+"分"+sec+"秒)");
  302.     }
  303. }