Source Code Cross Referenced for Password.java in  » Portal » Open-Portal » com » sun » portal » app » blog » password » 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 » Portal » Open Portal » com.sun.portal.app.blog.password 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package com.sun.portal.app.blog.password;
002:
003:        import com.sun.portal.app.blog.BlogPortletException;
004:        import com.sun.portal.app.blog.CommandLineOptions;
005:        import java.io.File;
006:        import java.io.FileInputStream;
007:        import java.io.FileNotFoundException;
008:        import java.io.FileOutputStream;
009:        import java.io.IOException;
010:        import java.io.InputStream;
011:        import java.io.ObjectInputStream;
012:        import java.io.ObjectOutputStream;
013:        import java.io.UnsupportedEncodingException;
014:        import java.security.InvalidKeyException;
015:        import java.security.NoSuchAlgorithmException;
016:        import javax.crypto.BadPaddingException;
017:        import javax.crypto.Cipher;
018:        import javax.crypto.IllegalBlockSizeException;
019:        import javax.crypto.KeyGenerator;
020:        import javax.crypto.NoSuchPaddingException;
021:        import javax.crypto.SecretKey;
022:        import org.apache.commons.codec.binary.Base64;
023:
024:        public class Password {
025:            public SecretKey generateKey() throws BlogPortletException {
026:                try {
027:                    KeyGenerator kg = KeyGenerator.getInstance("DES");
028:                    kg.init(56);
029:                    SecretKey sk = kg.generateKey();
030:
031:                    return sk;
032:                } catch (NoSuchAlgorithmException nsae) {
033:                    throw new BlogPortletException(nsae);
034:                }
035:            }
036:
037:            public void writeKey(File f) throws BlogPortletException {
038:                SecretKey sk = generateKey();
039:
040:                try {
041:                    FileOutputStream fos = new FileOutputStream(f);
042:                    ObjectOutputStream oos = new ObjectOutputStream(fos);
043:                    oos.writeObject(sk);
044:                    oos.close();
045:                    fos.close();
046:                } catch (IOException ioe) {
047:                    throw new BlogPortletException(ioe);
048:                }
049:            }
050:
051:            public SecretKey readKey(File f) throws BlogPortletException {
052:                if (!f.canRead()) {
053:                    throw new BlogPortletException("cannot read key file: " + f);
054:                }
055:                FileInputStream fis;
056:                try {
057:                    fis = new FileInputStream(f);
058:                } catch (FileNotFoundException fnfe) {
059:                    throw new BlogPortletException(fnfe);
060:                }
061:
062:                return readKey(fis);
063:            }
064:
065:            public SecretKey readKey(InputStream is)
066:                    throws BlogPortletException {
067:                try {
068:                    ObjectInputStream ois = new ObjectInputStream(is);
069:                    SecretKey sk = (SecretKey) ois.readObject();
070:                    ois.close();
071:
072:                    return sk;
073:                } catch (ClassNotFoundException cnfe) {
074:                    throw new BlogPortletException(cnfe);
075:                } catch (IOException ioe) {
076:                    throw new BlogPortletException(ioe);
077:                }
078:            }
079:
080:            public String decrypt(String s, SecretKey sk)
081:                    throws BlogPortletException {
082:                try {
083:                    s = new String(Base64.decodeBase64(s.getBytes()),
084:                            "ISO-8859-1");
085:                    //s = URLDecoder.decode(s, "UTF-8");
086:
087:                    Cipher dciph = Cipher.getInstance("DES");
088:                    dciph.init(Cipher.DECRYPT_MODE, sk);
089:
090:                    byte[] sb = s.getBytes("ISO-8859-1");
091:                    byte[] db = dciph.doFinal(sb);
092:                    String pw = new String(db, "ISO-8859-1");
093:
094:                    return pw;
095:                } catch (NoSuchAlgorithmException nsae) {
096:                    throw new BlogPortletException(nsae);
097:                } catch (NoSuchPaddingException nspe) {
098:                    throw new BlogPortletException(nspe);
099:                } catch (InvalidKeyException ike) {
100:                    throw new BlogPortletException(ike);
101:                } catch (UnsupportedEncodingException uee) {
102:                    throw new BlogPortletException(uee);
103:                } catch (IllegalBlockSizeException ibse) {
104:                    throw new BlogPortletException(ibse);
105:                } catch (BadPaddingException bpe) {
106:                    throw new BlogPortletException(bpe);
107:                }
108:            }
109:
110:            public String encrypt(String s, SecretKey sk)
111:                    throws BlogPortletException {
112:                try {
113:                    Cipher eciph = Cipher.getInstance("DES");
114:                    eciph.init(Cipher.ENCRYPT_MODE, sk);
115:
116:                    byte[] sb = s.getBytes("ISO-8859-1");
117:                    byte[] eb = eciph.doFinal(sb);
118:                    String epw = new String(Base64.encodeBase64(eb));
119:                    //String epw = URLEncoder.encode(new String(eb, "ISO-8859-1"), "UTF-8");
120:
121:                    return epw;
122:                } catch (NoSuchAlgorithmException nsae) {
123:                    throw new BlogPortletException(nsae);
124:                } catch (NoSuchPaddingException nspe) {
125:                    throw new BlogPortletException(nspe);
126:                } catch (InvalidKeyException ike) {
127:                    throw new BlogPortletException(ike);
128:                } catch (UnsupportedEncodingException uee) {
129:                    throw new BlogPortletException(uee);
130:                } catch (IllegalBlockSizeException ibse) {
131:                    throw new BlogPortletException(ibse);
132:                } catch (BadPaddingException bpe) {
133:                    throw new BlogPortletException(bpe);
134:                }
135:            }
136:
137:            public static void main(String[] args) {
138:                try {
139:                    CommandLineOptions clops = new CommandLineOptions(args);
140:                    System.out.println(clops);
141:                    String cmd = clops.get("c");
142:                    Password p = new Password();
143:
144:                    if (cmd.equals("genkey")) {
145:                        File keyFile = new File(clops.get("k"));
146:                        p.writeKey(keyFile);
147:                    } else if (cmd.equals("decrypt")) {
148:                        File keyFile = readKeyFile(clops);
149:                        SecretKey sk = p.readKey(keyFile);
150:                        String epw = clops.get("e");
151:                        String dpw = p.decrypt(epw, sk);
152:                        System.out.println(dpw);
153:                    } else if (cmd.equals("encrypt")) {
154:                        File keyFile = readKeyFile(clops);
155:                        SecretKey sk = p.readKey(keyFile);
156:                        String dpw = clops.get("d");
157:                        String epw = p.encrypt(dpw, sk);
158:                        System.out.println(epw);
159:                    } else if (cmd.equals("test")) {
160:                        File keyFile = readKeyFile(clops);
161:                        SecretKey sk = p.readKey(keyFile);
162:                        String epw = p.encrypt("test", sk);
163:                        String dpw = p.decrypt(epw, sk);
164:                        System.out.println(dpw);
165:                    }
166:                } catch (Throwable t) {
167:                    t.printStackTrace();
168:                }
169:            }
170:
171:            private static File readKeyFile(CommandLineOptions clops) {
172:                String f = clops.get("k");
173:                File keyFile;
174:                if (f == null) {
175:                    keyFile = new File("key.tmp");
176:                } else {
177:                    keyFile = new File(f);
178:                }
179:
180:                return keyFile;
181:            }
182:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.