七郎's JavaBlog

          草木竹石皆可為劒。至人之用人若鏡,不將不迎,應而不藏,故能勝物而不傷。
          posts - 60, comments - 14, trackbacks - 0, articles - 0

          java工具包

          Posted on 2007-04-16 15:31 七郎歸來 閱讀(521) 評論(0)  編輯  收藏

          import javax.crypto.SecretKey;
          import javax.crypto.spec.SecretKeySpec;
          import java.net.URL;
          import java.net.HttpURLConnection;
          import java.io.PrintWriter;
          import java.io.BufferedReader;
          import java.io.InputStreamReader;

          /**
           * Created by IntelliJ IDEA.
           * User: zhengzhg
           * Mail: snake_country@sina.com
           * Date: 2004-10-13
           * Time: 15:30:28
           * To change this template use File | Settings | File Templates.
           * 常用工具包。包括生成各種密碼隨機串,加密解密,編碼解碼,執行url等
           */

          public class CryptTool {
              /**
               * 生成密碼.
               * @param count 密碼位數
               * @param letters 是否包含字符
               * @param numbers 是否包含數字
               * @return String password
               */
              public static String getPassword(int count, boolean letters, boolean numbers) {
                  return org.apache.commons.lang.RandomStringUtils.random(count, letters, numbers);
              }

              /**
               * 生成字符數字混合的密碼.
               * @param count 密碼位數
               * @return String password
               */
              private static String getPassword(int count) {
                  return getPassword(count, true, true);
              }


              /**
               * 生成純數字密碼.
               * @param count 密碼位數
               * @return String password
               */
              public static String getPasswordOfNumber(int count) {
                  return getPassword(count, false, true);
              }

              /**
               * 生成純字符密碼.
               * @param count 密碼位數
               * @return String password
               */
              public static String getPasswordOfCharacter(int count) {
                  return getPassword(count, true, false);
              }

              /**
               * 生成3DES密鑰.
               * @param key_byte seed key
               * @throws Exception
               * @return javax.crypto.SecretKey Generated DES key
               */
              public static javax.crypto.SecretKey genDESKey(byte[] key_byte) throws Exception {
                  SecretKey k = new SecretKeySpec(key_byte, "DESede");

                  return k;
              }

              /**
               * 3DES 解密(byte[]).
               * @param key SecretKey
               * @param crypt byte[]
               * @throws Exception
               * @return byte[]
               */
              public static byte[] desDecrypt(javax.crypto.SecretKey key, byte[] crypt) throws Exception {
                  javax.crypto.Cipher cipher = javax.crypto.Cipher.getInstance("DESede");
                  cipher.init(javax.crypto.Cipher.DECRYPT_MODE, key);

                  return cipher.doFinal(crypt);
              }

              /**
               * 3DES 解密(String).
               * @param key SecretKey
               * @param crypt byte[]
               * @throws Exception
               * @return byte[]
               */
              public static String desDecrypt(javax.crypto.SecretKey key, String crypt) throws Exception {
                  return new String(desDecrypt(key, crypt.getBytes()));
              }

              /**
               * 3DES加密(byte[]).
               * @param key SecretKey
               * @param src byte[]
               * @throws Exception
               * @return byte[]
               */
              public static byte[] desEncrypt(javax.crypto.SecretKey key, byte[] src) throws Exception {
                  javax.crypto.Cipher cipher = javax.crypto.Cipher.getInstance("DESede");
                  cipher.init(javax.crypto.Cipher.ENCRYPT_MODE, key);

                  return cipher.doFinal(src);
              }

              /**
               * 3DES加密(String).
               * @param key SecretKey
               * @param src byte[]
               * @throws Exception
               * @return byte[]
               */
              public static String desEncrypt(javax.crypto.SecretKey key, String src) throws Exception {
                  return new String(desEncrypt(key, src.getBytes()));
              }

              /**
               * MD5 摘要計算(byte[]).
               * @param src byte[]
               * @throws Exception
               * @return byte[] 16 bit digest
               */
              public static byte[] md5Digest(byte[] src) throws Exception {
                  java.security.MessageDigest alg = java.security.MessageDigest.getInstance("MD5");
                  // MD5 is 16 bit message digest

                  return alg.digest(src);
              }

              /**
               * MD5 摘要計算(String).
               * @param src String
               * @throws Exception
               * @return String
               */
              public static String md5Digest(String src) throws Exception {
                  return new String(md5Digest(src.getBytes()));
              }

              /**
               * BASE64 編碼.
               * @param src String inputed string
               * @return String returned string
               */
              public static String base64Encode(String src) {
                  sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();

                  return encoder.encode(src.getBytes());
              }

              /**
               * BASE64 編碼(byte[]).
               * @param src byte[] inputed string
               * @return String returned string
               */
              public static String base64Encode(byte[] src) {
                  sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();

                  return encoder.encode(src);
              }

              /**
               * BASE64 解碼.
               * @param src String inputed string
               * @return String returned string
               */
              public static String base64Decode(String src) {
                  sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();

                  try {
                      return new String(decoder.decodeBuffer(src));
                  } catch (Exception ex) {
                      return null;
                  }
              }

              /**
               * BASE64 解碼(to byte[]).
               * @param src String inputed string
               * @return String returned string
               */
              public static byte[] base64DecodeToBytes(String src) {
                  sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();

                  try {
                      return decoder.decodeBuffer(src);
                  } catch (Exception ex) {
                      return null;
                  }
              }

              /**
               * 對給定字符進行 URL 編碼GB2312.
               * @param src String
               * @return String
               */
              public static String urlEncode(String src) {
                  return urlEncode(src, "GB2312");
              }

              /**
               * 對給定字符進行 URL 解碼GB2312
               * @param value 解碼前的字符串
               * @return 解碼后的字符串
               */
              public static String urlDecode(String value) {
                  return urlDecode(value, "GB2312");
              }

              /**
               * 對給定字符進行 URL 編碼.
               * @param src String
               * @param coder 字符編碼格式(GB2312/GBK)
               * @return String
               */
              public static String urlEncode(String src, String coder) {
                  try {
                      src = java.net.URLEncoder.encode(src, coder);

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

                  return src;
              }

              /**
               * 對給定字符進行 URL 解碼
               * @param value 解碼前的字符串
               * @param coder 字符編碼格式(GB2312/GBK)
               * @return 解碼后的字符串
               */
              public static String urlDecode(String value, String coder) {
                  try {
                      return java.net.URLDecoder.decode(value, coder);
                  } catch (Exception ex) {
                      ex.printStackTrace();
                  }

                  return value;
              }

              /**
               * 執行給定url
               * @param urlString 給定的url
               * @return 返回值
               */
              public static String executeURL(String urlString) throws Exception {
                  StringBuffer document = new StringBuffer();
                  URL url = new URL(urlString);
                  URLConnection conn = url.openConnection();
                  BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));

                  String line = null;
                  while ((line = reader.readLine()) != null)
                      document.append(line + "\n");

                  reader.close();
                 
                  return document.toString();
              }


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


          網站導航:
           
          主站蜘蛛池模板: 保康县| 满洲里市| 奉新县| 荆门市| 五莲县| 海宁市| 平遥县| 大英县| 安义县| 平南县| 洪洞县| 潜江市| 巍山| 白山市| 闽清县| 商城县| 贵港市| 年辖:市辖区| 百色市| 宁化县| 闽清县| 扶余县| 峨山| 且末县| 江川县| 津市市| 乐平市| 临泉县| 当涂县| 运城市| 黄石市| 冀州市| 东乡| 浏阳市| 扎鲁特旗| 祥云县| 额济纳旗| 郴州市| 宣恩县| 姚安县| 志丹县|