甜咖啡

          我的IT空間

          java實現DES加密算法

          一、java實現DES加密算法
          為了實現一對密鑰對整個項目所有加密解密文件都適用的方法,采用先生成一對密鑰.保存到xml文件中,以后獲得私匙和公鑰只需要從xml文件中取得就可以了.
          /**
          * 把成生的一對密鑰保存到DesKey.xml文件中
          */
          public static void saveDesKey(){     
              try {
                  SecureRandom sr = new SecureRandom();
                  //為我們選擇的DES算法生成一個KeyGenerator對象
                  KeyGenerator kg = KeyGenerator.getInstance ("DES" );
                  kg.init (sr);
                  FileOutputStream fos = new FileOutputStream("C:/DesKey.xml");
                ObjectOutputStream oos = new ObjectOutputStream(fos);
                  //生成密鑰
                  Key key = kg.generateKey();
                oos.writeObject(key);
                oos.close();
              } catch (Exception e) {
                e.printStackTrace();
              }
          }




          獲取密鑰方法如下:

          /**

          * 獲得DES加密的密鑰。在交易處理的過程中應該定時更
          * 換密鑰。需要JCE的支持,如果jdk版本低于1.4,則需要
          * 安裝jce-1_2_2才能正常使用。
          * @return   Key 返回對稱密鑰
          */
              public static Key getKey() {
                  Key kp = null;
                  try {
                        String fileName = "conf/DesKey.xml";
                        InputStream is = DesUtil.class.getClassLoader()
                                .getResourceAsStream(fileName);
                        ObjectInputStream oos = new ObjectInputStream(is);
                        kp = (Key) oos.readObject();
                        oos.close();
                  } catch (Exception e) {
                        e.printStackTrace();
                  }
                  return kp;
              }


          文件采用DES算法加密文件

          /**
          * 文件file進行加密并保存目標文件destFile中

          * @param file
          *         
          要加密的文件 如c:/test/srcFile.txt
          * @param destFile
          *         加密后存放的文件名 如c:/加密后文件
          .txt
          */

          public static void encrypt(String file, String destFile) throws Exception {

                  Cipher cipher = Cipher.getInstance("DES");

                  cipher.init(Cipher.ENCRYPT_MODE, getKey());

                  InputStream is = new FileInputStream(file);

                  OutputStream out = new FileOutputStream(dest);

                  CipherInputStream cis = new CipherInputStream(is, cipher);

                  byte[] buffer = new byte[1024];

                  int r;

                  while ((r = cis.read(buffer)) > 0) {

                        out.write(buffer, 0, r);

                  }

                  cis.close();

                  is.close();

                  out.close();

              }


          文件采用DES算法解密文件


          /**
          文件file進行加密并保存目標文件destFile中

          * @param file
          *         
          已加密的文件 如c:/加密后文件.txt
          * @param destFile
          *         解密后存放的文件名 如c:/ test/解密后文件
          .txt
          */

          public static void decrypt(String file, String dest) throws Exception {
                  Cipher cipher = Cipher.getInstance("DES");
                  cipher.init(Cipher.DECRYPT_MODE, getKey());
                  InputStream is = new FileInputStream(file);
                  OutputStream out = new FileOutputStream(dest);
                  CipherOutputStream cos = new CipherOutputStream(out, cipher);
                  byte[] buffer = new byte[1024];
                  int r;
                  while ((r = is.read(buffer)) >= 0) {
                        cos.write(buffer, 0, r);
                  }
                  cos.close();
                  out.close();
                  is.close();
              } 

          posted on 2013-03-29 15:58 甜咖啡 閱讀(443) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          導航

          <2013年3月>
          242526272812
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          統計

          常用鏈接

          留言簿(1)

          我參與的團隊

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 上林县| 长泰县| 探索| 丽水市| 吴江市| 海口市| 太和县| 佛山市| 黄山市| 招远市| 阳信县| 新龙县| 麟游县| 张掖市| 武邑县| 新建县| 黑山县| 利辛县| 太谷县| 白朗县| 大足县| 新昌县| 禄劝| 老河口市| 威海市| 甘孜| 横峰县| 临夏市| 滨海县| 军事| 河南省| 北碚区| 曲麻莱县| 新干县| 永丰县| 老河口市| 海丰县| 海兴县| 本溪市| 乐清市| 封丘县|