Top Description Fields Constructors Methods
org.bouncycastle.crypto.encodings

public Class PKCS1Encoding

extends Object
implements AsymmetricBlockCipher
Class Inheritance
All Implemented Interfaces
org.bouncycastle.crypto.AsymmetricBlockCipher
Imports
java.security.SecureRandom, org.bouncycastle.crypto.AsymmetricBlockCipher, .CipherParameters, .CryptoServicesRegistrar, .InvalidCipherTextException, org.bouncycastle.crypto.params.AsymmetricKeyParameter, .ParametersWithRandom, org.bouncycastle.util.Arrays, .Properties

this does your basic PKCS 1 v1.5 padding - whether or not you should be using this depends on your application - see PKCS1 Version 2 for details.

Field Summary

Modifier and TypeField and Description
private byte[]
private AsymmetricBlockCipher
private byte[]
private boolean
private boolean
private static final int
public static final String
NOT_STRICT_LENGTH_ENABLED_PROPERTY

some providers fail to include the leading zero in PKCS1 encoded blocks.

private int
private SecureRandom
public static final String
STRICT_LENGTH_ENABLED_PROPERTY

Deprecated use NOT_STRICT_LENGTH_ENABLED_PROPERTY
private boolean

Constructor Summary

AccessConstructor and Description
public
PKCS1Encoding(AsymmetricBlockCipher cipher)

Basic constructor.

public
PKCS1Encoding(AsymmetricBlockCipher
The cipher to use for cryptographic operation.
cipher
,
int
Length of the expected plaintext.
pLen
)

Constructor for decryption with a fixed plaintext length.

public
PKCS1Encoding(AsymmetricBlockCipher
The cipher to use for cryptographic operation.
cipher
,
byte[]
The fallback value, we don't do an arraycopy here.
fallback
)

Constructor for decryption with a fixed plaintext length and a fallback value that is returned, if the padding is incorrect.

Method Summary

Modifier and TypeMethod and Description
private static int
checkPkcs1Encoding1(byte[] buf)

Check the argument is a valid encoding with type 1.

private static int
checkPkcs1Encoding2(byte[] buf)

Check the argument is a valid encoding with type 2.

private static int
checkPkcs1Encoding2(byte[] buf, int plaintextLength)

Check the argument is a valid encoding with type 2 of a plaintext with the given length.

private byte[]
decodeBlock(byte[] in, int inOff, int inLen)

private byte[]

Returns:

The plaintext without padding, or a random value if the padding was incorrect.
decodeBlockOrRandom
(byte[]
The encrypted block.
in
,
int
Offset in the encrypted block.
inOff
,
int
Length of the encrypted block. //@param pLen Length of the desired output.
inLen
)

Decode PKCS#1.5 encoding, and return a random value if the padding is not correct.

private byte[]
encodeBlock(byte[] in, int inOff, int inLen)

public int
getInputBlockSize()

Implements org.bouncycastle.crypto.AsymmetricBlockCipher.getInputBlockSize.

returns the largest size an input block can be.

public int
getOutputBlockSize()

Implements org.bouncycastle.crypto.AsymmetricBlockCipher.getOutputBlockSize.

returns the maximum size of the block produced by this cipher.

public AsymmetricBlockCipher
public void
init(boolean
if true the cipher is initialised for encryption, if false for decryption.
forEncryption
,
CipherParameters
the key and other data required by the cipher.
param
)

Implements org.bouncycastle.crypto.AsymmetricBlockCipher.init.

initialise the cipher.

public byte[]
processBlock(byte[]
the input data
in
,
int
offset into the in array where the data starts
inOff
,
int
the length of the block to be processed.
inLen
)

Implements org.bouncycastle.crypto.AsymmetricBlockCipher.processBlock.

process the block of len bytes stored in in from offset inOff.

private boolean
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

blockBufferback to summary
private byte[] blockBuffer
engineback to summary
private AsymmetricBlockCipher engine
fallbackback to summary
private byte[] fallback
forEncryptionback to summary
private boolean forEncryption
forPrivateKeyback to summary
private boolean forPrivateKey
HEADER_LENGTHback to summary
private static final int HEADER_LENGTH
NOT_STRICT_LENGTH_ENABLED_PROPERTYback to summary
public static final String NOT_STRICT_LENGTH_ENABLED_PROPERTY

some providers fail to include the leading zero in PKCS1 encoded blocks. If you need to work with one of these set the system property org.bouncycastle.pkcs1.not_strict to true.

The system property is checked during construction of the encoding object, it is set to false by default.

