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


001:        package org.dbbrowser;
002:
003:        import java.security.InvalidAlgorithmParameterException;
004:        import java.security.InvalidKeyException;
005:        import java.security.NoSuchAlgorithmException;
006:        import java.security.spec.InvalidKeySpecException;
007:        import javax.crypto.BadPaddingException;
008:        import javax.crypto.Cipher;
009:        import javax.crypto.IllegalBlockSizeException;
010:        import javax.crypto.KeyGenerator;
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:
017:        import org.apache.axis.encoding.Base64;
018:
019:        public class TestEncryption {
020:            public static void main(String[] args) {
021:                (new TestEncryption()).runWithDES();
022:                //(new TestEncryption()).runWithPBE();
023:            }
024:
025:            public void runWithPBE() {
026:                System.out.println("Starting TestEncryption.runWithPBE...");
027:                try {
028:                    //Use a password based encryption
029:                    PBEKeySpec keySpec = new PBEKeySpec("MasterPassword"
030:                            .toCharArray());
031:
032:                    //Get a secret key using the key spec and the secret key factory
033:                    //Generate a key for DES encryption and MD5 checksum using the key spec
034:                    SecretKeyFactory keyFactory = SecretKeyFactory
035:                            .getInstance("PBEWithMD5AndDES");
036:                    SecretKey pbeSecretKey = keyFactory.generateSecret(keySpec);
037:
038:                    Cipher c = Cipher.getInstance(pbeSecretKey.getAlgorithm()); //Symmetric DES encryption
039:                    //Init using the salt and iteration count
040:                    byte[] salt = new byte[] { (byte) 8, (byte) 8, (byte) 8,
041:                            (byte) 8, (byte) 8, (byte) 8, (byte) 8, (byte) 8 };//8 byte long salt
042:                    PBEParameterSpec pbeParameterSpec = new PBEParameterSpec(
043:                            salt, 20);
044:                    c.init(Cipher.ENCRYPT_MODE, pbeSecretKey, pbeParameterSpec);
045:
046:                    byte[] cleartext = "aPassword".getBytes();
047:
048:                    // Encrypt the cleartext
049:                    byte[] ciphertext = c.doFinal(cleartext);
050:
051:                    //Print the encrypted text
052:                    System.out.println("Encrypted text: "
053:                            + new String(ciphertext));
054:
055:                    //-----------------------------------------------------------------
056:                    //Start decryption
057:                    //Convert the byte[] to string using base64 encoding
058:                    String base64EncodedString = Base64.encode(ciphertext);
059:                    System.out.println("base64EncodedString: "
060:                            + base64EncodedString);
061:
062:                    //Decode the base64 encoded string
063:                    byte[] base64DecodedByteArray = Base64
064:                            .decode(base64EncodedString);
065:                    System.out.println("Base64 decoded string: "
066:                            + new String(base64DecodedByteArray));
067:
068:                    // Initialize the same cipher for decryption
069:                    c.init(Cipher.DECRYPT_MODE, pbeSecretKey, pbeParameterSpec);
070:
071:                    // Decrypt the ciphertext
072:                    byte[] recreatedClearText = c
073:                            .doFinal(base64DecodedByteArray);
074:
075:                    System.out.println(new String(cleartext) + " = "
076:                            + new String(recreatedClearText));
077:                } catch (NoSuchAlgorithmException exc) {
078:                    System.out.println(exc.getMessage());
079:                } catch (NoSuchPaddingException exc) {
080:                    System.out.println(exc.getMessage());
081:                } catch (InvalidKeyException exc) {
082:                    System.out.println(exc.getMessage());
083:                } catch (BadPaddingException exc) {
084:                    System.out.println(exc.getMessage());
085:                } catch (IllegalBlockSizeException exc) {
086:                    System.out.println(exc.getMessage());
087:                } catch (InvalidKeySpecException exc) {
088:                    System.out.println(exc.getMessage());
089:                } catch (InvalidAlgorithmParameterException exc) {
090:                    System.out.println(exc.getMessage());
091:                }
092:            }
093:
094:            public void runWithDES() {
095:                System.out.println("Starting TestEncryption.runWithDES...");
096:                try {
097:                    KeyGenerator keygen = KeyGenerator.getInstance("DES");
098:                    SecretKey desKey = keygen.generateKey();
099:
100:                    Cipher c = Cipher.getInstance("DES"); //Symmetric encryption
101:                    c.init(Cipher.ENCRYPT_MODE, desKey);
102:
103:                    byte[] cleartext = "portal".getBytes();
104:
105:                    // Encrypt the cleartext
106:                    byte[] ciphertext = c.doFinal(cleartext);
107:
108:                    // Initialize the same cipher for decryption
109:                    c.init(Cipher.DECRYPT_MODE, desKey);
110:
111:                    // Decrypt the ciphertext
112:                    byte[] cleartext1 = c.doFinal(ciphertext);
113:
114:                    System.out
115:                            .println("Cypher text: " + new String(ciphertext));
116:                    System.out.println(new String(cleartext) + " = "
117:                            + new String(cleartext1));
118:                } catch (NoSuchAlgorithmException exc) {
119:                    System.out.println(exc.getMessage());
120:                } catch (NoSuchPaddingException exc) {
121:                    System.out.println(exc.getMessage());
122:                } catch (InvalidKeyException exc) {
123:                    System.out.println(exc.getMessage());
124:                } catch (BadPaddingException exc) {
125:                    System.out.println(exc.getMessage());
126:                } catch (IllegalBlockSizeException exc) {
127:                    System.out.println(exc.getMessage());
128:                }
129:            }
130:
131:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.