1 16 package dlog4j.util; 17 18 import java.security.*; 19 import java.util.regex.Matcher ; 20 import java.util.regex.Pattern ; 21 22 import javax.crypto.Cipher; 23 import javax.crypto.SecretKey; 24 import javax.crypto.SecretKeyFactory; 25 import javax.crypto.spec.DESKeySpec; 26 27 32 public class StringUtils extends org.apache.commons.lang.StringUtils{ 33 34 private static final String PASSWORD_CRYPT_KEY = "__jDlog_"; 35 private final static String DES = "DES"; 36 37 static Pattern emailer; 38 39 44 public static boolean isEmail(String email){ 45 if(emailer==null){ 46 String check = "^([a-z0-9A-Z]+[-|\\._]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$"; 47 emailer = Pattern.compile(check); 48 } 49 Matcher matcher = emailer.matcher(email); 50 return matcher.matches(); 51 } 52 59 public static byte[] encrypt(byte[] src, byte[] key) 60 throws Exception { 61 SecureRandom sr = new SecureRandom(); 63 DESKeySpec dks = new DESKeySpec(key); 65 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES); 68 SecretKey securekey = keyFactory.generateSecret(dks); 69 Cipher cipher = Cipher.getInstance(DES); 71 cipher.init(Cipher.ENCRYPT_MODE, securekey, sr); 73 return cipher.doFinal(src); 76 } 77 78 85 public static byte[] decrypt(byte[] src, byte[] key) 86 throws Exception { 87 SecureRandom sr = new SecureRandom(); 89 DESKeySpec dks = new DESKeySpec(key); 91 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES); 94 SecretKey securekey = keyFactory.generateSecret(dks); 95 Cipher cipher = Cipher.getInstance(DES); 97 cipher.init(Cipher.DECRYPT_MODE, securekey, sr); 99 return cipher.doFinal(src); 102 } 103 109 public final static String decrypt(String data){ 110 try { 111 return new String (decrypt(hex2byte(data.getBytes()),PASSWORD_CRYPT_KEY.getBytes())); 112 }catch(Exception e) { 113 } 114 return null; 115 } 116 122 public final static String encrypt(String password){ 123 try { 124 return byte2hex(encrypt(password.getBytes(),PASSWORD_CRYPT_KEY.getBytes())); 125 }catch(Exception e) { 126 } 127 return null; 128 } 129 134 public static String byte2hex(byte[] b) { 135 String hs = ""; 136 String stmp = ""; 137 for (int n = 0; n < b.length; n++) { 138 stmp = (java.lang.Integer.toHexString(b[n] & 0XFF)); 139 if (stmp.length() == 1) 140 hs = hs + "0" + stmp; 141 else 142 hs = hs + stmp; 143 } 144 return hs.toUpperCase(); 145 } 146 147 public static byte[] hex2byte(byte[] b) { 148 if((b.length%2)!=0) 149 throw new IllegalArgumentException ("长度不是偶数"); 150 byte[] b2 = new byte[b.length/2]; 151 for (int n = 0; n < b.length; n+=2) { 152 String item = new String (b,n,2); 153 b2[n/2] = (byte)Integer.parseInt(item,16); 154 } 155 return b2; 156 } 157 158 165 public static String replaceIgnoreCase(String str, String src, String obj){ 166 String l_str = str.toLowerCase(); 167 String l_src = src.toLowerCase(); 168 int fromIdx = 0; 169 StringBuffer result = new StringBuffer (); 170 do{ 171 int idx = l_str.indexOf(l_src, fromIdx); 172 if(idx==-1) 173 break; 174 result.append(str.substring(fromIdx, idx)); 175 result.append(obj); 176 fromIdx = idx + src.length(); 177 }while(true); 178 result.append(str.substring(fromIdx)); 179 return result.toString(); 180 } 181 182 public static void main(String [] args) { 183 String pwd = "测试dasdfaaaaaaa"; 184 String data = encrypt(pwd); 185 System.out.println("data="+data); 186 pwd = decrypt(data); 187 System.out.println("pwd="+pwd); 188 189 System.out.println(replaceIgnoreCase("public class StringUtilsTest extends TestCase","clAss","inTerface")); 190 } 191 } | Popular Tags |