1 /*
2  * @(#) $Id: Encrypt.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.Encryptor;
18import com.verisign.xmlenc.AlgorithmType;
19import com.verisign.xpath.XPath;
20
21public class Encrypt {
22    public static void main(String[] args) throws Exception {
23        // Input to signature.
24        String datafile = "book.xml";
25        String encfile = "enc.xml";
26        XPath encloc = new XPath("id('book_info')");
27        System.out.println("XML file: " + datafile + ", elem. : bookinfo");
28
29        // Info to get Secret Key.
30        String keyfile = "3des.key";
31
32
33        // Get the secret key from file.
34        FileInputStream fis = new FileInputStream(keyfile);
35        ObjectInputStream ois = new ObjectInputStream(fis);
36        SecretKey key = (SecretKey)ois.readObject();
37        System.out.println("Read key from file :" + keyfile);
38
39        // Read the XML file
40        Document doc = XmlUtility.readXML(datafile);
41
42        Encryptor encryptor = new Encryptor(doc, key, AlgorithmType.TRIPLEDES);
43
44        //encryptor.setContentEncryption(true);
45        Document encryptedDoc = encryptor.encrypt(encloc);
46
47        // Write the signed XML file
48        XmlUtility.writeXML(encryptedDoc, new FileOutputStream(encfile));
49
50        System.out.println();
51        System.out.println("Encryption SUCCESSFUL!!");
52        System.out.println("Encrypted doc. written to file: " + encfile);
53    }
54}