<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    甜咖啡

    我的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 甜咖啡 閱讀(447) 評論(0)  編輯  收藏


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


    網站導航:
     

    導航

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

    統計

    常用鏈接

    留言簿(1)

    我參與的團隊

    隨筆檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲精品岛国片在线观看| 中文字幕亚洲乱码熟女一区二区| 222www免费视频| 一区二区免费视频| 2021在线永久免费视频| 亚洲AV日韩精品久久久久久| 亚洲最新黄色网址| 精品国产亚洲一区二区三区在线观看| 又硬又粗又长又爽免费看| 国产偷伦视频免费观看| 成年轻人网站色免费看| 亚洲日韩国产一区二区三区| 亚洲人成在久久综合网站| 国产成人亚洲精品91专区高清| 华人在线精品免费观看| 亚洲国产AV无码专区亚洲AV| 国产精品亚洲综合五月天| jizz免费观看| 免费观看的a级毛片的网站| 最新国产AV无码专区亚洲| 99久久免费国产精品热| 成人毛片视频免费网站观看| 亚洲国产欧美日韩精品一区二区三区| 91免费在线视频| 久久亚洲精品国产精品| 老司机午夜性生免费福利| 人禽杂交18禁网站免费| 亚洲精品免费观看| a级大片免费观看| 亚洲色大成网站www永久| 在线观看黄片免费入口不卡| 精品亚洲麻豆1区2区3区| 免费毛片网站在线观看| 韩国免费a级作爱片无码| 免费在线视频一区| 亚洲中文字幕无码av永久| 最近最好最新2019中文字幕免费| 国产av无码专区亚洲av果冻传媒| 美丽姑娘免费观看在线观看中文版| 亚洲五月综合缴情在线观看| 一区二区三区免费在线视频|