public class FileEncryptionDemo extends java.lang.Object
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);
EnvelopedDataStream
,
EnvelopedData
,
EnvelopedDataOutputStream
,
PasswordRecipientInfo
Constructor and Description |
---|
FileEncryptionDemo()
Default constructor.
|
Modifier and Type | Method and Description |
---|---|
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.
|
public FileEncryptionDemo()
public void encrypt(java.lang.String inFile, java.lang.String outFile, char[] password) throws java.lang.Exception
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.inFile
- the file from which to read the encrypted (enveloped) dataoutFile
- the file to which to write the decrypted datapassword
- the password to be used for decryptionjava.lang.Exception
- if an error occurspublic void decrypt(java.lang.String inFile, java.lang.String outFile, char[] password) throws java.lang.Exception
inFile
and write the it to outFile
.inFile
- the file from which to read the encrypted (enveloped) dataoutFile
- the file to which to write the decrypted datapassword
- the password to be used for decryptionjava.lang.Exception
- if an error occurspublic void start()
public static void main(java.lang.String[] argv) throws java.lang.Exception
java.lang.Exception
- if some error occurs