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


001:        package org.bouncycastle.cms;
002:
003:        import org.bouncycastle.asn1.ASN1EncodableVector;
004:        import org.bouncycastle.asn1.ASN1OctetString;
005:        import org.bouncycastle.asn1.BERConstructedOctetString;
006:        import org.bouncycastle.asn1.DERSet;
007:        import org.bouncycastle.asn1.cms.ContentInfo;
008:        import org.bouncycastle.asn1.cms.EncryptedContentInfo;
009:        import org.bouncycastle.asn1.cms.EnvelopedData;
010:        import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
011:        import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
012:
013:        import javax.crypto.Cipher;
014:        import javax.crypto.CipherOutputStream;
015:        import javax.crypto.KeyGenerator;
016:        import javax.crypto.NoSuchPaddingException;
017:        import javax.crypto.SecretKey;
018:        import java.io.ByteArrayOutputStream;
019:        import java.io.IOException;
020:        import java.security.AlgorithmParameters;
021:        import java.security.GeneralSecurityException;
022:        import java.security.InvalidAlgorithmParameterException;
023:        import java.security.InvalidKeyException;
024:        import java.security.NoSuchAlgorithmException;
025:        import java.security.NoSuchProviderException;
026:        import java.util.Iterator;
027:
028:        /**
029:         * General class for generating a CMS enveloped-data message.
030:         *
031:         * A simple example of usage.
032:         *
033:         * <pre>
034:         *      CMSEnvelopedDataGenerator  fact = new CMSEnvelopedDataGenerator();
035:         *
036:         *      fact.addKeyTransRecipient(cert);
037:         *
038:         *      CMSEnvelopedData         data = fact.generate(content, algorithm, "BC");
039:         * </pre>
040:         */
041:        public class CMSEnvelopedDataGenerator extends CMSEnvelopedGenerator {
042:            /**
043:             * base constructor
044:             */
045:            public CMSEnvelopedDataGenerator() {
046:            }
047:
048:            /**
049:             * generate an enveloped object that contains an CMS Enveloped Data
050:             * object using the given provider and the passed in key generator.
051:             */
052:            private CMSEnvelopedData generate(CMSProcessable content,
053:                    String encryptionOID, KeyGenerator keyGen, String provider)
054:                    throws NoSuchAlgorithmException, NoSuchProviderException,
055:                    CMSException {
056:                String encProviderName = keyGen.getProvider().getName();
057:                ASN1EncodableVector recipientInfos = new ASN1EncodableVector();
058:                AlgorithmIdentifier encAlgId;
059:                SecretKey encKey;
060:                ASN1OctetString encContent;
061:
062:                try {
063:                    Cipher cipher = CMSEnvelopedHelper.INSTANCE
064:                            .getSymmetricCipher(encryptionOID, encProviderName);
065:
066:                    AlgorithmParameters params;
067:
068:                    encKey = keyGen.generateKey();
069:                    params = generateParameters(encryptionOID, encKey,
070:                            encProviderName);
071:
072:                    cipher.init(Cipher.ENCRYPT_MODE, encKey, params);
073:
074:                    //
075:                    // If params are null we try and second guess on them as some providers don't provide
076:                    // algorithm parameter generation explicity but instead generate them under the hood.
077:                    //
078:                    if (params == null) {
079:                        params = cipher.getParameters();
080:                    }
081:
082:                    encAlgId = getAlgorithmIdentifier(encryptionOID, params);
083:
084:                    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
085:                    CipherOutputStream cOut = new CipherOutputStream(bOut,
086:                            cipher);
087:
088:                    content.write(cOut);
089:
090:                    cOut.close();
091:
092:                    encContent = new BERConstructedOctetString(bOut
093:                            .toByteArray());
094:                } catch (NoSuchAlgorithmException e) {
095:                    throw new CMSException("can't find algorithm.", e);
096:                } catch (InvalidKeyException e) {
097:                    throw new CMSException("key invalid in message.", e);
098:                } catch (NoSuchPaddingException e) {
099:                    throw new CMSException("required padding not supported.", e);
100:                } catch (InvalidAlgorithmParameterException e) {
101:                    throw new CMSException("algorithm parameters invalid.", e);
102:                } catch (IOException e) {
103:                    throw new CMSException(
104:                            "exception decoding algorithm parameters.", e);
105:                }
106:
107:                Iterator it = recipientInfs.iterator();
108:
109:                while (it.hasNext()) {
110:                    RecipientInf recipient = (RecipientInf) it.next();
111:
112:                    try {
113:                        recipientInfos.add(recipient.toRecipientInfo(encKey,
114:                                provider));
115:                    } catch (IOException e) {
116:                        throw new CMSException("encoding error.", e);
117:                    } catch (InvalidKeyException e) {
118:                        throw new CMSException(
119:                                "key inappropriate for algorithm.", e);
120:                    } catch (GeneralSecurityException e) {
121:                        throw new CMSException(
122:                                "error making encrypted content.", e);
123:                    }
124:                }
125:
126:                EncryptedContentInfo eci = new EncryptedContentInfo(
127:                        PKCSObjectIdentifiers.data, encAlgId, encContent);
128:
129:                ContentInfo contentInfo = new ContentInfo(
130:                        PKCSObjectIdentifiers.envelopedData, new EnvelopedData(
131:                                null, new DERSet(recipientInfos), eci, null));
132:
133:                return new CMSEnvelopedData(contentInfo);
134:            }
135:
136:            /**
137:             * generate an enveloped object that contains an CMS Enveloped Data
138:             * object using the given provider.
139:             */
140:            public CMSEnvelopedData generate(CMSProcessable content,
141:                    String encryptionOID, String provider)
142:                    throws NoSuchAlgorithmException, NoSuchProviderException,
143:                    CMSException {
144:                KeyGenerator keyGen = CMSEnvelopedHelper.INSTANCE
145:                        .createSymmetricKeyGenerator(encryptionOID, provider);
146:
147:                return generate(content, encryptionOID, keyGen, provider);
148:            }
149:
150:            /**
151:             * generate an enveloped object that contains an CMS Enveloped Data
152:             * object using the given provider.
153:             */
154:            public CMSEnvelopedData generate(CMSProcessable content,
155:                    String encryptionOID, int keySize, String provider)
156:                    throws NoSuchAlgorithmException, NoSuchProviderException,
157:                    CMSException {
158:                KeyGenerator keyGen = CMSEnvelopedHelper.INSTANCE
159:                        .createSymmetricKeyGenerator(encryptionOID, provider);
160:
161:                keyGen.init(keySize);
162:
163:                return generate(content, encryptionOID, keyGen, provider);
164:            }
165:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.