org.pre4j.crypto.impl
Class PBESecretKey

java.lang.Object
  extended by org.pre4j.crypto.impl.PBESecretKey
All Implemented Interfaces:
java.io.Serializable, java.security.Key, javax.crypto.SecretKey

public class PBESecretKey
extends java.lang.Object
implements javax.crypto.SecretKey

A SecretKey for Password-Based Encryption (PBE) as defined in PKCS #5.

 // Create PBE key
 PBESecretKey key = new PBESecretKey(password, salt, count, "PBEWithMD5AndDES");

 // Create PBE Cipher
 Cipher cipher = Cipher.getInstance(key.getAlgorithm());

 // Initialize PBE Cipher with key and parameters
 cipher.init(Cipher.ENCRYPT_MODE, key, key.getParamSpec());

 // Encrypt the cleartext
 byte[] ciphertext = cipher.doFinal("This is an example".getBytes());

 // Decrypt the ciphertext
 cipher.init(Cipher.DECRYPT_MODE, key, key.getParamSpec());
 byte[] plaintext = cipher.doFinal(ciphertext);

Author:
Mark Giereth
See Also:
Serialized Form

Constructor Summary
PBESecretKey(char[] password)
          Creates a new SecretKey based on the given password.
PBESecretKey(char[] password, java.lang.String algorithm, byte[] salt, int count)
          Creates a new SecretKey based on the given password.
 
Method Summary
 boolean equals(java.lang.Object obj)
           
 java.lang.String getAlgorithm()
           
 byte[] getEncoded()
           
 java.lang.String getFormat()
           
 javax.crypto.spec.PBEParameterSpec getParamSpec()
           
 int hashCode()
           
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

PBESecretKey

public PBESecretKey(char[] password)
Creates a new SecretKey based on the given password.


PBESecretKey

public PBESecretKey(char[] password,
                    java.lang.String algorithm,
                    byte[] salt,
                    int count)
             throws java.security.spec.InvalidKeySpecException,
                    java.security.NoSuchAlgorithmException
Creates a new SecretKey based on the given password.

Throws:
java.security.spec.InvalidKeySpecException
java.security.NoSuchAlgorithmException
Method Detail

equals

public boolean equals(java.lang.Object obj)
Overrides:
equals in class java.lang.Object
See Also:
Object.equals(Object)

getAlgorithm

public java.lang.String getAlgorithm()
Specified by:
getAlgorithm in interface java.security.Key
See Also:
SecretKey.getAlgorithm()

getEncoded

public byte[] getEncoded()
Specified by:
getEncoded in interface java.security.Key
See Also:
SecretKey.getEncoded()

getFormat

public java.lang.String getFormat()
Specified by:
getFormat in interface java.security.Key
See Also:
SecretKey.getFormat()

getParamSpec

public javax.crypto.spec.PBEParameterSpec getParamSpec()

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object
See Also:
Object.hashCode()

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object
See Also:
Object.toString()