/* * (C) Copyright IBM Corp. 1999, 2000. All rights reserved. * * US Government Users Restricted Rights Use, duplication or * disclosure restricted by GSA ADP Schedule Contract with IBM Corp. * * The program is provided "as is" without any warranty express or * implied, including the warranty of non-infringement and the implied * warranties of merchantibility and fitness for a particular purpose. * IBM will not be liable for any damages suffered by you as a result * of using the Program. In no event will IBM be liable for any * special, indirect or consequential damages or lost profits even if * IBM has been advised of the possibility of their occurrence. IBM * will not be liable for any third party claims against you. */ import com.ibm.dom.security.*; import com.ibm.dom.util.ToXMLVisitor; import com.ibm.xml.parsers.DOMParser; import com.ibm.xml.security.util.*; import au.net.aba.crypto.*; import au.net.aba.crypto.provider.*; import au.net.aba.crypto.spec.*; import java.io.*; import java.security.*; import javax.crypto.*; import javax.crypto.spec.*; import org.w3c.dom.*; import org.xml.sax.InputSource; import org.xml.sax.Parser; import org.xml.sax.SAXException; import org.xml.sax.helpers.ParserFactory; public class CipherTest { public static void main(String[] args) { if (args.length < 3) { System.err.println("Usage: CipherTest -e|-d passphrase infile outfile"); return; } java.security.Security.addProvider(new au.net.aba.crypto.provider.ABAProvider()); /* * Generate symetric key from passphrase */ Key key = null; try { SecretKeyFactory skf = null; skf = SecretKeyFactory.getInstance("DES"); MessageDigest sha = MessageDigest.getInstance("SHA-1"); byte[] hash = sha.digest(args[1].getBytes("UTF8")); DESKeySpec dks = new DESKeySpec(hash); key = skf.generateSecret(dks); } catch (Exception e) { e.printStackTrace(); return; } try { NodeEncryptor ne = null; NodeDecryptor nd = null; if (args[0].indexOf("e") != -1) { ne = new NodeEncryptor(); } else if (args[0].indexOf("d") != -1) { nd = new NodeDecryptor(); nd.setParserHandler(new DomReader()); } /* * Read a xml file into a DOM tree */ FileInputStream in = new FileInputStream(args[2]); DomReader dr = new DomReader(); Document doc = dr.getDocument(in); in.close(); Node root = doc.getDocumentElement(); NodeList nl = root.getChildNodes(); if (nl == null) { System.err.println("root has no child"); return; } /* * Search cardinfo or EncryptedElement element * under root element */ Node child = null; int len = nl.getLength(); for (int i = 0; i < len; i++) { Node n = nl.item(i); if (n.getNodeType() == Node.ELEMENT_NODE) { //System.out.println(((Element)n).getTagName()); if ((ne != null && ((Element)n).getTagName().equals("credit_payment")) || (nd != null && ((Element)n).getTagName().equals("EncryptedElement"))) { child = n; break; } } } if (child == null) { System.err.println("root has no element child"); return; } if (ne != null) /* Encrypt contents of cardinfo element */ ne.encrypt(child, key, "DES/CBC/PKCS5Padding"); else if (nd != null) /* Decrypt contents of EncryptedElement element*/ nd.decrypt(child, key, "DES/CBC/PKCS5Padding"); if (args.length > 3) { FileOutputStream out = new FileOutputStream(args[3]); OutputStreamWriter writer = new OutputStreamWriter(out); new ToXMLVisitor(writer).traverse(doc); writer.close(); } } catch (Exception e) { if (e instanceof XSSException) System.err.println(e.getMessage()); else e.printStackTrace(); } } static class DomReader implements com.ibm.xml.security.util.ParserHandler { private static String parserClass = "com.ibm.xml.parsers.DOMParser"; private Parser parser = null; DomReader() { try { parser = ParserFactory.makeParser(parserClass); } catch (ClassNotFoundException e) {} catch (IllegalAccessException e) {} catch (InstantiationException e) {} } public Document getDocument(InputStream in) throws Exception { if (in == null || parser == null) throw new Exception("invalid argument"); try { parser.parse(new InputSource(in)); } catch (SAXException e) { throw e; } catch (IOException e) { throw e; } return ((DOMParser) parser).getDocument(); } } }