Source Code Cross Referenced for AbstractJCEEncryptionStrategy.java in  » ESB » mule » org » mule » 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 » ESB » mule » org.mule.security 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $Id: AbstractJCEEncryptionStrategy.java 10808 2008-02-14 20:36:57Z acooke $
003:         * --------------------------------------------------------------------------------------
004:         * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
005:         *
006:         * The software in this package is published under the terms of the CPAL v1.0
007:         * license, a copy of which has been included with this distribution in the
008:         * LICENSE.txt file.
009:         */
010:
011:        package org.mule.security;
012:
013:        import org.mule.api.lifecycle.InitialisationException;
014:        import org.mule.api.lifecycle.LifecycleTransitionResult;
015:        import org.mule.api.security.CryptoFailureException;
016:        import org.mule.config.i18n.CoreMessages;
017:        import org.mule.util.Base64;
018:
019:        import java.security.GeneralSecurityException;
020:        import java.security.spec.AlgorithmParameterSpec;
021:        import java.security.spec.KeySpec;
022:
023:        import javax.crypto.Cipher;
024:        import javax.crypto.SecretKey;
025:
026:        import org.apache.commons.logging.Log;
027:        import org.apache.commons.logging.LogFactory;
028:
029:        /**
030:         * A JCE based encryption strategy. It also provides base64 encoding of
031:         * encrypted/decrypted data by setting the base64encoding attribute.
032:         */
033:        public abstract class AbstractJCEEncryptionStrategy extends
034:                AbstractNamedEncryptionStrategy {
035:            /**
036:             * logger used by this class
037:             */
038:            protected transient Log logger = LogFactory.getLog(getClass());
039:
040:            protected KeySpec keySpec;
041:            protected SecretKey secretKey;
042:            protected Cipher encryptCipher;
043:            protected Cipher decryptCipher;
044:
045:            protected String algorithm = null;
046:
047:            protected boolean base64Encoding = true;
048:
049:            public LifecycleTransitionResult initialise()
050:                    throws InitialisationException {
051:                if (algorithm == null) {
052:                    throw new InitialisationException(CoreMessages
053:                            .objectIsNull("Algorithm"), this );
054:                } else {
055:                    logger.debug("Using encryption algorithm: " + algorithm);
056:                }
057:
058:                keySpec = createKeySpec();
059:
060:                try {
061:                    secretKey = getSecretKey();
062:                    // Create Ciphers
063:                    encryptCipher = Cipher.getInstance(getAlgorithm());
064:                    decryptCipher = Cipher.getInstance(getAlgorithm());
065:
066:                    AlgorithmParameterSpec paramSpec = createAlgorithmParameterSpec();
067:                    if (paramSpec != null) {
068:                        encryptCipher.init(Cipher.ENCRYPT_MODE, secretKey,
069:                                paramSpec);
070:                        decryptCipher.init(Cipher.DECRYPT_MODE, secretKey,
071:                                paramSpec);
072:                    } else {
073:                        encryptCipher.init(Cipher.ENCRYPT_MODE, secretKey);
074:                        decryptCipher.init(Cipher.DECRYPT_MODE, secretKey);
075:                    }
076:
077:                } catch (Exception e) {
078:                    throw new InitialisationException(CoreMessages
079:                            .failedToCreate("encryption ciphers"), e, this );
080:                }
081:                return LifecycleTransitionResult.OK;
082:            }
083:
084:            protected abstract SecretKey getSecretKey()
085:                    throws GeneralSecurityException;
086:
087:            public byte[] encrypt(byte[] data, Object info)
088:                    throws CryptoFailureException {
089:                try {
090:                    byte[] buf = encryptCipher.doFinal(data);
091:                    if (base64Encoding) {
092:                        return Base64.encodeBytes(buf).getBytes();
093:                    } else {
094:                        return buf;
095:                    }
096:                } catch (Exception e) {
097:                    throw new CryptoFailureException(this , e);
098:                }
099:            }
100:
101:            public byte[] decrypt(byte[] data, Object info)
102:                    throws CryptoFailureException {
103:                try {
104:                    byte[] dec = data;
105:                    if (base64Encoding) {
106:                        dec = Base64.decode(new String(data));
107:                    }
108:                    return decryptCipher.doFinal(dec);
109:                } catch (Exception e) {
110:                    throw new CryptoFailureException(this , e);
111:                }
112:            }
113:
114:            public String getAlgorithm() {
115:                return algorithm;
116:            }
117:
118:            public void setAlgorithm(String algorithm) {
119:                this .algorithm = algorithm;
120:            }
121:
122:            public String toString() {
123:                StringBuffer buf = new StringBuffer();
124:                buf.append("Algorithm=").append(algorithm);
125:                return buf.toString();
126:            }
127:
128:            public boolean isBase64Encoding() {
129:                return base64Encoding;
130:            }
131:
132:            public void setBase64Encoding(boolean base64Encoding) {
133:                this .base64Encoding = base64Encoding;
134:            }
135:
136:            protected abstract KeySpec createKeySpec();
137:
138:            protected abstract AlgorithmParameterSpec createAlgorithmParameterSpec();
139:
140:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.