The following Java code example encrypts a PDF document
named Loan.pdf with a certificate named Encryption.cer.
The encrypted PDF document is saved as a PDF file named EncryptLoanCert.pdf.
(See Encrypting PDF Documents with Certificates.)
/**
* Ensure that you create Java proxy files that consume
* the Encryption services WSDL. You can use JAX-WS to create
* the proxy Java files.
*
* For information, see "Invoking LiveCycle using SwaRef" in Programming with LiveCycle.
*/
import java.io.*;
import java.io.InputStream;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.xml.ws.BindingProvider;
import com.adobe.idp.services.*;
public class PKIEncryptPDFSWAref {
public static void main(String[] args) {
try{
// Setting configurations to retrieve the Web service
String url = "http://hiro-xp:8080/soap/services/EncryptionService?blob=swaRef";
String username = "administrator";
String password = "password";
// Create the service Client objects needed
EncryptionServiceService encryptionService = new EncryptionServiceService();
EncryptionService encryptionClient = encryptionService.getEncryptionService();
// Retrieve the Web services from the LiveCycle Server.
((BindingProvider) encryptionClient).getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, url);
((BindingProvider) encryptionClient).getRequestContext().put(BindingProvider.USERNAME_PROPERTY, username);
((BindingProvider) encryptionClient).getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, password);
//Get the PDF document
String path1 = "C:\\Adobe\Loan.pdf";
FileDataSource ds1 = new FileDataSource(path1);
DataHandler handler1 = new DataHandler(ds1);
BLOB inDoc = new BLOB();
inDoc.setSwaRef(handler1);
//Set the List that stores PKI information
MyArrayOfCertificateEncryptionIdentity pkiIdentities = new MyArrayOfCertificateEncryptionIdentity();
//Set the Permission List
MyArrayOfXsdAnyType permList = new MyArrayOfXsdAnyType();
// permList.getItem().add("PKI_ALL_PERM");
//Create a Recipient object to store certificate information
Recipient recipient = new Recipient();
//Specify the private key that is used to encrypt the document
// FileInputStream fileInputStreamCert = new FileInputStream("C:\\Adobe\Encryption.cer");
// Document privateKey = new Document (fileInputStreamCert);
// recipient.setX509Cert(privateKey);
String path2 = "C:\\Adobe\Encryption.cer";
FileDataSource ds2 = new FileDataSource(path2);
DataHandler handler2 = new DataHandler(ds2);
BLOB inCert = new BLOB();
inCert.setSwaRef(handler2);
recipient.setX509Cert(inCert);
//Create an EncryptionIdentity object
CertificateEncryptionIdentity encryptionId = new CertificateEncryptionIdentity();
encryptionId.setPerms(permList);
encryptionId.setRecipient(recipient);
//Add the EncryptionIdentity to the list
pkiIdentities.getItem().add(encryptionId);
//Set encryption run-time options
CertificateEncryptionOptionSpec certOptionsSpec = new CertificateEncryptionOptionSpec();
certOptionsSpec.setOption(CertificateEncryptionOption.ALL);
certOptionsSpec.setCompat(CertificateEncryptionCompatibility.ACRO_7);
//Encrypt the PDF document with a certificate
BLOB encryptDoc = encryptionClient.encryptPDFUsingCertificates(inDoc,pkiIdentities, certOptionsSpec);
//Create a new file containing the returned PDF document
File f = new File("C:\\Adobe\EncryptLoanCert.pdf");
BLOB outBlob = encryptDoc;
DataHandler handler3 = outBlob.getSwaRef();
//Get an InputStream from DataHandler and
//write to a file
InputStream inputStream = handler3.getInputStream();
OutputStream out = new FileOutputStream(f);
//Iterate through the buffer
byte buf[] = new byte[1024];
int len;
while ((len = inputStream.read(buf)) > 0)
out.write(buf, 0, len);
out.close();
inputStream.close();
}catch (Exception e) {
e.printStackTrace();
}
}
}
|
|
|