FileDocCategorySizeDatePackage
PBEKeySpec.javaAPI DocAndroid 1.5 API6662Wed May 06 22:41:02 BST 2009javax.crypto.spec

PBEKeySpec

public class PBEKeySpec extends Object implements KeySpec
The key specification for a password based encryption key.

Password based encryption is described in PKCS #5.

since
Android 1.0

Fields Summary
private char[]
password
private final byte[]
salt
private final int
iterationCount
private final int
keyLength
Constructors Summary
public PBEKeySpec(char[] password)
Creates a new PBEKeySpec with the specified password.

param
password the password.

        if (password == null) {
            this.password = new char[0];
        } else {
            this.password = new char[password.length];
            System.arraycopy(password, 0, this.password, 0, password.length);
        }
        salt = null;
        iterationCount = 0;
        keyLength = 0;
    
public PBEKeySpec(char[] password, byte[] salt, int iterationCount, int keyLength)
Creates a new PBEKeySpec with the specified password, salt, iteration count and the desired length of the derived key.

param
password the password.
param
salt the salt.
param
iterationCount the iteration count.
param
keyLength the desired key length of the derived key,
throws
NullPointerException if the salt is null.
throws
IllegalArgumentException if the salt is empty, iteration count is zero or negative or the key length is zero or negative.

        if (salt == null) {
            throw new NullPointerException(Messages.getString("crypto.3B")); //$NON-NLS-1$
        }
        if (salt.length == 0) {
            throw new IllegalArgumentException(Messages.getString("crypto.3C")); //$NON-NLS-1$
        }
        if (iterationCount <= 0) {
            throw new IllegalArgumentException(
                    Messages.getString("crypto.3D")); //$NON-NLS-1$
        }
        if (keyLength <= 0) {
            throw new IllegalArgumentException(Messages.getString("crypto.3E")); //$NON-NLS-1$
        }

        if (password == null) {
            this.password = new char[0];
        } else {
            this.password = new char[password.length];
            System.arraycopy(password, 0, this.password, 0, password.length);
        }
        this.salt = new byte[salt.length];
        System.arraycopy(salt, 0, this.salt, 0, salt.length);
        this.iterationCount = iterationCount;
        this.keyLength = keyLength;
    
public PBEKeySpec(char[] password, byte[] salt, int iterationCount)
Creates a new PBEKeySpec with the specified password, salt and iteration count.

param
password the password.
param
salt the salt.
param
iterationCount the iteration count.
throws
NullPointerException if salt is null.
throws
IllegalArgumentException if the salt is empty or iteration count is zero or negative.

        if (salt == null) {
            throw new NullPointerException(Messages.getString("crypto.3B")); //$NON-NLS-1$
        }
        if (salt.length == 0) {
            throw new IllegalArgumentException(Messages.getString("crypto.3C")); //$NON-NLS-1$
        }
        if (iterationCount <= 0) {
            throw new IllegalArgumentException(
                    Messages.getString("crypto.3D")); //$NON-NLS-1$
        }

        if (password == null) {
            this.password = new char[0];
        } else {
            this.password = new char[password.length];
            System.arraycopy(password, 0, this.password, 0, password.length);
        }
        this.salt = new byte[salt.length];
        System.arraycopy(salt, 0, this.salt, 0, salt.length);
        this.iterationCount = iterationCount;
        this.keyLength = 0;
    
Methods Summary
public final voidclearPassword()
Clears the password by overwriting it.

        Arrays.fill(password, '?");
        password = null;
    
public final intgetIterationCount()
Returns the iteration count of this key specification.

return
the iteration count of this key specification.

        return iterationCount;
    
public final intgetKeyLength()
Returns the desired key length of the derived key.

return
the desired key length of the derived key.

        return keyLength;
    
public final char[]getPassword()
Returns a copy of the password of this key specification.

return
a copy of the password of this key specification.
throws
IllegalStateException if the password has been cleared before.

        if (password == null) {
            throw new IllegalStateException(Messages.getString("crypto.3F")); //$NON-NLS-1$
        }
        char[] result = new char[password.length];
        System.arraycopy(password, 0, result, 0, password.length);
        return result;
    
public final byte[]getSalt()
Returns a copy of the salt of this key specification.

return
a copy of the salt of this key specification or null if none is specified.

        if (salt == null) {
            return null;
        }
        byte[] result = new byte[salt.length];
        System.arraycopy(salt, 0, result, 0, salt.length);
        return result;