Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.bouncycastle.pqc.jcajce.provider.util;
  
  import java.security.Key;
 
The AsymmetricBlockCipher class extends CipherSpiExt. NOTE: Some Ciphers are using Padding. OneAndZeroesPadding is used as default padding. However padding can still be specified, but mode is not supported; if you try to instantiate the cipher with something else than "NONE" as mode NoSuchAlgorithmException is thrown.
 
 public abstract class AsymmetricBlockCipher
     extends CipherSpiExt
 {

    
ParameterSpec used with this cipher
 
     protected AlgorithmParameterSpec paramSpec;

    
Internal buffer
 
     protected ByteArrayOutputStream buf;

    
The maximum number of bytes the cipher can decrypt.
 
     protected int maxPlainTextSize;

    
The maximum number of bytes the cipher can encrypt.
 
     protected int cipherTextSize;

    
The AsymmetricBlockCipher() constructor
 
     public AsymmetricBlockCipher()
     {
          = new ByteArrayOutputStream();
     }

    
Return the block size (in bytes). Note: although the ciphers extending this class are not block ciphers, the method was adopted to return the maximal plaintext and ciphertext sizes for non hybrid ciphers. If the cipher is hybrid, it returns 0.

Returns:
if the cipher is not a hybrid one the max plain/cipher text size is returned, otherwise 0 is returned
 
     public final int getBlockSize()
     {
         return  ==  ?  : ;
     }

    

Returns:
null since no initialization vector is used.
 
     public final byte[] getIV()
     {
         return null;
     }

    
Return 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 inLen (in bytes). This call takes into account any unprocessed (buffered) data from a previous update call, and padding. The actual output length of the next update() or doFinal() call may be smaller than the length returned by this method.

If the input length plus the length of the buffered data exceeds the maximum length, 0 is returned.

Parameters:
inLen the length of the input
Returns:
the length of the ciphertext or 0 if the input is too long.
 
     public final int getOutputSize(int inLen)
     {
 
         int totalLen = inLen + .size();
 
         int maxLen = getBlockSize();
 
         if (totalLen > maxLen)
        {
            // the length of the input exceeds the maximal supported length
            return 0;
        }
        return maxLen;
    }

    
Returns the parameters used with this cipher.

The returned parameters may be the same that were used to initialize this cipher, or may contain the default set of parameters or a set of randomly generated parameters used by the underlying cipher implementation (provided that the underlying cipher implementation uses a default set of parameters or creates new parameters if it needs parameters but was not initialized with any).

Returns:
the parameters used with this cipher, or null if this cipher does not use any parameters.
    public final AlgorithmParameterSpec getParameters()
    {
        return ;
    }

    
Initializes the cipher for encryption by forwarding it to initEncrypt(Key, FlexiSecureRandom).

If this cipher requires any algorithm parameters that cannot be derived from the given key, the underlying cipher implementation is supposed to generate the required parameters itself (using provider-specific default or random values) if it is being initialized for encryption, and raise an InvalidKeyException if it is being initialized for decryption. The generated parameters can be retrieved using engineGetParameters or engineGetIV (if the parameter is an IV).

Parameters:
key the encryption or decryption key.
Throws:
java.security.InvalidKeyException if the given key is inappropriate for initializing this cipher.
    public final void initEncrypt(Key key)
        throws InvalidKeyException
    {
        try
        {
            initEncrypt(keynullnew SecureRandom());
        }
        catch (InvalidAlgorithmParameterException e)
        {
            throw new InvalidParameterException(
                "This cipher needs algorithm parameters for initialization (cannot be null).");
        }
    }

    
Initialize this cipher for encryption by forwarding it to initEncrypt(Key, FlexiSecureRandom, AlgorithmParameterSpec).

If this cipher requires any algorithm parameters that cannot be derived from the given key, the underlying cipher implementation is supposed to generate the required parameters itself (using provider-specific default or random values) if it is being initialized for encryption, and raise an InvalidKeyException if it is being initialized for decryption. The generated parameters can be retrieved using engineGetParameters or engineGetIV (if the parameter is an IV).

Parameters:
key the encryption or decryption key.
random the source of randomness.
Throws:
java.security.InvalidKeyException if the given key is inappropriate for initializing this cipher.
    public final void initEncrypt(Key keySecureRandom random)
        throws InvalidKeyException
    {
        try
        {
            initEncrypt(keynullrandom);
        }
        catch (InvalidAlgorithmParameterException iape)
        {
            throw new InvalidParameterException(
                "This cipher needs algorithm parameters for initialization (cannot be null).");
        }
    }

    
Initializes the cipher for encryption by forwarding it to initEncrypt(Key, FlexiSecureRandom, AlgorithmParameterSpec).

Parameters:
key the encryption or decryption key.
params the algorithm parameters.
Throws:
java.security.InvalidKeyException if the given key is inappropriate for initializing this cipher.
java.security.InvalidAlgorithmParameterException if the given algortihm parameters are inappropriate for this cipher, or if this cipher is being initialized for decryption and requires algorithm parameters and params is null.
    public final void initEncrypt(Key keyAlgorithmParameterSpec params)
    {
        initEncrypt(keyparamsnew SecureRandom());
    }

    
This method initializes the AsymmetricBlockCipher with a certain key for data encryption.

If this cipher (including its underlying feedback or padding scheme) requires any random bytes (e.g., for parameter generation), it will get them from random.

Note that when a Cipher object is initialized, it loses all previously-acquired state. In other words, initializing a Cipher is equivalent to creating a new instance of that Cipher and initializing it

Parameters:
key the key which has to be used to encrypt data.
secureRandom the source of randomness.
params the algorithm parameters.
Throws:
java.security.InvalidKeyException if the given key is inappropriate for initializing this cipher
java.security.InvalidAlgorithmParameterException if the given algorithm parameters are inappropriate for this cipher, or if this cipher is being initialized for decryption and requires algorithm parameters and params is null.
    public final void initEncrypt(Key keyAlgorithmParameterSpec params,
                                  SecureRandom secureRandom)
        throws InvalidKeyException,
    {
         = ;
        initCipherEncrypt(keyparamssecureRandom);
    }

    
Initialize the cipher for decryption by forwarding it to initDecrypt(java.security.Key,java.security.spec.AlgorithmParameterSpec).

If this cipher requires any algorithm parameters that cannot be derived from the given key, the underlying cipher implementation is supposed to generate the required parameters itself (using provider-specific default or random values) if it is being initialized for encryption, and raise an InvalidKeyException if it is being initialized for decryption. The generated parameters can be retrieved using engineGetParameters or engineGetIV (if the parameter is an IV).

Parameters:
key the encryption or decryption key.
Throws:
java.security.InvalidKeyException if the given key is inappropriate for initializing this cipher.
    public final void initDecrypt(Key key)
        throws InvalidKeyException
    {
        try
        {
            initDecrypt(keynull);
        }
        catch (InvalidAlgorithmParameterException iape)
        {
            throw new InvalidParameterException(
                "This cipher needs algorithm parameters for initialization (cannot be null).");
        }
    }

    
This method initializes the AsymmetricBlockCipher with a certain key for data decryption.

If this cipher (including its underlying feedback or padding scheme) requires any random bytes (e.g., for parameter generation), it will get them from random.

Note that when a Cipher object is initialized, it loses all previously-acquired state. In other words, initializing a Cipher is equivalent to creating a new instance of that Cipher and initializing it

Parameters:
key the key which has to be used to decrypt data.
params the algorithm parameters.
Throws:
java.security.InvalidKeyException if the given key is inappropriate for initializing this cipher
java.security.InvalidAlgorithmParameterException if the given algorithm parameters are inappropriate for this cipher, or if this cipher is being initialized for decryption and requires algorithm parameters and params is null.
    public final void initDecrypt(Key keyAlgorithmParameterSpec params)
    {
         = ;
        initCipherDecrypt(keyparams);
    }

    
Continue a multiple-part encryption or decryption operation. This method just writes the input into an internal buffer.

Parameters:
input byte array containing the next part of the input
inOff index in the array where the input starts
inLen length of the input
Returns:
a new buffer with the result (always empty)
    public final byte[] update(byte[] inputint inOffint inLen)
    {
        if (inLen != 0)
        {
            .write(inputinOffinLen);
        }
        return new byte[0];
    }

    
Continue a multiple-part encryption or decryption operation (depending on how this cipher was initialized), processing another data part.

Parameters:
input the input buffer
inOff the offset where the input starts
inLen the input length
output the output buffer
outOff the offset where the result is stored
Returns:
the length of the output (always 0)
    public final int update(byte[] inputint inOffint inLenbyte[] output,
                            int outOff)
    {
        update(inputinOffinLen);
        return 0;
    }

    
Finish a multiple-part encryption or decryption operation (depending on how this cipher was initialized).

Parameters:
input the input buffer
inOff the offset where the input starts
inLen the input length
Returns:
a new buffer with the result
Throws:
javax.crypto.IllegalBlockSizeException if the plaintext or ciphertext size is too large.
javax.crypto.BadPaddingException if the ciphertext is invalid.
    public final byte[] doFinal(byte[] inputint inOffint inLen)
    {
        checkLength(inLen);
        update(inputinOffinLen);
        byte[] mBytes = .toByteArray();
        .reset();
        switch ()
        {
        case :
            return messageEncrypt(mBytes);
        case :
            return messageDecrypt(mBytes);
        default:
            return null;
        }
    }

    
Finish a multiple-part encryption or decryption operation (depending on how this cipher was initialized).

Parameters:
input the input buffer
inOff the offset where the input starts
inLen the input length
output the buffer for the result
outOff the offset where the result is stored
Returns:
the output length
Throws:
javax.crypto.ShortBufferException if the output buffer is too small to hold the result.
javax.crypto.IllegalBlockSizeException if the plaintext or ciphertext size is too large.
javax.crypto.BadPaddingException if the ciphertext is invalid.
    public final int doFinal(byte[] inputint inOffint inLenbyte[] output,
                             int outOff)
        BadPaddingException
    {
        if (output.length < getOutputSize(inLen))
        {
            throw new ShortBufferException("Output buffer too short.");
        }
        byte[] out = doFinal(inputinOffinLen);
        System.arraycopy(out, 0, outputoutOffout.length);
        return out.length;
    }

    
Since asymmetric block ciphers do not support modes, this method does nothing.

Parameters:
modeName the cipher mode (unused)
    protected final void setMode(String modeName)
    {
        // empty
    }

    
Since asymmetric block ciphers do not support padding, this method does nothing.

Parameters:
paddingName the name of the padding scheme (not used)
    protected final void setPadding(String paddingName)
    {
        // empty
    }

    
Check if the message length plus the length of the input length can be en/decrypted. This method uses the specific values maxPlainTextSize and cipherTextSize which are set by the implementations. If the input length plus the length of the internal buffer is greater than maxPlainTextSize for encryption or not equal to cipherTextSize for decryption, an javax.crypto.IllegalBlockSizeException will be thrown.

Parameters:
inLen length of the input to check
Throws:
javax.crypto.IllegalBlockSizeException if the input length is invalid.
    protected void checkLength(int inLen)
        throws IllegalBlockSizeException
    {
        int inLength = inLen + .size();
        if ( == )
        {
            if (inLength > )
            {
                throw new IllegalBlockSizeException(
                    "The length of the plaintext (" + inLength
                        + " bytes) is not supported by "
                        + "the cipher (max. " + 
                        + " bytes).");
            }
        }
        else if ( == )
        {
            if (inLength != )
            {
                throw new IllegalBlockSizeException(
                    "Illegal ciphertext length (expected " + 
                        + " bytes, was " + inLength + " bytes).");
            }
        }
    }

    
Initialize the AsymmetricBlockCipher with a certain key for data encryption.

Parameters:
key the key which has to be used to encrypt data
params the algorithm parameters
sr the source of randomness
Throws:
java.security.InvalidKeyException if the given key is inappropriate for initializing this cipher.
java.security.InvalidAlgorithmParameterException if the given parameters are inappropriate for initializing this cipher.
    protected abstract void initCipherEncrypt(Key key,
                                              AlgorithmParameterSpec paramsSecureRandom sr)
Initialize the AsymmetricBlockCipher with a certain key for data encryption.

Parameters:
key the key which has to be used to decrypt data
params the algorithm parameters
Throws:
java.security.InvalidKeyException if the given key is inappropriate for initializing this cipher
java.security.InvalidAlgorithmParameterException if the given parameters are inappropriate for initializing this cipher.
    protected abstract void initCipherDecrypt(Key key,
                                              AlgorithmParameterSpec params)
        throws InvalidKeyException,
        InvalidAlgorithmParameterException;

    
Encrypt the message stored in input. The method should also perform an additional length check.

Parameters:
input the message to be encrypted (usually the message length is less than or equal to maxPlainTextSize)
Returns:
the encrypted message (it has length equal to maxCipherTextSize_)
Throws:
javax.crypto.IllegalBlockSizeException if the input is inappropriate for this cipher.
javax.crypto.BadPaddingException if the input format is invalid.
    protected abstract byte[] messageEncrypt(byte[] input)
        throws IllegalBlockSizeExceptionBadPaddingException;

    
Decrypt the ciphertext stored in input. The method should also perform an additional length check.

Parameters:
input the ciphertext to be decrypted (the ciphertext length is less than or equal to maxCipherTextSize)
Returns:
the decrypted message
Throws:
javax.crypto.IllegalBlockSizeException if the input is inappropriate for this cipher.
javax.crypto.BadPaddingException if the input format is invalid.
    protected abstract byte[] messageDecrypt(byte[] input)
New to GrepCode? Check out our FAQ X