📄 pbe.java
字号:
package au.net.aba.crypto.provider;
/*
* $Id: PBE.java,v 1.3 1999/01/22 06:00:46 leachbj Exp $
* $Author: leachbj $
*
* Copyright (C) 1996-1998 Australian Business Access Pty Ltd.
* All rights reserved.
*
* Use, modification, copying and distribution of this software is subject the
* terms and conditions of the ABA Public Licence. See the file
* "PUBLIC_LICENCE" for additional information.
*
* If you have not received a copy of the Public Licence, you must destroy all
* copies of this file immediately.
*
* $Source: /aba/CVSROOT/jdk1.1/src/au.net.aba/crypto/provider/PBE.java,v $
* $Revision: 1.3 $
* $Date: 1999/01/22 06:00:46 $
* $State: Exp $
*/
import java.security.*;
import java.security.*;
import java.security.spec.*;
import javax.crypto.*;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
/**
* This class implements a basic password based encryption (PBE) algorithm.
* The particular implementation of PBE will specify the cipher and
* digest algorithms to use as well as the specific mechanism used to
* convert the supplied password into a secret key.
*/
public abstract class PBE extends CipherSpi
{
public final static String ident = "$Id: PBE.java,v 1.3 1999/01/22 06:00:46 leachbj Exp $";
protected Cipher cipher;
protected PBE(String cipherName)
{
try
{
cipher = Cipher.getInstance(cipherName, "ABA");
}
catch (Exception e)
{
throw new ExceptionInInitializerError(e);
}
}
/**
* Encrypts or decrypts data in a single-part operation, or
* finishes a multiple-part operation. The data is encrypted or
* decrypted, depending on how this cipher was initialised.
* <p>
* The first inputLen bytes in the input buffer, starting at
* inputOffset, and any input bytes that may have been buffered during
* a previous update operation, are processed, with padding (if
* requested) being applied. The result is stored in a new buffer.
* <p>
* The cipher is reset to its initial state (uninitialised) after
* this call.
*
* @param input the input buffer
* @param inputOffset the offset in input where the input starts
* @param inputLen the input length
*
* @exception IllegalBlockSizeException if this cipher is a block
* cipher, no padding has been requested (only in encryption
* mode), and the total input length of the data processed by
* this cipher is not a multiple of block size
* @exception BadPaddingException if this cipher is in decryption mode,
* and (un)padding has been requested, but the decrypted data is
* not bounded by the appropriate padding bytes.
*
* @return the new buffer with the result
*/
protected byte[] engineDoFinal(
byte input[],
int inputOffset,
int inputLen)
throws IllegalBlockSizeException, BadPaddingException
{
return cipher.doFinal(input, inputOffset, inputLen);
}
/**
* Encrypts or decrypts data in a single-part operation, or finishes
* a multiple-part operation. The data is encrypted or decrypted,
* depending on how this cipher was initialised.
* <p>
* The first inputLen bytes in the input buffer, starting at
* inputOffset, and any input bytes that may have been buffered during
* a previous update operation, are processed, with padding (if
* requested) being applied. The result is stored in the output buffer,
* starting at outputOffset.
* <p>
* If the output buffer is too small to hold the result, a
* ShortBufferException is thrown. In this case, repeat this call
* with a larger output buffer. Use getOutputSize to determine how
* big the output buffer should be.
*
* @param input the input buffer
* @param inputOffset - the offset in input where the input starts
* @param inputLen - the input length
* @param output - the buffer for the result
* @param outputOffset - the offset in output where the result is stored
*
* @exception IllegalBlockSizeException if this cipher is a block
* cipher, no padding has been requested (only in encryption mode),
* and the total input length of the data processed by this cipher
* is not a multiple of block size
* @exception ShortBufferException if the given output buffer is too
* small to hold the result
* @exception BadPaddingException if this cipher is in decryption mode,
* and (un)padding has been requested, but the decrypted data is
* not bounded by the appropriate padding bytes
* @returns the number of bytes stored in output
*/
protected int engineDoFinal(
byte[] input,
int inputOff,
int inputLen,
byte[] output,
int outputOff)
throws ShortBufferException, IllegalBlockSizeException,
BadPaddingException
{
return cipher.doFinal(input, inputOff, inputLen,
output, outputOff);
}
/**
* Returns the block size (in bytes). This method will return
* the block size of the underlying Cipher.
*
* @return the block size (in bytes), or 0 if the underlying
* algorithm is not a block cipher
*/
protected int engineGetBlockSize()
{
return cipher.getBlockSize();
}
/**
* Returns the initialisation vector (IV) in a new buffer.
* <p>
* This is useful in the context of password-based encryption or
* decryption, where the IV is derived from a user-provided passphrase.
*
* @return the initialisation vector in a new buffer, or null if the
* underlying algorithm does not use an IV, or if the IV has
* not yet been set.
*/
protected byte[] engineGetIV()
{
return cipher.getIV();
}
/**
* Returns the length in bytes that an output buffer would need to be
* in order to hold the result of the next update or doFinal operation,
* given the input length inputLen (in bytes).
* <p>
* This method will return a result based on the underlying cipher.
*
* @param inputLen the input length (in bytes)
* @return the required output buffer size (in bytes)
*/
protected int engineGetOutputSize(int inputLen)
{
return cipher.getOutputSize(inputLen);
}
/**
* Returns the parameters used with this cipher.
* <p>
* Currently, the PBE ciphers do not support this method.
*
* @return the parameters used with this cipher, or null if this cipher
* does not use any parameters.
*/
protected AlgorithmParameters engineGetParameters()
{
return null;
}
/**
* Initialises this cipher with a key, a set of algorithm parameters,
* and a source of randomness.
* <p>
* Attempts to convert the supplied algorithm parameters into a
* PBEParameterSpec and then initialise the cipher as above.
* <p>
* @param opmode the operation mode of this cipher (this is either
* ENCRYPT_MODE or DECRYPT_MODE)
* @param key the encryption key
* @param params the algorithm parameters
* @param random the source of randomness
* @exception InvalidKeyException if the given key is
* not a PBEKey
* @exception InvalidAlgorithmParameterException if the given algorithm
* parameters cannot be converted into a PBEParameterSpec
*/
protected void engineInit(
int opmode,
Key key,
AlgorithmParameters params,
SecureRandom random)
throws InvalidKeyException, InvalidAlgorithmParameterException
{
PBEParameterSpec spec;
if (params != null)
{
try
{
spec = (PBEParameterSpec)
params.getParameterSpec(
Class.forName("javax.crypto.spec.PBEParameterSpec"));
}
catch (Exception e)
{
throw new InvalidAlgorithmParameterException(
"Processing error: " + e);
}
}
else
{
spec = null;
}
engineInit(opmode, key, spec, random);
}
/**
* Initialises this cipher with a key and a source of randomness.
* <p>
* This method of initialisation is not supported for PBE ciphers
* as they require parameterisation that cannot be generated
* by the cipher.
*/
protected void engineInit(int opmode,
Key key,
SecureRandom random)
throws InvalidKeyException
{
throw new InvalidKeyException(
"Use the init implementation with a parameter spec.");
}
/**
* Initialises this cipher with a key, a set of algorithm parameters,
* and a source of randomness.
* <p>
* The key must be a PBEKey and the algorithm parameter spec must be
* a PBEParameterSpec.
* <p>
* The cipher is initialised for encryption or decryption, depending
* on the value of opmode.
*
* @param opmode the operation mode of this cipher (this is either
* ENCRYPT_MODE or DECRYPT_MODE)
* @param key the encryption key
* @param params the algorithm parameters
* @param random the source of randomness
*
* @exception InvalidKeyException if the given key is not a PBEKey
* @exception InvalidAlgorithmParameterException if the given algorithm
* parameter instance is not a PBEParameterSpec.
*/
protected void engineInit(
int opmode,
Key key,
AlgorithmParameterSpec params,
SecureRandom random)
throws InvalidKeyException, InvalidAlgorithmParameterException
{
if (!(params instanceof PBEParameterSpec))
{
throw new InvalidAlgorithmParameterException(
"Expecting a PBEParameterSpec");
}
if (!(key instanceof PBEKey))
{
throw new InvalidKeyException("Expecting a PBEKey");
}
// Public Parameters
byte[] salt = ((PBEParameterSpec)params).getSalt();
int iteration = ((PBEParameterSpec)params).getIterationCount();
// Private Parameters
char[] password = ((PBEKey)key).getPassword();
initCipher(opmode, salt, iteration, password);
}
/**
* Sets the mode of this cipher. Generally PBE ciphers only
* operate in a single mode and so this method will throw
* an exception.
*
* @param mode the cipher mode
* @exception NoSuchAlgorithmException if the requested cipher mode
* does not exist
*/
protected void engineSetMode(String mode)
throws NoSuchAlgorithmException
{
throw new NoSuchAlgorithmException("Can not set the mode");
}
/**
* Sets the padding mechanism of this cipher. Generally PBE
* ciphers only operate in a single mode and so this method will
* throw an exception.
*
* @param padding the padding mechanism
* @exception NoSuchPaddingException if the requested padding
* mechanism does not exist.
*/
protected void engineSetPadding(String padding)
throws NoSuchPaddingException
{
throw new NoSuchPaddingException(
"PBE only supports PKCS5 padding.");
}
/**
* Continues a multiple-part encryption or decryption operation
* (depending on how this cipher was initialised), processing another
* data part.
* <p>
* The first inputLen bytes in the input buffer, starting at
* inputOffset, are processed, and the result is stored in a new buffer.
*
* @param input the input buffer
* @param inputOffset the offset in input where the input starts
* @param inputLen the input length
* @return the new buffer with the result, or null if the underlying
* cipher is a block cipher and the input data is too short to
* result in a new block.
*/
protected byte[] engineUpdate(
byte[] input,
int inputOff,
int inputLen)
{
return cipher.update(input, inputOff, inputLen);
}
/**
* Continues a multiple-part encryption or decryption operation
* (depending on how this cipher was initialised), processing another
* data part.
* <p>
* The first inputLen bytes in the input buffer, starting at
* inputOffset, are processed, and the result is stored in the output
* buffer, starting at outputOffset.
* <p>
* If the output buffer is too small to hold the result, a
* ShortBufferException is thrown. In this case, repeat this call with
* a larger output buffer. Use getOutputSize to determine how big the
* output buffer should be.
*
* @param input the input buffer
* @param inputOffset the offset in input where the input starts
* @param inputLen the input length
* @param output the buffer for the result
* @param outputOffset the offset in output where the result is stored
*
* @exception ShortBufferException if the given output buffer is too
* small to hold the result
*
* @return the number of bytes stored in output
*/
protected int engineUpdate(
byte[] input,
int inputOff,
int inputLen,
byte[] output,
int outputOff)
throws ShortBufferException
{
return cipher.update(input, inputOff, inputLen,
output, outputOff);
}
/**
* This method is used to convert the given digest into
* an algorithm specific SecretKey
*/
/**
* This method is provided by the specific implementation
* to create the digest from the password and the algorithm
* parameters. This digest is then used to create the
* SecretKey and the IV for the cipher.
*/
protected abstract void initCipher(
int opmode,
byte[] salt,
int iteration,
char[] password);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -