天行健,君子以自強不息

          BlogJava 首頁 新隨筆 聯系 聚合 管理
            12 Posts :: 0 Stories :: 2 Comments :: 0 Trackbacks
          package com.yill;

          import java.security.SecureRandom;

          import javax.crypto.Cipher;
          import javax.crypto.SecretKey;
          import javax.crypto.SecretKeyFactory;
          import javax.crypto.spec.DESKeySpec;

          public class YillDES
          {
              
          private static final byte[] key = "HelloWorld".getBytes();

              
          public static void main(String[] args)
              
          {
                  String myInfo 
          = "http://www.aygfsteel.com/yill/";

                  System.out.println(
          "MyInfo is " + myInfo);

                  
          byte[] encrypted = encrypt(myInfo.getBytes());

                  System.out.println(
          "Encrypted myInfo is " + new String(encrypted));

                  System.out.println(
          "Decrypted myInfo is "
                          
          + new String(decrypt(encrypted)));
              }


              
          public static byte[] encrypt(byte[] origin)
              
          {
                  
          byte[] encrypted = null;
                  
          try
                  
          {
                      
          // 生成密鑰
                      SecretKeyFactory factory = SecretKeyFactory.getInstance("DES");
                      SecretKey secretKey 
          = factory.generateSecret(new DESKeySpec(key));

                      
          // 初始化加密工具cipher
                      Cipher cipher = Cipher.getInstance("DES");
                      cipher.init(Cipher.ENCRYPT_MODE, secretKey, 
          new SecureRandom());
                      encrypted 
          = cipher.doFinal(origin);
                  }

                  
          catch (Exception e)
                  
          {
                      
          // TODO Auto-generated catch block
                      e.printStackTrace();
                  }


                  
          return encrypted;
              }


              
          public static byte[] decrypt(byte[] encrypted)
              
          {
                  
          byte[] decrypted = null;
                  
          try
                  
          {
                      
          // 生成密鑰
                      SecretKeyFactory factory = SecretKeyFactory.getInstance("DES");
                      SecretKey secretKey 
          = factory.generateSecret(new DESKeySpec(key));

                      
          // 初始化加密工具cipher
                      Cipher cipher = Cipher.getInstance("DES");
                      cipher.init(Cipher.DECRYPT_MODE, secretKey, 
          new SecureRandom());
                      decrypted 
          = cipher.doFinal(encrypted);
                  }

                  
          catch (Exception e)
                  
          {
                      
          // TODO Auto-generated catch block
                      e.printStackTrace();
                  }


                  
          return decrypted;
              }

          }



          運行結果:
          MyInfo is http://www.aygfsteel.com/yill/
          Encrypted myInfo is X耾%
          孑塖籫ㄎn勯錍雴?:啌狒>堷z
          Decrypted myInfo is http://www.aygfsteel.com/yill/
          posted on 2008-03-16 13:58 yill 閱讀(471) 評論(0)  編輯  收藏

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


          網站導航:
           
          主站蜘蛛池模板: 普宁市| 榆林市| 宁安市| 宁强县| 延吉市| 彰化市| 岑巩县| 泰来县| 外汇| 高密市| 共和县| 苏尼特右旗| 鹿泉市| 桂东县| 昌黎县| 灵寿县| 交口县| 朝阳县| 阜南县| 芜湖市| 勃利县| 图们市| 新和县| 乌恰县| 彭州市| 江都市| 和林格尔县| 怀仁县| 玛纳斯县| 博乐市| 江门市| 闽侯县| 天峻县| 茶陵县| 天台县| 区。| 井陉县| 政和县| 富顺县| 博湖县| 沙田区|