|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object iaik.cms.EncryptedDataStream iaik.cms.EncryptedData
public class EncryptedData
This class represents the non-stream implementation of the CMS
EncryptedData
type.
Each CMS content type is associated with a specific object identifier, derived from:
pkcs-7 OBJECT IDENTIFIER ::= { iso(1) member-body(2) US(840) rsadsi(113549) pkcs(1) 7 }
The object identifier for the EncryptedData
content type is
defined as:
encryptedData OBJECT IDENTIFIER ::= { pkcs-7 6 }
which corresponds to the OID string "1.2.840.113549.1.7.6".
The Cryptographic Message Syntax (CMS) (RFC 5652)
specifies the EncryptedData
content type for providing a syntax for building encrypted contents. The
encrypted-data content type consists of encrypted content of any type:
EncryptedData ::= SEQUENCE { version CMSVersion, encryptedContentInfo EncryptedContentInfo unprotectedAttrs [1] IMPLICIT UnprotectedAttributes OPTIONAL }
The encryptedContentInfo
field specifies the type of the content
being encrypted, the content-encryption algorithm used for encrypting the content,
and the result of the content encryption. If the encrypted content value
is not present in the encryptedContent
field, it has to be
supplied by other means:
EncryptedContentInfo ::= SEQUENCE { contentType ContentType, contentEncryptionAlgorithm ContentEncryptionAlgorithmIdentifier, encryptedContent [0] IMPLICIT EncryptedContent OPTIONAL }
EncryptedContent ::= OCTET STRING
The key that is used for encrypting the content is not included in the
EncryptedData
structure, it is assumed to be managed by other
means.
If unprotected attributes are present, the version number is 2; otherwise 0:
UnprotectedAttributes ::= SET SIZE (1..MAX) OF Attribute
When creating a new EncryptedData
instance
the encrypted content has to be supplied as EncryptedContentInfo
object.
Example:
//create a EncryptedContentInfo for the data to be encrypted, supplied as byte array: byte[] data = ...; EncryptedContentInfo eci = new EncryptedContentInfo(ObjectID.cms_data, data); //generate secret key and use it for encrypting the content : SecretKey key = eci.setupCipher((AlgorithmID)AlgorithmID.aes256_CBC.clone()); //create an EncryptedData for the EncryptedContentInfo: EncryptedData encrypted_data = new EncryptedData(eci); //Prepare the EncryptedData for transmission by transforming it into an ASN1Object or //immediately performing the DER encoding: ASN1Object = encrypted_data.toASN1Object(); //respectively byte[] encoding = encrypted_data.getEncoded();For initiating a constructed encoding of the inherent encrypted content, use the corresponding
writeTo
method of the parent
EncryptedDataStream
class,
or set a positive blocksize value for the inherent EncryptedContentInfo by means of
the setBlockSize
method of the EncryptedContentInfo
class.
If a positive blocksize is specified, the encrypted content of the inherent
EncryptedContentInfo will be encoded as indefinite primitive
octet string instead of using the default primitive definite encoding scheme:
0x24 0x80 0x04 <blocksize> <first encrypted content block> 0x04 <blocksize> <second encrypted content block> 0x04 <blocksize> <third encrypted content block> ... 0x00 0x00instead of:
0x04 <length> <encrypted content>
Decrypting goes the reverse way: From the DER encoded encryptedData
a new EncryptedData
is created and parsed for the inherent
EncryptedContentInfo
. From the EncryptedContentInfo
the encrypted content is obtained and decrypted using the same secret key:
//create an EncryptedData from its DER encoding: ByteArrayInputStream bais = new ByteArrayInputStream(encoding); EncryptedData encryptedData = new EncryptedDataStream(bais); EncryptedContentInfo eci = (EncryptedContentInfo)encryptedData.getEncryptedContentInfo(); //decrypt the encrypted content using the right secret key: eci.setupCipher(key); //get the recovered data: byte[] data = eci.getContent();
This class additionally supports specific constructors and methods allowing to easily use
the EncryptedData content type for password based encrypting data -- the intended
usage of CMS EncryptedData. Please remark that the following proceeding only
may be used when doing a password based encryption. In all other situations you
have to follow the way described above. However, you also may create your
own EncryptedContentInfo
even when doing a PBE encryption.
If you want to use PBE encryption but not creating an EncryptedContentInfo
by yourself you first have to supply the data to be read from an
byte array, subsequently setup the cipher for PBE-encryption and finally call
a toASN1Object
, getEncoded
or writeTo
method
for preparing the EncryptedData object for transmission by transforming it into an
ASN1Object or immediately encoding it, e.g.:
EncryptedData(byte[] data)
constructor for
creating a new EncryptedData
object and supplying the data to be
encrypted from a byte array:
//the data to be encrypted supplied from a byte array: byte[] data = ...; EncryptedData encrypted_data = new EncryptedData(data);
setupCipher(AlgorithmID contentEA, char[] password)
thereby specifying the PBE-algorithm to be
used and the password, e.g.:
AlgorithmID pbeAlgorithm = (AlgorithmID)AlgorithmID.pbeWithSHAAnd3_KeyTripleDES_CBC.clone(); char[] password = ...; encrypted_data.setupCipher(pbeAlgorithm, password);
toASN1Object
method, the latter by using the
getEncoded
method:
ASN1Object obj = encrypted_data.toASN1Object();respectively
byte[] encoding = encrypted_data.getEncoded();You alternatively may use a proper
writeTo
method of the parent
EncryptedDataStream
class for immediately
encoding this EncryptedData object to an output stream.
When a positive block size is specified for encoding the EncryptedData to a stream,
the encrypted content is BER encoded as indefinite constructed octet string being
composed of a series of definite primitive encoded octet strings of
blockSize
length:
0x24 0x80 0x04 <blocksize> <first encrypted content block> 0x04 <blocksize> <second encrypted content block> 0x04 <blocksize> <third encrypted content block> ... 0x00 0x00instead of:
0x04 <length> <encrypted content>
EncryptedData(ASN1Object obj)
or EncryptedData(InputStream is)
constructor to parse
the internal structure. Before reading the recovered content by
means of the getContent
method, the cipher has to be
initialized for decryption with the password by calling the setupCipher(char[] password)
method:
ByteArrayInputStream bais = new ByteArrayInputStream(encoding); EncryptedData encryptedData = new EncryptedDataStream(bais);
EncryptedContentInfoStream eci = (EncryptedContentInfo)encrypted_data.getEncryptedContentInfo(); System.out.println("Content type: "+eci.getContentType().getName()); System.out.println("Content encryption algorithm: "+eci.getContentEncryptionAlgorithm().getName());
char[] password = ...; encrypted_data.setupCipher(password);Unlike the stream supporting
EncryptedDataStream
class where the setupCipher
method
only initializes the cipher for decryption, whole the encrypted-content decryption
already is performed inside the setupCipher
method of this class.
byte[] content = encrypted_data.getContent();
EncryptedContentInfo
Field Summary |
---|
Fields inherited from class iaik.cms.EncryptedDataStream |
---|
blockSize_, encryptedContentInfo_, securityProvider_, unprotectedAttrs_, version_ |
Constructor Summary | |
---|---|
protected |
EncryptedData()
Default constructor for dynamic object creation in ContentInfo. |
|
EncryptedData(ASN1Object obj)
Creates an EncryptedData object from the given ASN1 object. |
|
EncryptedData(byte[] data)
Creates a new CMS EncryptedData object where the content to be encrypted is read from the supplied byte array. |
|
EncryptedData(EncryptedContentInfo encryptedContentInfo)
Creates a CMS EncryptedData from an EncryptedContentInfo. |
|
EncryptedData(java.io.InputStream is)
Creates a new EncryptedData from a DER encoded EncryptedData which is read from the given InputStream. |
|
EncryptedData(ObjectID contentType,
byte[] data)
Creates a new CMS EncryptedData object where the content to be encrypted is read from the supplied byte array. |
Method Summary | |
---|---|
void |
decode(ASN1Object obj)
Decodes the given EncryptedData ASN1 object. |
void |
decode(java.io.InputStream is)
Reads and decodes an encoded EncryptedData from the given input stream. |
byte[] |
getContent()
Returns the content as byte array. |
byte[] |
getEncoded()
Returns the DER encoding of this EncryptedData in a byte array. |
void |
setInputStream(java.io.InputStream is)
Throws a RuntimeException since not supported. |
protected ASN1Object |
toASN1Object(int blockSize)
Returns this EncryptedData as ASN1Object where a constructed
OCTET STRING is used for encoding the encrypted content. |
Methods inherited from class iaik.cms.EncryptedDataStream |
---|
getBlockSize, getContentType, getEncryptedContentInfo, getInputStream, getSecurityProvider, getUnprotectedAttribute, getUnprotectedAttributes, getVersion, notifyEOF, setBlockSize, setSecurityProvider, setUnprotectedAttributes, setupCipher, setupCipher, setupCipher, setupCipher, setupCipher, toASN1Object, toString, toString, writeTo, writeTo |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Methods inherited from interface iaik.cms.ContentStream |
---|
getBlockSize, getContentType, setBlockSize, toASN1Object, toString |
Constructor Detail |
---|
protected EncryptedData()
public EncryptedData(EncryptedContentInfo encryptedContentInfo)
encryptedContentInfo
- the already created encrypted content infopublic EncryptedData(byte[] data)
Data
.
This constructor only shall be used when intending to PBE encrypt the
data by subsequently calling method setupCipher
thereby supplying PBE-algorithm and
password to be used.
This constructor shall not be used in situations where the desired content
encryption algorithm is not a PBE algorithm. In such cases the EncryptedData(EncryptedContentInfo)
constructor shall be used to be supplied with a precomputed EncryptedContentInfo.
Consult the EncryptedContentInfo
class documentation for more information about
EncryptedContentInfo handling.
data
- the byte array containing the data to encryptpublic EncryptedData(ObjectID contentType, byte[] data)
Data
.
This constructor only shall be used when intending to PBE encrypt the
data by subsequently calling method setupCipher
thereby supplying PBE-algorithm and
password to be used.
This constructor shall not be used in situations where the desired content
encryption algorithm is not a PBE algorithm. In such cases the EncryptedData(EncryptedContentInfo)
constructor shall be used to be supplied with a precomputed EncryptedContentInfo.
Consult the EncryptedContentInfo
class documentation for more information about
EncryptedContentInfo handling.
data
- the byte array containing the data to encryptpublic EncryptedData(ASN1Object obj) throws CMSParsingException
The ASN1Object supplied to this constructor represents an already
exisiting EncryptedData
object that may have
been created by calling toASN1Object
.
obj
- the ASN1Object representing an already exisiting EncryptedData object.
CMSParsingException
- if an error occurs when parsing the given ASN1Objectpublic EncryptedData(java.io.InputStream is) throws java.io.IOException, CMSParsingException
is
- the InputStream holding a DER encoded EncryptedData object
java.io.IOException
- if an I/O error occurs during reading from the InputStream
CMSParsingException
- if an error occurs while parsing the objectMethod Detail |
---|
public void decode(ASN1Object obj) throws CMSParsingException
decode
in interface Content
obj
- the ASN1Object representing an already exisiting EncryptedData object
CMSParsingException
- if an error occurs when parsing the given ASN1Objectpublic void decode(java.io.InputStream is) throws java.io.IOException, CMSParsingException
decode
in interface ContentStream
decode
in class EncryptedDataStream
is
- the InputStream holding a DER encoded EncryptedData object
java.io.IOException
- if an I/O error occurs during reading from the InputStream
CMSParsingException
- if an error occurs while parsing the objectpublic byte[] getContent()
The returned content depends on whether creating a new EncryptedData or parsing an existing one:
public void setInputStream(java.io.InputStream is)
EncryptedDataStream
but throws a
RuntimeException since this method is not supported by this class. The
to-be-encrypted content has to be supplied when creating an EncryptedData object.
setInputStream
in class EncryptedDataStream
is
- ignored
RuntimeRuntimeException
- since method is not supportedprotected ASN1Object toASN1Object(int blockSize) throws CMSException
EncryptedData
as ASN1Object where a constructed
OCTET STRING is used for encoding the encrypted content.
toASN1Object
in class EncryptedDataStream
blockSize
- the block size defining the encoding scheme - and specifying the
length of each primitive encoded octet string component, if positive
EncryptedData
as ASN1Object
CMSException
- if the ASN1Object could not be createdpublic byte[] getEncoded() throws CMSException
If the blockSize value of the inherent
EncryptedContentInfo
structure has been set to a positive value, the encrypted content is
encoded as indefinite constructed octet string being composed of a certain
number of definite primitive encoded octet strings of blockSize
length. If no - or a not-positive - blockSize value has been specified
whole the encrypted content will be encoded as definite primitive octet string.
CMSException
- if an error occurs during the encoding procedure
|
This Javadoc may contain text parts from text parts from IETF Internet Standard specifications (see copyright note). | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |