Source Code Cross Referenced for ReadSignedMail.java in  » Security » Bouncy-Castle » org » bouncycastle » mail » smime » examples » 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 » Security » Bouncy Castle » org.bouncycastle.mail.smime.examples 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.bouncycastle.mail.smime.examples;
002:
003:        import java.io.FileInputStream;
004:        import java.security.cert.CertStore;
005:        import java.security.cert.X509Certificate;
006:        import java.util.Collection;
007:        import java.util.Iterator;
008:        import java.util.Properties;
009:
010:        import javax.mail.BodyPart;
011:        import javax.mail.Multipart;
012:        import javax.mail.Session;
013:        import javax.mail.internet.MimeBodyPart;
014:        import javax.mail.internet.MimeMessage;
015:        import javax.mail.internet.MimeMultipart;
016:
017:        import org.bouncycastle.cms.SignerInformation;
018:        import org.bouncycastle.cms.SignerInformationStore;
019:        import org.bouncycastle.mail.smime.SMIMESigned;
020:
021:        /**
022:         * a simple example that reads a basic SMIME signed mail file.
023:         */
024:        public class ReadSignedMail {
025:            /**
026:             * verify the signature (assuming the cert is contained in the message)
027:             */
028:            private static void verify(SMIMESigned s) throws Exception {
029:                //
030:                // extract the information to verify the signatures.
031:                //
032:
033:                //
034:                // certificates and crls passed in the signature
035:                //
036:                CertStore certs = s.getCertificatesAndCRLs("Collection", "BC");
037:
038:                //
039:                // SignerInfo blocks which contain the signatures
040:                //
041:                SignerInformationStore signers = s.getSignerInfos();
042:
043:                Collection c = signers.getSigners();
044:                Iterator it = c.iterator();
045:
046:                //
047:                // check each signer
048:                //
049:                while (it.hasNext()) {
050:                    SignerInformation signer = (SignerInformation) it.next();
051:                    Collection certCollection = certs.getCertificates(signer
052:                            .getSID());
053:
054:                    Iterator certIt = certCollection.iterator();
055:                    X509Certificate cert = (X509Certificate) certIt.next();
056:
057:                    //
058:                    // verify that the sig is correct and that it was generated
059:                    // when the certificate was current
060:                    //
061:                    if (signer.verify(cert, "BC")) {
062:                        System.out.println("signature verified");
063:                    } else {
064:                        System.out.println("signature failed!");
065:                    }
066:                }
067:            }
068:
069:            public static void main(String[] args) throws Exception {
070:                //
071:                // Get a Session object with the default properties.
072:                //         
073:                Properties props = System.getProperties();
074:
075:                Session session = Session.getDefaultInstance(props, null);
076:
077:                MimeMessage msg = new MimeMessage(session, new FileInputStream(
078:                        "signed.message"));
079:
080:                //
081:                // make sure this was a multipart/signed message - there should be
082:                // two parts as we have one part for the content that was signed and
083:                // one part for the actual signature.
084:                //
085:                if (msg.isMimeType("multipart/signed")) {
086:                    SMIMESigned s = new SMIMESigned((MimeMultipart) msg
087:                            .getContent());
088:
089:                    //
090:                    // extract the content
091:                    //
092:                    MimeBodyPart content = s.getContent();
093:
094:                    System.out.println("Content:");
095:
096:                    Object cont = content.getContent();
097:
098:                    if (cont instanceof  String) {
099:                        System.out.println((String) cont);
100:                    } else if (cont instanceof  Multipart) {
101:                        Multipart mp = (Multipart) cont;
102:                        int count = mp.getCount();
103:                        for (int i = 0; i < count; i++) {
104:                            BodyPart m = mp.getBodyPart(i);
105:                            Object part = m.getContent();
106:
107:                            System.out.println("Part " + i);
108:                            System.out.println("---------------------------");
109:
110:                            if (part instanceof  String) {
111:                                System.out.println((String) part);
112:                            } else {
113:                                System.out.println("can't print...");
114:                            }
115:                        }
116:                    }
117:
118:                    System.out.println("Status:");
119:
120:                    verify(s);
121:                } else if (msg.isMimeType("application/pkcs7-mime")
122:                        || msg.isMimeType("application/x-pkcs7-mime")) {
123:                    //
124:                    // in this case the content is wrapped in the signature block.
125:                    //
126:                    SMIMESigned s = new SMIMESigned(msg);
127:
128:                    //
129:                    // extract the content
130:                    //
131:                    MimeBodyPart content = s.getContent();
132:
133:                    System.out.println("Content:");
134:
135:                    Object cont = content.getContent();
136:
137:                    if (cont instanceof  String) {
138:                        System.out.println((String) cont);
139:                    }
140:
141:                    System.out.println("Status:");
142:
143:                    verify(s);
144:                } else {
145:                    System.err.println("Not a signed message!");
146:                }
147:            }
148:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.