001    /* Copyright  (c) 2002 Graz University of Technology. All rights reserved.
002     *
003     * Redistribution and use in  source and binary forms, with or without 
004     * modification, are permitted  provided that the following conditions are met:
005     *
006     * 1. Redistributions of  source code must retain the above copyright notice,
007     *    this list of conditions and the following disclaimer.
008     *
009     * 2. Redistributions in  binary form must reproduce the above copyright notice,
010     *    this list of conditions and the following disclaimer in the documentation
011     *    and/or other materials provided with the distribution.
012     *  
013     * 3. The end-user documentation included with the redistribution, if any, must
014     *    include the following acknowledgment:
015     * 
016     *    "This product includes software developed by IAIK of Graz University of
017     *     Technology."
018     * 
019     *    Alternately, this acknowledgment may appear in the software itself, if 
020     *    and wherever such third-party acknowledgments normally appear.
021     *  
022     * 4. The names "Graz University of Technology" and "IAIK of Graz University of
023     *    Technology" must not be used to endorse or promote products derived from 
024     *    this software without prior written permission.
025     *  
026     * 5. Products derived from this software may not be called 
027     *    "IAIK PKCS Wrapper", nor may "IAIK" appear in their name, without prior 
028     *    written permission of Graz University of Technology.
029     *  
030     *  THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
031     *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
032     *  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
033     *  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE LICENSOR BE
034     *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
035     *  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
036     *  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
037     *  OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
038     *  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
039     *  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
040     *  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
041     *  POSSIBILITY  OF SUCH DAMAGE.
042     */
043    
044    package demo.pkcs.pkcs11; 
045    
046    import java.io.FileInputStream;
047    import java.io.FileOutputStream;
048    import java.io.InputStream;
049    import java.io.OutputStream;
050    import java.security.Security;
051    
052    import iaik.asn1.structures.AlgorithmID;
053    import iaik.pkcs.pkcs7.EnvelopedDataStream;
054    import iaik.pkcs.pkcs7.RecipientInfo;
055    import iaik.security.provider.IAIK;
056    import iaik.x509.X509Certificate;
057    
058    
059    
060    /**
061     * This helper class encrypts the given data using TrippleDES and encrypts the
062     * symmetric key using the public key in the given certificate.
063     *
064     * @author <a href="mailto:Karl.Scheibelhofer@iaik.at"> Karl Scheibelhofer </a>
065     * @version 0.1
066     * @invariants
067     */
068    public class EncryptPKCS7EnvelopedData {
069    
070      public static void main(String[] args) {
071        if (args.length != 3) {
072          printUsage();
073          System.exit(1);
074        }
075    
076        try {
077          Security.addProvider(new IAIK());
078    
079          System.out.println("Encrypting data from file: " + args[0]);
080          InputStream dataInputStream = new FileInputStream(args[0]);
081    
082          EnvelopedDataStream envelopedData = new EnvelopedDataStream(dataInputStream, AlgorithmID.des_EDE3_CBC);
083    
084          System.out.println("using recipient certificate from: " + args[1]);
085          InputStream certificateInputStream = new FileInputStream(args[1]);
086    
087          X509Certificate recipientCertificate = new X509Certificate(certificateInputStream);
088          System.out.println("which is: ");
089          System.out.println(recipientCertificate.toString(true));
090    
091    
092          RecipientInfo recipient = new RecipientInfo(recipientCertificate, AlgorithmID.rsaEncryption);
093    
094          envelopedData.setRecipientInfos(new RecipientInfo[] { recipient } );
095    
096          System.out.println("writing enveloped data to: " + args[2]);
097          OutputStream envelopedDataOutputStream = new FileOutputStream(args[2]);
098          envelopedData.writeTo(envelopedDataOutputStream);
099    
100        } catch (Throwable thr) {
101          thr.printStackTrace();
102        }
103      }
104    
105      public static void printUsage() {
106        System.out.println("Usage: EncryptPKCS7EnvelopedData <data to encrypt file> <recipient certificate> <PKCS#7 enveloped data file>");
107        System.out.println(" e.g.: EncryptPKCS7EnvelopedData contentData.dat recipientCertificte.der envelopedData.p7");
108      }
109    
110    
111    }