001: /*
002: * This program is free software; you can redistribute it and/or modify
003: * it under the terms of the GNU General Public License as published by
004: * the Free Software Foundation; either version 2 of the License, or
005: * (at your option) any later version.
006: *
007: * This program is distributed in the hope that it will be useful,
008: * but WITHOUT ANY WARRANTY; without even the implied warranty of
009: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: * GNU Library General Public License for more details.
011: *
012: * You should have received a copy of the GNU General Public License
013: * along with this program; if not, write to the Free Software
014: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
015: */
016: package dlog4j.util;
017:
018: import java.security.*;
019: import java.util.regex.Matcher;
020: import java.util.regex.Pattern;
021:
022: import javax.crypto.Cipher;
023: import javax.crypto.SecretKey;
024: import javax.crypto.SecretKeyFactory;
025: import javax.crypto.spec.DESKeySpec;
026:
027: /**
028: * 字符串工具集合
029: * 此类需要JRE 1.4
030: * @author Liudong
031: */
032: public class StringUtils extends org.apache.commons.lang.StringUtils {
033:
034: private static final String PASSWORD_CRYPT_KEY = "__jDlog_";
035: private final static String DES = "DES";
036:
037: static Pattern emailer;
038:
039: /**
040: * 判断是不是一个合法的电子邮件地址
041: * @param email
042: * @return
043: */
044: public static boolean isEmail(String email) {
045: if (emailer == null) {
046: String check = "^([a-z0-9A-Z]+[-|\\._]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
047: emailer = Pattern.compile(check);
048: }
049: Matcher matcher = emailer.matcher(email);
050: return matcher.matches();
051: }
052:
053: /**
054: * 加密
055: * @param src 数据源
056: * @param key 密钥,长度必须是8的倍数
057: * @return 返回加密后的数据
058: * @throws Exception
059: */
060: public static byte[] encrypt(byte[] src, byte[] key)
061: throws Exception {
062: // DES算法要求有一个可信任的随机数源
063: SecureRandom sr = new SecureRandom();
064: // 从原始密匙数据创建DESKeySpec对象
065: DESKeySpec dks = new DESKeySpec(key);
066: // 创建一个密匙工厂,然后用它把DESKeySpec转换成
067: // 一个SecretKey对象
068: SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
069: SecretKey securekey = keyFactory.generateSecret(dks);
070: // Cipher对象实际完成加密操作
071: Cipher cipher = Cipher.getInstance(DES);
072: // 用密匙初始化Cipher对象
073: cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
074: // 现在,获取数据并加密
075: // 正式执行加密操作
076: return cipher.doFinal(src);
077: }
078:
079: /**
080: * 解密
081: * @param src 数据源
082: * @param key 密钥,长度必须是8的倍数
083: * @return 返回解密后的原始数据
084: * @throws Exception
085: */
086: public static byte[] decrypt(byte[] src, byte[] key)
087: throws Exception {
088: // DES算法要求有一个可信任的随机数源
089: SecureRandom sr = new SecureRandom();
090: // 从原始密匙数据创建一个DESKeySpec对象
091: DESKeySpec dks = new DESKeySpec(key);
092: // 创建一个密匙工厂,然后用它把DESKeySpec对象转换成
093: // 一个SecretKey对象
094: SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
095: SecretKey securekey = keyFactory.generateSecret(dks);
096: // Cipher对象实际完成解密操作
097: Cipher cipher = Cipher.getInstance(DES);
098: // 用密匙初始化Cipher对象
099: cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
100: // 现在,获取数据并解密
101: // 正式执行解密操作
102: return cipher.doFinal(src);
103: }
104:
105: /**
106: * 密码解密
107: * @param data
108: * @return
109: * @throws Exception
110: */
111: public final static String decrypt(String data) {
112: try {
113: return new String(decrypt(hex2byte(data.getBytes()),
114: PASSWORD_CRYPT_KEY.getBytes()));
115: } catch (Exception e) {
116: }
117: return null;
118: }
119:
120: /**
121: * 密码加密
122: * @param password
123: * @return
124: * @throws Exception
125: */
126: public final static String encrypt(String password) {
127: try {
128: return byte2hex(encrypt(password.getBytes(),
129: PASSWORD_CRYPT_KEY.getBytes()));
130: } catch (Exception e) {
131: }
132: return null;
133: }
134:
135: /**
136: * 二行制转字符串
137: * @param b
138: * @return
139: */
140: public static String byte2hex(byte[] b) {
141: String hs = "";
142: String stmp = "";
143: for (int n = 0; n < b.length; n++) {
144: stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
145: if (stmp.length() == 1)
146: hs = hs + "0" + stmp;
147: else
148: hs = hs + stmp;
149: }
150: return hs.toUpperCase();
151: }
152:
153: public static byte[] hex2byte(byte[] b) {
154: if ((b.length % 2) != 0)
155: throw new IllegalArgumentException("长度不是偶数");
156: byte[] b2 = new byte[b.length / 2];
157: for (int n = 0; n < b.length; n += 2) {
158: String item = new String(b, n, 2);
159: b2[n / 2] = (byte) Integer.parseInt(item, 16);
160: }
161: return b2;
162: }
163:
164: /**
165: * 大小写无关的字符串替换策略
166: * @param str
167: * @param src
168: * @param obj
169: * @return
170: */
171: public static String replaceIgnoreCase(String str, String src,
172: String obj) {
173: String l_str = str.toLowerCase();
174: String l_src = src.toLowerCase();
175: int fromIdx = 0;
176: StringBuffer result = new StringBuffer();
177: do {
178: int idx = l_str.indexOf(l_src, fromIdx);
179: if (idx == -1)
180: break;
181: result.append(str.substring(fromIdx, idx));
182: result.append(obj);
183: fromIdx = idx + src.length();
184: } while (true);
185: result.append(str.substring(fromIdx));
186: return result.toString();
187: }
188:
189: public static void main(String[] args) {
190: String pwd = "测试dasdfaaaaaaa";
191: String data = encrypt(pwd);
192: System.out.println("data=" + data);
193: pwd = decrypt(data);
194: System.out.println("pwd=" + pwd);
195:
196: System.out.println(replaceIgnoreCase(
197: "public class StringUtilsTest extends TestCase",
198: "clAss", "inTerface"));
199: }
200: }
|