Source Code Cross Referenced for Encryption.java in  » Wiki-Engine » VeryQuickWiki » vqwiki » utils » 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 » Wiki Engine » VeryQuickWiki » vqwiki.utils 
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 latest version of the GNU Lesser General
004:        Public License as published by the Free Software Foundation;
005:
006:        This program is distributed in the hope that it will be useful,
007:        but WITHOUT ANY WARRANTY; without even the implied warranty of
008:        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
009:        GNU Lesser General Public License for more details.
010:
011:        You should have received a copy of the GNU Lesser General Public License
012:        along with this program (gpl.txt); if not, write to the Free Software
013:        Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
014:         */
015:
016:        package vqwiki.utils;
017:
018:        import javax.crypto.Cipher;
019:        import javax.crypto.SecretKey;
020:        import javax.crypto.SecretKeyFactory;
021:        import javax.crypto.spec.DESKeySpec;
022:        import org.apache.log4j.Logger;
023:        import org.apache.commons.codec.binary.Base64;
024:
025:        /**
026:         * Provide capability for encrypting and decrypting values.  Inspired by an
027:         * example from http://www.devx.com/assets/sourcecode/10387.zip.
028:         *
029:         * @author Ryan Holliday
030:         */
031:        public class Encryption {
032:
033:            private static Logger logger = Logger.getLogger(Encryption.class);
034:            public static final String DES_ALGORITHM = "DES";
035:            public static final String ENCRYPTION_KEY = "VQWiki Key 12345";
036:
037:            /**
038:             * Hide the constructor by making it private.
039:             */
040:            private Encryption() {
041:            }
042:
043:            /**
044:             * Encrypt a String value using the DES encryption algorithm.
045:             *
046:             * @param unencryptedString The unencrypted String value that is to be encrypted.
047:             * @return An encrypted version of the String that was passed to this method.
048:             */
049:            public static String encrypt(String unencryptedString)
050:                    throws Exception {
051:                if (unencryptedString == null
052:                        || unencryptedString.trim().length() == 0) {
053:                    return unencryptedString;
054:                }
055:                try {
056:                    SecretKey key = createKey();
057:                    Cipher cipher = Cipher.getInstance(key.getAlgorithm());
058:                    cipher.init(Cipher.ENCRYPT_MODE, key);
059:                    byte[] unencryptedBytes = unencryptedString
060:                            .getBytes("UTF8");
061:                    byte[] encryptedBytes = Base64.encodeBase64(cipher
062:                            .doFinal(unencryptedBytes));
063:                    return bytes2String(encryptedBytes);
064:                } catch (Exception e) {
065:                    logger.error("Encryption error while processing value '"
066:                            + unencryptedString + "'", e);
067:                    throw e;
068:                }
069:            }
070:
071:            /**
072:             * Unencrypt a String value using the DES encryption algorithm.
073:             *
074:             * @param encryptedString The encrypted String value that is to be unencrypted.
075:             * @return An unencrypted version of the String that was passed to this method.
076:             */
077:            public static String decrypt(String encryptedString) {
078:                if (encryptedString == null
079:                        || encryptedString.trim().length() <= 0) {
080:                    return encryptedString;
081:                }
082:                try {
083:                    SecretKey key = createKey();
084:                    Cipher cipher = Cipher.getInstance(key.getAlgorithm());
085:                    cipher.init(Cipher.DECRYPT_MODE, key);
086:                    byte[] encryptedBytes = encryptedString.getBytes("UTF8");
087:                    byte[] unencryptedBytes = cipher.doFinal(Base64
088:                            .decodeBase64(encryptedBytes));
089:                    return bytes2String(unencryptedBytes);
090:                } catch (Exception e) {
091:                    logger.error("Decryption error while processing value '"
092:                            + encryptedString + "'", e);
093:                    // FIXME - should this throw the exception - caues issues upstream.
094:                    return null;
095:                }
096:            }
097:
098:            /**
099:             * Convert a byte array to a String value.
100:             *
101:             * @param bytes The byte array that is to be converted.
102:             * @return A String value created from the byte array that was passed to this method.
103:             */
104:            private static String bytes2String(byte[] bytes) {
105:                StringBuffer buffer = new StringBuffer();
106:                for (int i = 0; i < bytes.length; i++) {
107:                    buffer.append((char) bytes[i]);
108:                }
109:                return buffer.toString();
110:            }
111:
112:            /**
113:             * Create the encryption key value.
114:             *
115:             * @return An encryption key value implementing the DES encryption algorithm.
116:             */
117:            private static SecretKey createKey() throws Exception {
118:                byte[] bytes = ENCRYPTION_KEY.getBytes("UTF8");
119:                DESKeySpec spec = new DESKeySpec(bytes);
120:                SecretKeyFactory keyFactory = SecretKeyFactory
121:                        .getInstance(DES_ALGORITHM);
122:                return keyFactory.generateSecret(spec);
123:            }
124:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.