1
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.PublicKey;
17import javax.crypto.SecretKey;
18
19import com.verisign.xmlenc.Encryptor;
20import com.verisign.xmlenc.AlgorithmType;
21import com.verisign.xpath.XPath;
22
23public class Encrypt1 {
24 public static void main(String[] args) throws Exception {
25 String datafile = "book.xml";
27 String encfile = "enc.xml";
28 XPath encloc = new XPath("id('book_info')");
29 System.out.println("Encrypting element with id=book_info of file \"" + datafile + "\"");
30
31 String kpfile = "rsa.kp";
33 String keyfile = "3des.key";
34 System.out.println("Using RSA key-pair in file \"" + kpfile + "\" ...");
35
36 FileInputStream fis = new FileInputStream(kpfile);
38 ObjectInputStream ois = new ObjectInputStream(fis);
39 KeyPair kp = (KeyPair)ois.readObject();
40 PublicKey pk = kp.getPublic();
41 fis = new FileInputStream(keyfile);
42 ois = new ObjectInputStream(fis);
43 SecretKey key = (SecretKey)ois.readObject();
44
45 Document doc = XmlUtility.readXML(datafile);
47
48 Encryptor encryptor = new Encryptor(doc,
49 key, AlgorithmType.TRIPLEDES, pk, AlgorithmType.RSA1_5);
50 Document encryptedDoc = encryptor.encrypt(encloc);
51
52 XmlUtility.writeXML(encryptedDoc, new FileOutputStream(encfile));
54
55 System.out.println();
56 System.out.println("Encryption SUCCESSFUL!!");
57 System.out.println("Document with encrypted element written to file: " + encfile);
58 }
59}