夢幻之旅

          DEBUG - 天道酬勤

             :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            671 隨筆 :: 6 文章 :: 256 評論 :: 0 Trackbacks

           

          import java.security.InvalidKeyException;
          import java.security.NoSuchAlgorithmException;
          import java.security.SecureRandom;

          import javax.crypto.Cipher;
          import javax.crypto.KeyGenerator;
          import javax.crypto.NoSuchPaddingException;
          import javax.crypto.SecretKey;
          import javax.crypto.spec.SecretKeySpec;

          public class AESUtil
          {
              
          public static String encrypt(String content, String password)
              
          {
                  String encryptResult 
          = null;
                  
          try
                  
          {
                      Cipher cipher 
          = AESUtil.initCipher(Cipher.ENCRYPT_MODE, password);
                      
          byte[] byteContent = content.getBytes("UTF8");
                      
          byte[] result = cipher.doFinal(byteContent);
                      encryptResult 
          = parseByte2HexStr(result);
                  }

                  
          catch (Exception e)
                  
          {
                      e.printStackTrace();
                  }

                  
          return encryptResult;
              }

              
              
          public static String decrypt(String content, String password)
              
          {
                  String decryptResult 
          = null;
                  
          try
                  
          {
                      Cipher cipher 
          = AESUtil.initCipher(Cipher.DECRYPT_MODE, password);
                      
          byte[] result = cipher.doFinal(parseHexStr2Byte(content));
                      
          return decryptResult = new String(result, "UTF8");
                  }

                  
          catch (Exception ex)
                  
          {
                      ex.printStackTrace();
                  }

                  
          return decryptResult;
              }

              
              
          private static String parseByte2HexStr(byte buf[])
              
          {
                  StringBuffer sb 
          = new StringBuffer();
                  
          for (int i = 0; i < buf.length; i++)
                  
          {
                      String hex 
          = Integer.toHexString(buf[i] & 0xFF);
                      
          if (hex.length() == 1)
                      
          {
                          hex 
          = '0' + hex;
                      }

                      sb.append(hex.toUpperCase());
                  }

                  
          return sb.toString();
              }

              
              
          private static byte[] parseHexStr2Byte(String hexStr)
              
          {
                  
          if (hexStr.length() < 1)
                      
          return null;
                  
          byte[] result = new byte[hexStr.length() / 2];
                  
          for (int i = 0; i < hexStr.length() / 2; i++)
                  
          {
                      
          int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
                      
          int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2),
                              
          16);
                      result[i] 
          = (byte) (high * 16 + low);
                  }

                  
          return result;
              }

              
              
          private static Cipher initCipher(int model, String password)
                      
          throws NoSuchAlgorithmException, NoSuchPaddingException,
                      InvalidKeyException
              
          {
                  KeyGenerator kgen 
          = KeyGenerator.getInstance("AES");
                  kgen.init(
          128new SecureRandom(password.getBytes()));
                  SecretKey secretKey 
          = kgen.generateKey();
                  
          byte[] enCodeFormat = secretKey.getEncoded();
                  SecretKeySpec key 
          = new SecretKeySpec(enCodeFormat, "AES");
                  Cipher cipher 
          = Cipher.getInstance("AES");
                  cipher.init(model, key);
                  
          return cipher;
              }

              
              
          public static void main(String[] args)
              
          {
                  String content 
          = "123456惠萬鵬!@#$%^&*()_+|";
                  String password 
          = "";
                  
          // 加密
                  System.out.println("加密前:" + content);
                  String result 
          = AESUtil.encrypt(content, password);
                  System.out.println(
          "加密后:" + result);
                  
          // 解密
                  System.out.println("解密后:" + AESUtil.decrypt(result, password));
              }

          }

          posted on 2010-05-28 15:37 HUIKK 閱讀(203) 評論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 澎湖县| 鄂尔多斯市| 阳春市| 阿鲁科尔沁旗| 宁德市| 邵武市| 襄汾县| 鄢陵县| 盐边县| 海淀区| 开原市| 天台县| 那曲县| 镇雄县| 新疆| 莱芜市| 开原市| 山西省| 平武县| 繁昌县| 阿拉尔市| 关岭| 阳山县| 莱阳市| 开鲁县| 噶尔县| 丹东市| 英吉沙县| 县级市| 江源县| 乌鲁木齐县| 阳东县| 康保县| 青阳县| 自贡市| 龙胜| 永定县| 民权县| 云霄县| 华蓥市| 丽江市|