Holger's Java API

## com.antelmann.util
Class CipherKey

    java.lang.Object
      com.antelmann.util.CipherKey

All Implemented Interfaces:
  SynchronousKey

    public class CipherKey

extends Object
implements SynchronousKey

CipherKey provides a very easy-to-use, yet effective encryption mechanism. The relevant data is transient, so serialization is useless on this instance.

Author:
  Holger Antelmann

Field Summary
`static String` `defaultAlgorithm`
`static int` `defaultIterationCount`

Constructor Summary
`CipherKey(char[] password)` uses PBE with MD5 and DES
`CipherKey(char[] password, String algorithm, byte[] salt, int iterationCount)` uses PBE with MD5 and DES
`CipherKey(Cipher cipher, Key key)`
`CipherKey(Cipher cipher, Key key, AlgorithmParameterSpec paramSpec)`
`CipherKey(Cipher cipher, Key key, AlgorithmParameterSpec paramSpec, byte[] salt, int iterationCount)`
`CipherKey(String pwd)`

Method Summary
`byte[]` `decode(byte[] encrypted)`
`void` `decode(InputStream in, OutputStream out)` decodes the given InputStream into the given OutputStream
`String` `decode(String s)` convenience method; requires a base64 string; encoding based on UTF8
`byte[]` `encode(byte[] plainSource)`
`void` `encode(InputStream in, OutputStream out)` encodes the given InputStream into the given OutputStream
`String` `encode(String s)` convenience method; returns a base64 string derived from UTF8 encoded bytes
`String` `get(String s)` convenience method; calls and returns the result of `decode(String)`, but doesn't throw an exception; instead it returns the given string in case of a failure
`String` `getAlgorithm()` returns the algorithm of the embedded Cipher object
`Cipher` `getDecoder()` fully initializes the Cipher for decoding and returns it.
`static byte[]` `getDefaultSalt()`
`Cipher` `getEncoder()` fully initializes the Cipher for encoding and returns it.
`String` `getKeySignature()` just returns the algorithm, which doesn't say much
`void` `setPassword(char[] password)` only supported with PBEWithMD5AndDES algorithm
`void` `setPassword(char[] password, byte[] salt, int iterationCount)` only supported with PBEWithMD5AndDES algorithm

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Field Detail

### defaultIterationCount

    public static final int defaultIterationCount

See Also:
  Constant Field Values

### defaultAlgorithm

    public static final String defaultAlgorithm

See Also:
  Constant Field Values

Constructor Detail

### CipherKey

    public CipherKey(String pwd)
              throws GeneralSecurityException

Throws:
  `GeneralSecurityException`

### CipherKey

    public CipherKey(char[] password)
              throws GeneralSecurityException

  uses PBE with MD5 and DES

  Throws:
    `GeneralSecurityException`

### CipherKey

    public CipherKey(char[] password,
                     String algorithm,
                     byte[] salt,
                     int iterationCount)
              throws GeneralSecurityException

  uses PBE with MD5 and DES

  Throws:
    `GeneralSecurityException`

### CipherKey

    public CipherKey(Cipher cipher,
                     Key key)

### CipherKey

    public CipherKey(Cipher cipher,
                     Key key,
                     AlgorithmParameterSpec paramSpec)

### CipherKey

    public CipherKey(Cipher cipher,
                     Key key,
                     AlgorithmParameterSpec paramSpec,
                     byte[] salt,
                     int iterationCount)

Method Detail

### setPassword

    public void setPassword(char[] password)
                     throws GeneralSecurityException

  only supported with PBEWithMD5AndDES algorithm

  Throws:
    `GeneralSecurityException`

### setPassword

    public void setPassword(char[] password,
                            byte[] salt,
                            int iterationCount)
                     throws GeneralSecurityException

  only supported with PBEWithMD5AndDES algorithm

  Throws:
    `GeneralSecurityException`

### getEncoder

    public Cipher getEncoder()

  fully initializes the Cipher for encoding and returns it. To be thread-save, operating on the returned Cipher should be synchronized on the returned value.

### getDecoder

    public Cipher getDecoder()

  fully initializes the Cipher for decoding and returns it. To be thread-save, operating on the returned Cipher should be synchronized on the returned value.

### encode

    public void encode(InputStream in,
                       OutputStream out)
                throws IOException

  Description copied from interface: `SynchronousKey`
  encodes the given InputStream into the given OutputStream

  Specified by:
    `encode` in interface `SynchronousKey`
  Throws:
    `IOException`

### decode

    public void decode(InputStream in,
                       OutputStream out)
                throws IOException

  Description copied from interface: `SynchronousKey`
  decodes the given InputStream into the given OutputStream

  Specified by:
    `decode` in interface `SynchronousKey`
  Throws:
    `IOException`

### decode

    public byte[] decode(byte[] encrypted)
                  throws GeneralSecurityException

  Throws:
    `GeneralSecurityException`

### encode

    public byte[] encode(byte[] plainSource)
                  throws GeneralSecurityException

  Throws:
    `GeneralSecurityException`

### encode

    public String encode(String s)
                  throws GeneralSecurityException

  convenience method; returns a base64 string derived from UTF8 encoded bytes

  Throws:
    `GeneralSecurityException`

### decode

    public String decode(String s)
                  throws GeneralSecurityException

  convenience method; requires a base64 string; encoding based on UTF8

  Throws:
    `GeneralSecurityException`

### get

    public String get(String s)

  convenience method; calls and returns the result of `decode(String)`, but doesn't throw an exception; instead it returns the given string in case of a failure

### getAlgorithm

    public String getAlgorithm()

  returns the algorithm of the embedded Cipher object

### getKeySignature

    public String getKeySignature()

  just returns the algorithm, which doesn't say much

  Specified by:
    `getKeySignature` in interface `SynchronousKey`

### getDefaultSalt

    public static byte[] getDefaultSalt()

(c) Holger Antelmann since 2001- all rights reserved (contact: info@antelmann.com)
see www.antelmann.com/developer for further details and available downloads