1 /*
2  * @(#) $Id: Decrypt.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 javax.crypto.SecretKey;
16
17import com.verisign.xmlenc.Decryptor;
18import com.verisign.xpath.XPath;
19
20public class Decrypt {
21    public static void main(String[] args) throws Exception {
22        // Input to signature.
23        String encfile = "enc.xml";
24        String decfile = "dec.xml";
25        System.out.println("Decrypting file: " + encfile);
26
27        // Info to get Secret Key.
28        String keyfile = "3des.key";
29
30        // Get the secret key from file.
31        FileInputStream fis = new FileInputStream(keyfile);
32        ObjectInputStream ois = new ObjectInputStream(fis);
33        SecretKey key = (SecretKey)ois.readObject();
34        System.out.println("Read key from file :" + keyfile);
35
36        // Read the XML file
37        Document doc = XmlUtility.readXML(encfile);
38
39        String[] ns = {"xenc", "http://www.w3.org/2001/04/xmlenc#"};
40        XPath encloc = new XPath("//xenc:EncryptedData", ns);
41
42        Decryptor decryptor = new Decryptor(doc, key, encloc);
43        Document decryptedDoc = decryptor.decrypt();
44
45        // Write the signed XML file
46        XmlUtility.writeXML(decryptedDoc, new FileOutputStream(decfile));
47
48        System.out.println();
49        System.out.println("Decryption SUCCESSFUL!!");
50        System.out.println("Decrypted document written to file: " + decfile);
51    }
52}