crypter

Description

This task can be used to generate symmetric keys as well as perform encryption and decryption of files using these keys and appropriate cipher transformations.

This task assumes that you have a JDK that contains the Java encryption libaries formerly known as JCE or that you have these libraries added to an earlier JDK release. For more information refer to Java Cryptography Extension (JCE). Currently all calls use default values for the cryptographic provider used when generating keys and so on. If you need to use a cryptographic provider other than the default provider as set up by a standard JDK installation see Installing JCE Providers for the Java 2 SDK, v 1.4 for more information.

Parameters

Attribute Description Required
encrypt Whether to perform encryption or decryption. Setting this to "true" will perform encryption, setting it to false will perform decryption. No; default is "false", ignored if inputFile and outputFile are not set.
keyFile The file containing the key to be used for encryption or decryption (i.e. if inputFile and outputFile have been set). If generateKey has been set to "true" then the key will be generated to this file. Yes.
generateKey Whether to generate a key. Setting this to "true" will generate a key (this is done as the first action of this task, so this key can be used to perform encryption or decryption in the same call). Setting this to false will not generate a new key, but will use the key specified by keyFile. No; default is "false".
keyAlgorithm What algorithm to use to generate a key. No; default is "Blowfish", ignored if generateKey set to "false".
inputFile The input file for a cryptographic transformation. If encrypt is set to "true" this should be the unencrypted source file that you want to encrypt. If encrypt is set to "false" this should be the encrypted source file that you want to decrypt. Only if outputFile is also set.
outputFile The output file of a cryptographic transformation. If encrypt is set to "true" this will be the result of encrypting the file specified by inputFile. If encrypt is set to "false" this will be the result of decrypting the file specified by inputFile. Only if inputFile is also set.
cipherTransformationtd> The cipher transformation to use during encyrption or decryption operation. This must be set if you are using a key generated by anything other than the default "Blowfish" key generation algorithm. No; default is "Blowfish/ECB/PKCS5Padding", ignored if inputFile and outputFile set to "false".

Examples

All of these examples assume you have defined the task by including the following taskdef in your ant build file:
<taskdef name="crypter" classname="za.co.massdosage.ant.Crypter"/>

This must also be run prior to calling the example task fragments shown below.

<crypter keyFile="key.ser" generateKey="true" keyAlgorithm="Blowfish"/>

Generates a key and serialises this to a file called "key.ser" using the default key generation algorithm (Blowfish).

<crypter keyFile="des-key.ser" generateKey="true" keyAlgorithm="DES"/>

Generates a key and serialises this to a file called "des-key.ser" using the DES key generation algorithm.

<crypter encrypt="true" keyFile="key.ser" inputFile="source.txt" outputFile="encrypted.txt" />

Encrypts the contents of the "source.txt" file using an existing key file, "key.ser" and places the results of this encryption in the file "encrypted.txt". This assumes that the key file was generated by the Blowfish key generation algorithm since no value has been specified for "cipherTransformation" (i.e. "Blowfish/ECB/PKCS5Padding" is assumed).

<crypter encrypt="false"  keyFile="key.ser" inputFile="encrypted.txt" outputFile="decrypted.txt" />

Decrypts the contents of the "encrypted.txt" file using an existing key file, "key.ser" and places the results of this decryption in the file "decrypted.txt". This assumes that the key file was generated by the Blowfish key generation algorithm since no value has been specified for "cipherTransformation" (i.e. "Blowfish/ECB/PKCS5Padding" is assumed).

<crypter encrypt="true" generateKey="true" keyAlgorithm="DES" keyFile="des-key.ser" 
	        inputFile=""source.txt" outputFile="encrypted.txt" cipherTransformation="DES/ECB/PKCS5Padding" />

Generates a key and serialises this to a file called "des-key.ser" using the DES key generation algorithm. Then uses the DES algorithm to encrypt the contents of the "source.txt" file using this key file and places the results of the encryption in the file "encrypted.txt".

<crypter encrypt="false" keyFile="des-key.ser" inputFile="encrypted.txt" outputFile="decrypted.txt" 
    cipherTransformation="DES/ECB/PKCS5Padding"/>

Decrypts the contents of the "encrypted.txt" file using an existing DES key file, "des-key.ser" and the DES algorithm and places the results of this decryption in the file "decrypted.txt".