1 /*
2  * @(#) $Id: Decrypt1.java,v 1.3 2003/07/08 08:13:52 pankaj Exp $
3  *
4  * Copyright (c) 2002-03 by Pankaj Kumar (http://www.pankaj-k.net). 
5  * All rights reserved.
6  *
7  * The license governing the use of this file can be found in the 
8  * root directory of the containing software.
9  */
10
11import java.io.FileInputStream;
12import java.io.ObjectInputStream;
13import java.io.FileOutputStream;
14import org.w3c.dom.Document;
15import java.security.KeyPair;
16import java.security.PrivateKey;
17
18import com.verisign.xmlenc.Decryptor;
19import com.verisign.xpath.XPath;
20
21public class Decrypt1 {
22    public static void main(String[] args) throws Exception {
23        // Input to signature.
24        String encfile = "enc.xml";
25        String decfile = "dec.xml";
26        System.out.println("Decrypting file \"" + decfile + "\"");
27
28        // Info to get Secret Key.
29        String kpfile = "rsa.kp";
30        System.out.println("Using secret key in file \"" + kpfile + "\" ...");
31
32        // Get the secret key from file.
33        FileInputStream fis = new FileInputStream(kpfile);
34        ObjectInputStream ois = new ObjectInputStream(fis);
35        KeyPair kp = (KeyPair)ois.readObject();
36        PrivateKey pk = kp.getPrivate();
37
38        // Read the XML file
39        Document doc = XmlUtility.readXML(encfile);
40
41        String[] ns = {"xenc", "http://www.w3.org/2001/04/xmlenc#"};
42        XPath encloc = new XPath("//xenc:EncryptedData", ns);
43
44        Decryptor decryptor = new Decryptor(doc, pk, encloc);
45        Document decryptedDoc = decryptor.decrypt();
46
47        // Write the signed XML file
48        XmlUtility.writeXML(decryptedDoc, new FileOutputStream(decfile));
49
50        System.out.println();
51        System.out.println("Decryption SUCCESSFUL!!");
52        System.out.println("Decrypted document written to file: " + decfile);
53    }
54}