Source Code Cross Referenced for DesEncriptionImpl.java in  » J2EE » Enhydra-Demos » org » enhydra » dm » 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 » J2EE » Enhydra Demos » org.enhydra.dm.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.enhydra.dm.util;
002:
003:        import java.security.spec.AlgorithmParameterSpec;
004:        import java.util.Properties;
005:
006:        import javax.crypto.Cipher;
007:        import javax.crypto.SecretKey;
008:        import javax.crypto.SecretKeyFactory;
009:        import javax.crypto.spec.PBEKeySpec;
010:        import javax.crypto.spec.PBEParameterSpec;
011:
012:        import org.enhydra.dm.api.exceptions.BaseException;
013:        import org.enhydra.dm.api.loggers.Log;
014:        import org.enhydra.dm.api.util.DesEncription;
015:
016:        import com.lutris.util.Config;
017:
018:        /**
019:         * Encrypting with DES Using a Pass Phrase This class uses pass phrase (a string of
020:         * multiple words) for encryption. Web page:
021:         * http://www.exampledepot.com/egs/javax.crypto/PassKey.html?l=rel
022:         */
023:
024:        public class DesEncriptionImpl implements  DesEncription {
025:
026:            public Cipher ecipher;
027:
028:            public Cipher dcipher;
029:
030:            public final String encodeSecretDefault = "SomeStringValue";
031:
032:            public final String encodeEncodingDefault = "UTF-8";
033:
034:            public String encoding;
035:
036:            public String secret;
037:
038:            public Log logger;
039:
040:            // 8-byte Salt
041:            byte[] salt = { (byte) 0xA9, (byte) 0x9B, (byte) 0xC8, (byte) 0x32,
042:                    (byte) 0x56, (byte) 0x35, (byte) 0xE3, (byte) 0x03 };
043:
044:            // Iteration count
045:            int iterationCount = 19;
046:
047:            /**
048:             * @param strPass Secret phrase parameter
049:             * @param strPass Encoding parameter
050:             * @throws BaseException
051:             */
052:            public DesEncriptionImpl(String strPass, String strEncoding) {
053:
054:                if (strPass != null && !"".equalsIgnoreCase(strPass)) {
055:                    secret = strPass;
056:                }
057:
058:                if (strEncoding != null && !"".equalsIgnoreCase(strEncoding)) {
059:                    encoding = strEncoding;
060:                }
061:
062:            }
063:
064:            public DesEncriptionImpl() {
065:
066:            }
067:
068:            private void init() throws BaseException {
069:                try {
070:                    // Create the key
071:                    PBEKeySpec keySpec = new PBEKeySpec(secret.toCharArray(),
072:                            salt, iterationCount);
073:                    SecretKey key = SecretKeyFactory.getInstance(
074:                            "PBEWithMD5AndDES").generateSecret(keySpec);
075:                    ecipher = Cipher.getInstance(key.getAlgorithm());
076:                    dcipher = Cipher.getInstance(key.getAlgorithm());
077:
078:                    // Prepare the parameter to the ciphers
079:                    AlgorithmParameterSpec paramSpec = new PBEParameterSpec(
080:                            salt, iterationCount);
081:
082:                    // Create the ciphers
083:                    ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
084:                    dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
085:
086:                } catch (Exception e) {
087:                    throw new BaseException(e.getMessage());
088:                }
089:
090:            }
091:
092:            /**
093:             * @param strPass Secret phrase parameter
094:             * @throws BaseException
095:             */
096:            public DesEncriptionImpl(String strPass) {
097:                this (strPass, null);
098:            }
099:
100:            /**
101:             * Encrypt the parametar
102:             * 
103:             * @param str parametar to encrypt
104:             * @param strEncoding type of encoding
105:             * @return encrypted object
106:             * @throws BaseException
107:             */
108:            public String encrypt(String str, String strEncoding)
109:                    throws BaseException {
110:
111:                if (null != logger) {
112:                    logger.log(Log.DEBUG, "Encrypt method for parameters!");
113:                }
114:                try {
115:
116:                    byte[] input = str.getBytes(strEncoding);
117:
118:                    // Encrypt
119:                    byte[] encript = ecipher.doFinal(input);
120:
121:                    // Encode bytes to base64 to get a string
122:                    return Base64.encodeBytes(encript, 16);
123:
124:                } catch (Exception e) {
125:                    throw new BaseException(e.getMessage());
126:                }
127:
128:            }
129:
130:            /**
131:             * Encrypt the parametar
132:             * 
133:             * @param str parametar to encrypt
134:             * @return encrypted object
135:             * @throws BaseException
136:             */
137:            public String encrypt(String str) throws BaseException {
138:                return encrypt(str, encoding);
139:            }
140:
141:            /**
142:             * Decrypt the parametar
143:             * 
144:             * @param strEncripted encrypted parametar
145:             * @param strEncoding type of encoding
146:             * @return decrypted object
147:             * @throws BaseException
148:             */
149:            public String decrypt(String strEncrypted, String strEncoding)
150:                    throws BaseException {
151:
152:                if (null != logger) {
153:                    logger.log(Log.DEBUG, "Decrypt method for parameters!");
154:                }
155:                try {
156:                    // Decode base64 to get bytes
157:                    byte[] input = Base64.decode(strEncrypted, 16);
158:
159:                    // Decrypt
160:                    byte[] decrypted = dcipher.doFinal(input);
161:
162:                    // Decode
163:                    return new String(decrypted, strEncoding);
164:
165:                } catch (Exception e) {
166:                    throw new BaseException(e.getMessage());
167:                }
168:
169:            }
170:
171:            /**
172:             * Decrypt the parametar
173:             * 
174:             * @param strEncripted encrypted parametar
175:             * @return decrypted object
176:             * @throws BaseException
177:             */
178:            public String decrypt(String strEncrypted) throws BaseException {
179:                return decrypt(strEncrypted, encoding);
180:            }
181:
182:            public static void main(String[] args) {
183:                DesEncriptionImpl de;
184:                try {
185:                    de = new DesEncriptionImpl("SomeStringValue");
186:                    de.init();
187:                    String parameter = EnhydraDMConstants.ACTION + "=edit&"
188:                            + EnhydraDMConstants.DOCUMENT_ID + "=1000000";
189:                    System.out.println("1. " + parameter);
190:                    parameter = de.encrypt(parameter);
191:                    System.out.println("2. " + parameter);
192:                    parameter = de.decrypt(parameter);
193:                    System.out.println("3. " + parameter);
194:                    System.out.println();
195:                } catch (BaseException e) {
196:
197:                    e.printStackTrace();
198:                }
199:            }
200:
201:            public void configure(Config config) throws BaseException {
202:                try {
203:                    this .secret = config.getString("Encode.Secret");
204:                    this .encoding = config.getString("Encode.Encoding");
205:                    if (this .secret == null || "".equalsIgnoreCase(this .secret)) {
206:                        this .secret = encodeSecretDefault;
207:                    }
208:
209:                    if (this .encoding == null
210:                            || "".equalsIgnoreCase(this .encoding)) {
211:                        this .encoding = encodeEncodingDefault;
212:                    }
213:
214:                    init();
215:
216:                } catch (Exception e) {
217:                    throw new BaseException(e.getMessage());
218:                }
219:            }
220:
221:            public void configure(Properties properties) throws BaseException {
222:                try {
223:                    this .secret = properties.getProperty("Encode.Secret");
224:                    this .encoding = properties.getProperty("Encode.Encoding");
225:                    // Create the key
226:                    if (this .secret == null || "".equalsIgnoreCase(this .secret)) {
227:                        this .secret = encodeSecretDefault;
228:                    }
229:
230:                    if (this .encoding == null
231:                            || "".equalsIgnoreCase(this .encoding)) {
232:                        this .encoding = encodeEncodingDefault;
233:                    }
234:                    init();
235:
236:                } catch (Exception e) {
237:                    throw new BaseException(e.getMessage());
238:                }
239:            }
240:
241:            /**
242:             * 
243:             */
244:            public Log getLogger() {
245:                return logger;
246:            }
247:
248:            /**
249:             * 
250:             */
251:            public void setLogger(Log logger) {
252:                this.logger = logger;
253:            }
254:
255:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.