Source Code Cross Referenced for AsymmetricEncryptionEngine.java in  » Database-Client » DBBrowser » org » dbbrowser » security » 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 » Database Client » DBBrowser » org.dbbrowser.security 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.dbbrowser.security;
002:
003:        import infrastructure.logging.Log;
004:        import java.security.InvalidAlgorithmParameterException;
005:        import java.security.InvalidKeyException;
006:        import java.security.NoSuchAlgorithmException;
007:        import java.security.spec.InvalidKeySpecException;
008:        import javax.crypto.BadPaddingException;
009:        import javax.crypto.Cipher;
010:        import javax.crypto.IllegalBlockSizeException;
011:        import javax.crypto.NoSuchPaddingException;
012:        import javax.crypto.SecretKey;
013:        import javax.crypto.SecretKeyFactory;
014:        import javax.crypto.spec.PBEKeySpec;
015:        import javax.crypto.spec.PBEParameterSpec;
016:        import org.apache.axis.encoding.Base64;
017:
018:        /**
019:         * Class used to encrypt plaintext using asymmetric encryption
020:         * @author amangat
021:         *
022:         */
023:        public class AsymmetricEncryptionEngine {
024:            /**
025:             * Uses asymmetric encryption to encrypt clearText into cypherText.  Base64 Encodes
026:             * the cypherText and returns the base 64 encoded string 
027:             * @param clearText
028:             * @param passPhrase
029:             * @throws EncryptionEngineException - if there is an error during encryption
030:             * @return
031:             */
032:            public static String encrypt(String clearText, String passPhrase)
033:                    throws EncryptionEngineException {
034:                String base64EncodedCipherText = null;
035:                Log.getInstance().debugMessage(
036:                        "Starting Asymmetric Encryption...",
037:                        AsymmetricEncryptionEngine.class.getName());
038:                try {
039:                    //Use a password based encryption
040:                    PBEKeySpec keySpec = new PBEKeySpec(passPhrase
041:                            .toCharArray());
042:
043:                    //Get a secret key using the key spec and the secret key factory
044:                    //Generate a key for DES encryption and MD5 checksum using the key spec
045:                    SecretKeyFactory keyFactory = SecretKeyFactory
046:                            .getInstance("PBEWithMD5AndDES");
047:                    SecretKey pbeSecretKey = keyFactory.generateSecret(keySpec);
048:
049:                    Cipher c = Cipher.getInstance(pbeSecretKey.getAlgorithm()); //Symmetric DES encryption
050:                    //Init using the salt and iteration count
051:                    byte[] salt = new byte[] { (byte) 8, (byte) 8, (byte) 8,
052:                            (byte) 8, (byte) 8, (byte) 8, (byte) 8, (byte) 8 };//8 byte long salt
053:                    PBEParameterSpec pbeParameterSpec = new PBEParameterSpec(
054:                            salt, 20);
055:                    c.init(Cipher.ENCRYPT_MODE, pbeSecretKey, pbeParameterSpec);
056:
057:                    byte[] cleartext = clearText.getBytes();
058:
059:                    // Encrypt the cleartext
060:                    byte[] ciphertextArray = c.doFinal(cleartext);
061:
062:                    //Print the encrypted text
063:                    Log.getInstance().debugMessage(
064:                            "Encrypted text: " + new String(ciphertextArray),
065:                            AsymmetricEncryptionEngine.class.getName());
066:
067:                    base64EncodedCipherText = Base64.encode(ciphertextArray);
068:                    Log.getInstance().debugMessage(
069:                            "base64EncodedString: " + base64EncodedCipherText,
070:                            AsymmetricEncryptionEngine.class.getName());
071:                } catch (NoSuchAlgorithmException exc) {
072:                    Log.getInstance().debugMessage(exc.getMessage(),
073:                            AsymmetricEncryptionEngine.class.getName());
074:                    exc.printStackTrace();
075:                    throw new EncryptionEngineException(exc);
076:                } catch (NoSuchPaddingException exc) {
077:                    Log.getInstance().debugMessage(exc.getMessage(),
078:                            AsymmetricEncryptionEngine.class.getName());
079:                    exc.printStackTrace();
080:                    throw new EncryptionEngineException(exc);
081:                } catch (InvalidKeyException exc) {
082:                    Log.getInstance().debugMessage(exc.getMessage(),
083:                            AsymmetricEncryptionEngine.class.getName());
084:                    exc.printStackTrace();
085:                    throw new EncryptionEngineException(exc);
086:                } catch (BadPaddingException exc) {
087:                    Log.getInstance().debugMessage(exc.getMessage(),
088:                            AsymmetricEncryptionEngine.class.getName());
089:                    exc.printStackTrace();
090:                    throw new EncryptionEngineException(exc);
091:                } catch (IllegalBlockSizeException exc) {
092:                    Log.getInstance().debugMessage(exc.getMessage(),
093:                            AsymmetricEncryptionEngine.class.getName());
094:                    exc.printStackTrace();
095:                    throw new EncryptionEngineException(exc);
096:                } catch (InvalidAlgorithmParameterException exc) {
097:                    Log.getInstance().debugMessage(exc.getMessage(),
098:                            AsymmetricEncryptionEngine.class.getName());
099:                    exc.printStackTrace();
100:                    throw new EncryptionEngineException(exc);
101:                } catch (InvalidKeySpecException exc) {
102:                    Log.getInstance().debugMessage(exc.getMessage(),
103:                            AsymmetricEncryptionEngine.class.getName());
104:                    exc.printStackTrace();
105:                    throw new EncryptionEngineException(exc);
106:                }
107:
108:                return base64EncodedCipherText;
109:            }
110:
111:            /**
112:             * Uses asymmetric encryption to decrypt cypherText into clearText.  
113:             * clearText is a base64 encoded string which is first decoded and then decrypted  
114:             * @param clearText
115:             * @param passPhrase
116:             * @throws EncryptionEngineException
117:             * @return
118:             */
119:            public static String decrypt(String cypherTextInBase64Encoding,
120:                    String passPhrase) throws EncryptionEngineException {
121:                String cleartext = null;
122:                Log.getInstance().debugMessage(
123:                        "Starting Asymmetric Decryption...",
124:                        AsymmetricEncryptionEngine.class.getName());
125:                try {
126:                    byte[] base64DecodedByteArray = Base64
127:                            .decode(cypherTextInBase64Encoding);
128:                    Log.getInstance().debugMessage(
129:                            "Base64 decoded string: "
130:                                    + new String(base64DecodedByteArray),
131:                            AsymmetricEncryptionEngine.class.getName());
132:
133:                    PBEKeySpec keySpec = new PBEKeySpec(passPhrase
134:                            .toCharArray());
135:
136:                    //Get a secret key using the key spec and the secret key factory
137:                    //Generate a key for DES encryption and MD5 checksum using the key spec
138:                    SecretKeyFactory keyFactory = SecretKeyFactory
139:                            .getInstance("PBEWithMD5AndDES");
140:                    SecretKey pbeSecretKey = keyFactory.generateSecret(keySpec);
141:                    Cipher c = Cipher.getInstance(pbeSecretKey.getAlgorithm()); //Asymmetric DES encryption			
142:                    byte[] salt = new byte[] { (byte) 8, (byte) 8, (byte) 8,
143:                            (byte) 8, (byte) 8, (byte) 8, (byte) 8, (byte) 8 };//8 byte long salt
144:                    PBEParameterSpec pbeParameterSpec = new PBEParameterSpec(
145:                            salt, 20);
146:
147:                    c.init(Cipher.DECRYPT_MODE, pbeSecretKey, pbeParameterSpec);
148:
149:                    // Decrypt the ciphertext
150:                    byte[] recreatedClearText = c
151:                            .doFinal(base64DecodedByteArray);
152:                    cleartext = new String(recreatedClearText);
153:                    Log.getInstance().debugMessage(
154:                            "Finished recreating clearText",
155:                            AsymmetricEncryptionEngine.class.getName());
156:                } catch (InvalidAlgorithmParameterException exc) {
157:                    Log.getInstance().debugMessage(exc.getMessage(),
158:                            AsymmetricEncryptionEngine.class.getName());
159:                    exc.printStackTrace();
160:                    throw new EncryptionEngineException(exc);
161:                } catch (InvalidKeySpecException exc) {
162:                    Log.getInstance().debugMessage(exc.getMessage(),
163:                            AsymmetricEncryptionEngine.class.getName());
164:                    exc.printStackTrace();
165:                    throw new EncryptionEngineException(exc);
166:                } catch (NoSuchAlgorithmException exc) {
167:                    Log.getInstance().debugMessage(exc.getMessage(),
168:                            AsymmetricEncryptionEngine.class.getName());
169:                    exc.printStackTrace();
170:                    throw new EncryptionEngineException(exc);
171:                } catch (NoSuchPaddingException exc) {
172:                    Log.getInstance().debugMessage(exc.getMessage(),
173:                            AsymmetricEncryptionEngine.class.getName());
174:                    exc.printStackTrace();
175:                    throw new EncryptionEngineException(exc);
176:                } catch (InvalidKeyException exc) {
177:                    Log.getInstance().debugMessage(exc.getMessage(),
178:                            AsymmetricEncryptionEngine.class.getName());
179:                    exc.printStackTrace();
180:                    throw new EncryptionEngineException(exc);
181:                } catch (BadPaddingException exc) {
182:                    Log.getInstance().debugMessage(exc.getMessage(),
183:                            AsymmetricEncryptionEngine.class.getName());
184:                    exc.printStackTrace();
185:                    throw new EncryptionEngineException(exc);
186:                } catch (IllegalBlockSizeException exc) {
187:                    Log.getInstance().debugMessage(exc.getMessage(),
188:                            AsymmetricEncryptionEngine.class.getName());
189:                    exc.printStackTrace();
190:                    throw new EncryptionEngineException(exc);
191:                }
192:
193:                return cleartext;
194:            }
195:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.