7.7 Unsealing a File

The following code demonstrates how to unseal a sealed file using the unseal method. The content to unseal can be provided as any type of java.io.InputStream; this example uses a file input stream. The sample code writes the resulting stream out to a file.

Example 7-6

 import static oracle.irm.engine.content.sealing.SealingOperationsInstance.unseal;
 
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.net.Authenticator;
 import java.net.PasswordAuthentication;
 
 public class UnsealFile {
 
     public static void main(String[] args) throws Exception {
 
         // The user name and password are provided on the command line. In a production
         // system these details should be provided in a more secure manner, such
         // as prompting from the console, or reading from a secure source.
         final String username = args[0];
         final String password = args[1];
         
         // Configure an authenticator to provide the credentials for any network access
         Authenticator.setDefault(new Authenticator() {
             @Override
             protected PasswordAuthentication getPasswordAuthentication() {
                 return new PasswordAuthentication(username, password.toCharArray());
             }
         });
         
         // The file to unseal
         String sealedFilename = args[2];
 
         // The unsealed file name
         String unsealedFilename = args[3];
 
         // Sealed file input stream
         FileInputStream inputStream = new FileInputStream(sealedFilename);
 
         // Unsealed file output stream        
         FileOutputStream outputStream = new FileOutputStream(unsealedFilename);
         
         // Unseal the sealed file
         unseal(inputStream, outputStream);
 
         // Close the file streams
         inputStream.close();
         outputStream.close();
     }
 }