| Using
SecureXML
Digital Signature from Java
See example of
using SecureXML from Java in J2EE
Security book by Pankaj Kumar.
You can get a free 90-day evaluation license of
SecureXML by using the license access number provided in the book
on page 407.
Java classes for SecureXML are included as securexml.jar file.
Java with SecureXML requires jacob.jar and jacob.dll files also
included with SecureXML. Both the jar files need to be added to
your CLASSPATH environment variable and the jacob.dll should be
in your PATH environment variable. If you are using SecureXML from
a Java applet, you will also need to provide appropriate access
permissions to the two files in your .java.policy file.
import com.jacob.com.*;
import infomosaic.securexml.*;
public class SignTest
{
public static void main(String[] args)
{
Signature s1 = new Signature(); /* Creates an instance of the SecureXML
Signature object */
String outFilePath;
/* Lets report the number of certificates installed in the local
windows certificate store for the currently logged on user
*/
System.out.println("Certificate Count="+ s1.getCertificateCount());
/* Lets show the certificate selection window */
s1.selectActiveCertificate();
/* Read the file to be signed */
String fileData = s1.readAll("signature.tmpl");
/* Sign the XML read from the file */
String outFileData = s1.signXMLStr(fileData, "MySignature");
/* Show the signed XML */
System.out.println("Signed XML= "+ outFileData);
/* Save the signed XML to a file */
s1.saveXMLStr(outFileData, "signatureSigned.xml");
/* Verify the signature we just created */
int result = s1.verify("signatureSigned.xml");
System.out.println("Signature verification result= "+
result);
String verifyResult = s1.secureXMLVerify(outFileData);
System.out.println("secureXMLVerify = "+ verifyResult);
/* Lets create another signature */
s1.setSignatureID(0, "MySignature1234");
s1.setExcludeSignerCertificate(1);
s1.setProperties(0, 0, "Date=12/19/2002");
s1.setAddWindowImage(1);
s1.setPhysicalSignatureUsage(2);
/* This time, lets just sign a simple data string */
outFileData = s1.signDataStr("This is my input");
System.out.println("Signed Data Str XML= "+ outFileData);
/* Save the signed XML to a file */
s1.saveXMLStr(outFileData, "dataSigned.xml");
/* Verify the signature we just created */
result = s1.verify("dataSigned.xml");
String propertyStr = s1.getProperties(0,0);
System.out.println("Signature Property 0 = "+ propertyStr);
/* The following code passes a list of files to be signed */
/* The output XML contains Base64 encoded file content along with
the signature */
String [] fileList = new String[3];
fileList[0] = "f1.doc";
fileList[1] = "f2.txt";
fileList[2] = "f3.doc";
SafeArray sA = new SafeArray(Variant.VariantString, new int [] {0},
new int [] {fileList.length + 1});
sA.fromStringArray(fileList);
String outFileName = s1.signFiles(new Variant(sA), "3FilesSigned.xml");
System.out.println("Out File Name = " + outFileName);
}
}
|