pLenback to summary
private int pLen
randomback to summary
private SecureRandom random
STRICT_LENGTH_ENABLED_PROPERTYback to summary
public static final String STRICT_LENGTH_ENABLED_PROPERTY

Deprecated

use NOT_STRICT_LENGTH_ENABLED_PROPERTY

useStrictLengthback to summary
private boolean useStrictLength

Constructor Detail

PKCS1Encodingback to summary
public PKCS1Encoding(AsymmetricBlockCipher cipher)

Basic constructor.

PKCS1Encodingback to summary
public PKCS1Encoding(AsymmetricBlockCipher cipher, int pLen)

Constructor for decryption with a fixed plaintext length.

Parameters
cipher:AsymmetricBlockCipher

The cipher to use for cryptographic operation.

pLen:int

Length of the expected plaintext.

PKCS1Encodingback to summary
public PKCS1Encoding(AsymmetricBlockCipher cipher, byte[] fallback)

Constructor for decryption with a fixed plaintext length and a fallback value that is returned, if the padding is incorrect.

Parameters
cipher:AsymmetricBlockCipher

The cipher to use for cryptographic operation.

fallback:byte[]

The fallback value, we don't do an arraycopy here.

Method Detail

checkPkcs1Encoding1back to summary
private static int checkPkcs1Encoding1(byte[] buf)

Check the argument is a valid encoding with type 1. Returns the plaintext length if valid, or -1 if invalid.

checkPkcs1Encoding2back to summary
private static int checkPkcs1Encoding2(byte[] buf)

Check the argument is a valid encoding with type 2. Returns the plaintext length if valid, or -1 if invalid.

checkPkcs1Encoding2back to summary
private static int checkPkcs1Encoding2(byte[] buf, int plaintextLength)

Check the argument is a valid encoding with type 2 of a plaintext with the given length. Returns 0 if valid, or -1 if invalid.

decodeBlockback to summary
private byte[] decodeBlock(byte[] in, int inOff, int inLen) throws InvalidCipherTextException
Exceptions
InvalidCipherTextException:
if the decrypted block is not in PKCS1 format.
decodeBlockOrRandomback to summary
private byte[] decodeBlockOrRandom(byte[] in, int inOff, int inLen) throws InvalidCipherTextException

Decode PKCS#1.5 encoding, and return a random value if the padding is not correct.

Parameters
in:byte[]

The encrypted block.

inOff:int

Offset in the encrypted block.

inLen:int

Length of the encrypted block. //@param pLen Length of the desired output.

Returns:byte[]

The plaintext without padding, or a random value if the padding was incorrect.

encodeBlockback to summary
private byte[] encodeBlock(byte[] in, int inOff, int inLen) throws InvalidCipherTextException
getInputBlockSizeback to summary
public int getInputBlockSize()

Implements org.bouncycastle.crypto.AsymmetricBlockCipher.getInputBlockSize.

Doc from org.bouncycastle.crypto.AsymmetricBlockCipher.getInputBlockSize.

returns the largest size an input block can be.

Returns:int

maximum size for an input block.

getOutputBlockSizeback to summary
public int getOutputBlockSize()

Implements org.bouncycastle.crypto.AsymmetricBlockCipher.getOutputBlockSize.

Doc from org.bouncycastle.crypto.AsymmetricBlockCipher.getOutputBlockSize.

returns the maximum size of the block produced by this cipher.

Returns:int

maximum size of the output block produced by the cipher.

getUnderlyingCipherback to summary
public AsymmetricBlockCipher getUnderlyingCipher()
initback to summary
public void init(boolean forEncryption, CipherParameters param)

Implements org.bouncycastle.crypto.AsymmetricBlockCipher.init.

Doc from org.bouncycastle.crypto.AsymmetricBlockCipher.init.

initialise the cipher.

Parameters
forEncryption:boolean

if true the cipher is initialised for encryption, if false for decryption.

param:CipherParameters

the key and other data required by the cipher.

processBlockback to summary
public byte[] processBlock(byte[] in, int inOff, int inLen) throws InvalidCipherTextException

Implements org.bouncycastle.crypto.AsymmetricBlockCipher.processBlock.

Doc from org.bouncycastle.crypto.AsymmetricBlockCipher.processBlock.

process the block of len bytes stored in in from offset inOff.

Parameters
in:byte[]

the input data

inOff:int

offset into the in array where the data starts

inLen:int

the length of the block to be processed.

Returns:byte[]

the resulting byte array of the encryption/decryption process.

Exceptions
InvalidCipherTextException:
data decrypts improperly.
useStrictback to summary
private boolean useStrict()