Source Code Cross Referenced for StringUtils.java in  » Portal » DLOG4J » dlog4j » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Portal » DLOG4J » dlog4j.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.