DES

This algorithm is a 64-bit block cipher with a 64-bit key. The effective key size is only 56-bit, however, as 8 bits of the key are used for parity. The algorithm described in FIPS PUB 46-2.

DES Cipher Initialization

This cipher supports both ECB and CBC modes, and may be used with NoPadding or PKCS5Padding. To create an instance of this class use the Cipher.getInstance() method with “SAFENET” as the provider and one of the following strings as the transformation:

>DES

>DES/ECB/NoPadding

>DES/ECB/PKCS5Padding

>DES/CBC/NoPadding

>DES/CBC/PKCS5Padding

Using the “DES” transformation, the Cipher will default to ECB and NoPadding.

If the NoPadding mode is selected, the input data must be a multiple of 8 bytes; otherwise, the encrypted or decrypted result will be truncated. In PKCS5Padding, arbitrary data lengths are accepted; the ciphertext will be padded to a multiple of 8 bytes, as described in PKCS#5. The decryption process will remove the padding from the data so that the correct plaintext is returned.

This Cipher will accept a javax.crypto.spec.SecretKeySpec or au.com.safenet.crypto.provider.CryptokiSecretKey as the key parameter during initialization.

When the Cipher is initialized in CBC mode, the Initialization Vector (IV) may be specified by passing a javax.crypto.spec.IvParameterSpec instance to the Cipher.init() method. When decrypting in this mode, a valid IV must be specified in the Cipher.init() method. For encryption, however, a random IV will be generated if none is specified (the IV may be retrieved using the Cipher.getIV() method).

The IV may be provided as a java.security.AlgorithmParameters or a javax.crypto.spec.IvParameterSpec instance. If the initialization is done using an AlgorithmParameters instance, it must be convertible to an IvParameterSpec using the AlgorithmParameters.getParameterSpec() method.

This Cipher does not support the Cipher.getParameters() method; this method will always return null. The only supported parameter for this class is the initialization vector, which may be determined using the Cipher.getIV() method.

DES Key

The DES Cipher requires either a SecretKeySpec or ProtectToolkit-J provider DES Key during initialization.

To create an appropriate SecretKeySpec, pass an 8 byte array and the algorithm name “DES” to the SecretKeySpec constructor. For example:

byte[] keyBytes = { 0x01, 0x23, 0x45, 0x67,
                    0x89, 0xAB, 0xCD, 0xEF };
SecretKeySpec desKey = new SecretKeySpec(keyBytes, “DES”);

Alternatively, a random ProtectToolkit-J DES key can be generated randomly using the KeyGenerator as described in Public Keys, or from a provider-independent form as described in Key Specifications. The DES key may also be stored in the ProtectToolkit-J KeyStore, as described in Key Storage .

The ProtectToolkit-J DES key will return the string “DES” as its algorithm name, “RAW” as its encoding. However, since the key is stored within the hardware, the actual key encoding may not be available.

The key value can only be extracted from a key if the associated Cryptoki key is not marked as Sensitive. The keys generated in ProtectToolkit-J will always be marked as sensitive. It is possible, however, to access any Cryptoki keys stored on the device, and it is possible that the attributes of these keys have been modified.

DES KeyGenerator

The DES KeyGenerator is used to generate random DES keys. The generated key will be a hardware key that has the Cryptoki CKA_EXTRACTABLE and CKA_SENSITIVE attributes set. Since these keys are marked as sensitive, their getEncoded() method will return null.

During initialization, the strength and random parameters are ignored, as all keys are 64-bits and the hardware includes a cryptographically-secure random source.

Keys generated using the KeyGenerator are not thread-safe. That is, a ProtectToolkit-J Key instance may only be used by a single Cipher instance (as well as a single MAC instance) at any given time. See Key Generation for information on threading and ProtectToolkit-J keys.

DES SecretKeyFactory

The DES SecretKeyFactory is used to construct ProtectToolkit-J keys from their provider-independent form. The provider independent form of the DES key is the javax.crypto.spec.DESKeySpec class.

Keys generated using the SecretKeyFactory are not thread-safe. That is, a ProtectToolkit-J Key instance may only be used by a single Cipher instance (as well as a single MAC instance) at any given time. SeeKey Generation for information on threading and ProtectToolkit-J keys.

For example, to create the provider based key from its provider-independent form:

byte[] keyBytes = { 0x01, 0x23, 0x45, 0x67,
                    0x89, 0xAB, 0xCD, 0xEF };
DESKeySpec desKeySpec = new DESKeySpec(keyBytes);
SecretKeyFactory desKeyFact =
              SecretKeyFactory.getInstance(“DES”, “SAFENET”);
SecretKey desKey = desKeyFact.generateSecret(desKeySpec);

DES Example Code

The following example code will create a random DES key, then create a DES cipher in CBC mode with PKCS5Padding. Next, it initializes the cipher for encryption using the newly-created key. We then save the initialization vector and encrypt the string "hello world".

To perform the decryption, we re-initialize the cipher in decrypt mode, with the same key and the initialization vector that was created during encryption.

KeyGenerator keyGen = KeyGenerator.getInstance("DES",
                                               "SAFENET");
Key desKey = keyGen.generateKey();
Cipher desCipher = Cipher.getInstance("DES/CBC/PKCS5Padding",
                                      "SAFENET");
desCipher.init(Cipher.ENCRYPT_MODE, desKey);
byte[] iv = desCipher.getIV();
byte[] cipherText = desCipher.doFinal(
                                   "hello world".getBytes());
desCipher.init(Cipher.DECRYPT_MODE, desKey,
                        new IvParameterSpec(iv));
byte[] plainText = desCipher.doFinal(cipherText);