IAIK CMS/SMIME Toolkit Demo API Documentation
Version 6.1

demo.cms.envelopedData
Class FileEncryptionDemo

java.lang.Object
  extended by demo.cms.envelopedData.FileEncryptionDemo

public class FileEncryptionDemo
extends java.lang.Object

This class shows how to use the CMS PasswordRecipientInfo type for password based encrypting the contents of a file (and later decrypting it again) with the CMS EnvelopedDataStream EnvelopedData} content type.

The contents is encrypted using the AES cipher. For deriving the key encryption key from the password, the PKCS#5 PBKDF2 key derivation function is used. The content encryption key is decrypted with AES, too.

You can modify this demo to use any other supported algorithm(s). Or you can use it with PBKDF2/AES for encrypting a file simply by calling:

 // the file to be encrypted
 String dataFile = ...;
 // the file to which to write the encrypted data
 String encryptedFile = ...;
 // password 
 char[] password = ...;
 // encrypt file
 FileEncryption fe = new FileEncryption();
 fe.encrypt(dataFile, encryptedFile, password);
 
Or decrypting a file:
 // the encrypted file
 String encryptedFile = ...;
 // the file to which to write the decrypted data
 String decryptedFile = ...;
 // password 
 char[] password = ...;
 // decrypt file
 FileEncryption fe = new FileEncryption();
 fe.decrypt(encryptedFile, decryptedFile, password);
 

See Also:
EnvelopedDataStream, EnvelopedData, EnvelopedDataOutputStream, PasswordRecipientInfo

Constructor Summary
FileEncryptionDemo()
          Default constructor.
 
Method Summary
 void decrypt(java.lang.String inFile, java.lang.String outFile, char[] password)
          Uses the given password to decrypt the contents from inFile and write the it to outFile.
 void encrypt(java.lang.String inFile, java.lang.String outFile, char[] password)
          Uses the given password to encrypt the contents from inFile and write the encryption result to outFile.
static void main(java.lang.String[] argv)
          Main method.
 void start()
          Starts the demo.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileEncryptionDemo

public FileEncryptionDemo()
Default constructor.

Method Detail

encrypt

public void encrypt(java.lang.String inFile,
                    java.lang.String outFile,
                    char[] password)
             throws java.lang.Exception
Uses the given password to encrypt the contents from inFile and write the encryption result to outFile. The contents is encrypted using the AES cipher. For dereiving the key encryption key from the password, the PKCS#5 PBKDF2 key derivation function is used. The content encryption key is decrypted with AES, too.

Parameters:
inFile - the file from which to read the encrypted (enveloped) data
outFile - the file to which to write the decrypted data
password - the password to be used for decryption
Throws:
java.lang.Exception - if an error occurs

decrypt

public void decrypt(java.lang.String inFile,
                    java.lang.String outFile,
                    char[] password)
             throws java.lang.Exception
Uses the given password to decrypt the contents from inFile and write the it to outFile.

Parameters:
inFile - the file from which to read the encrypted (enveloped) data
outFile - the file to which to write the decrypted data
password - the password to be used for decryption
Throws:
java.lang.Exception - if an error occurs

start

public void start()
Starts the demo.


main

public static void main(java.lang.String[] argv)
                 throws java.lang.Exception
Main method.

Throws:
java.lang.Exception - if some error occurs

IAIK CMS/SMIME Toolkit Demo API Documentation
Version 6.1

v6.1
(c) 2002 IAIK, (c) 2003 - 2025 SIC