|
PERMISION NOTICE AND DISCLAIMER This website contains certain downloadable software. This software is copyrighted and the copyrighter claims all exclusive rights to such software. The copyright owner of the software that you download through this site may be indicated in the accompanying read-me file and in the accompanying source code as well as in the area of this Web Site from which the software is downloaded. Permission to use, copy, modify and distribute this software and its source code for non commercial purposes and without fee is hereby granted, provided that the name of the copyright owner or related contributors not be used in advertising or publicity pertaining to distribution of the software without specific, written prior permission. The copyright owner and contributors makes no representations about the suitability of this software for any purpose. It is provided "as is" without express or implied warranty. The copyright owner and its contributors disclaim all warranties with regard to this software, including all implied warranties of merchantability and fitness. In no event shall the author and the contributors be liable for any special, indirect or consequential damages or any damages whatsoever resulting from loss of use, data or profits, whether in an action of contract, negligence or other tortuous action, arising out of or in connection with the use or performance of this software. AcceptCancel
The objective of this example is to encrypt an XML document using the TrustedX encryption service. In order to understand this example, it is recommended that first you understand the previously seen example: X.509 Certificate Validation.
To try this example, you must download the file called sampleAxis9.zip and follow the instructions in Configuration of the Environment.
The method used for encrypting data guarantees confidentiality between the issuer and the recipient. This method uses the key pair supplied by the PKI infrastructure, i.e. the private key and the public key.
First, we must initialize the connection with the Web service. In this case, the Web service is DigitalEncryption, which enables us to both encrypt and decrypt data.
TWSDELocator locatorDe = new TWSDELocator();
locatorDe.setDigitalEncryptionEndpointAddress(host);
DigitalEncryptionType det = locatorDe.getDigitalEncryption();
DEBindingStub bindingDe = (DEBindingStub) det;
UtilTrustedX.ssl_conf();
The data encryption request is performed using an EncryptRequest object, in which, it is indicated that the profile for this request is xmlenc.
EncryptRequest er = new EncryptRequest();
er.setProfile(new URI(PROF_XML_ENCRYPT));
UtilTrustedX.addUsernameToken(bindingDe, user, password);
The data to be encrypted must be included in the request. In this example, the Base64 encoded data are inserted in a Document object inside an InputDocuments object.
InputDocuments idoc = new InputDocuments();
Document doc = new Document();
doc.setBase64XML(Util.readBinaryFile(path_in + filename));
idoc.setDocument(doc);
er.setInputDocuments(idoc);
Optionally, the request can contain information about the recipients for whom the message must be encrypted. To do this, an OptionalInputs object, which contains the optional information of the request, is created. A recipient is inserted in this request. To create the recipient, we use a RecipientType object and we select the recipient’s identity using its distinguished name.
OptionalInputs opt = new OptionalInputs();
RecipientType rt = new RecipientType();
KeySelector ks = new KeySelector();
NameIdentifierType nit = new NameIdentifierType(distinguishedName);
nit.setFormat(new URI(URI_DN));
ks.setName(nit);
rt.setKeySelector(ks);
Once the recipient has been selected, it is inserted in a Recipients object, which will be included in the request using the setRecipients() method. The format, in which the data will be returned, in this case Base64, is also indicated.
Recipients r = new Recipients();
r.setRecipient(new RecipientType[]{rt});
opt.setRecipients(r);
opt.setReturnBase64XML("");
er.setOptionalInputs(opt);
Once all the data have been entered in the request, the encrypt() operation is invoked; this operation sends the signature request to the host and collects the response in a EncryptResponse object.
EncryptResponse ers = bindingDe.encrypt(er);
Once the request has been sent, we can check if it has been processed correctly by consulting the response parameters. The functions of the UtilTrustedX auxiliary class are used for this. If all goes well, the encrypted data are retrieved and are stored in a file. In the XML Decryption example, we will see how its content is decrypted. The getBase64XMLEnvelope() method is used to obtain the encrypted data from the response. The EnvelopedObject object contains these data.
if(UtilTrustedXAxis.check(ers.getResult(), bindingDe)){
byte[] data = ers.getEnvelopedObject().getBase64XMLEnvelope().get_value();
String destFilename = path_out + filename.substring(0, filename.lastIndexOf("."))
+ "Encrypted.xml";
Util.writeBinaryFile(destFilename, data);
}
After executing the example, we obtain a response, such as the one below, and a file with the encrypted content.
File saved successfully on: data/output/DemoEncrypted.xml
|