Skip to main content

Class: EncryptedData

Represents the EncryptedData structure described in RFC5652

Example

The following example demonstrates how to create and encrypt CMS Encrypted Data

const cmsEncrypted = new pkijs.EncryptedData();

await cmsEncrypted.encrypt({
contentEncryptionAlgorithm: {
name: "AES-GCM",
length: 256,
},
hmacHashAlgorithm: "SHA-256",
iterationCount: 1000,
password: password,
contentToEncrypt: dataToEncrypt,
});

// Add Encrypted Data into CMS Content Info
const cmsContent = new pkijs.ContentInfo();
cmsContent.contentType = pkijs.ContentInfo.ENCRYPTED_DATA;
cmsContent.content = cmsEncrypted.toSchema();

const cmsContentRaw = cmsContent.toSchema().toBER();

Example

The following example demonstrates how to decrypt CMS Encrypted Data

// Parse CMS Content Info
const cmsContent = pkijs.ContentInfo.fromBER(cmsContentRaw);
if (cmsContent.contentType !== pkijs.ContentInfo.ENCRYPTED_DATA) {
throw new Error("CMS is not Encrypted Data");
}
// Parse CMS Encrypted Data
const cmsEncrypted = new pkijs.EncryptedData({ schema: cmsContent.content });

// Decrypt data
const decryptedData = await cmsEncrypted.decrypt({
password: password,
});

Hierarchy

Implements

Constructors

constructor

new EncryptedData(parameters?): EncryptedData

Initializes a new instance of the EncryptedData class

Parameters

NameTypeDescription
parametersEncryptedDataParametersInitialization parameters

Returns

EncryptedData

Overrides

PkiObject.constructor

Properties

encryptedContentInfo

encryptedContentInfo: EncryptedContentInfo

Encrypted content information

Implementation of

IEncryptedData.encryptedContentInfo


unprotectedAttrs

Optional unprotectedAttrs: Attribute[]

Collection of attributes that are not encrypted

Implementation of

IEncryptedData.unprotectedAttrs


version

version: number

Version number.

If unprotectedAttrs is present, then the version MUST be 2. If unprotectedAttrs is absent, then version MUST be 0.

Implementation of

IEncryptedData.version


CLASS_NAME

Static CLASS_NAME: string = "EncryptedData"

Name of the class

Overrides

PkiObject.CLASS_NAME

Accessors

className

get className(): string

Returns

string

Inherited from

PkiObject.className

Methods

decrypt

decrypt(parameters, crypto?): Promise<ArrayBuffer>

Creates a new CMS Encrypted Data content

Parameters

NameTypeDescription
parametersObjectParameters necessary for encryption
parameters.passwordArrayBuffer-
cryptoICryptoEngineCrypto engine

Returns

Promise<ArrayBuffer>

Returns decrypted raw data


encrypt

encrypt(parameters, crypto?): Promise<void>

Creates a new CMS Encrypted Data content

Parameters

NameTypeDescription
parametersEncryptedDataEncryptParamsParameters necessary for encryption
cryptoICryptoEngine-

Returns

Promise<void>


fromSchema

fromSchema(schema): void

Converts parsed ASN.1 object into current class

Parameters

NameTypeDescription
schemaanyASN.1 schema

Returns

void

Overrides

PkiObject.fromSchema


toJSON

toJSON(): EncryptedDataJson

Converts the class to JSON object

Returns

EncryptedDataJson

JSON object

Overrides

PkiObject.toJSON


toSchema

toSchema(): Sequence

Converts current object to ASN.1 object and sets correct values

Returns

Sequence

ASN.1 object

Overrides

PkiObject.toSchema


toString

toString(encoding?): string

Parameters

NameTypeDefault value
encoding"base64" | "base64url" | "hex""hex"

Returns

string

Inherited from

PkiObject.toString


blockName

blockName(): string

Returns block name

Returns

string

Returns string block name

Inherited from

PkiObject.blockName


compareWithDefault

compareWithDefault(memberName, memberValue): boolean

Compare values with default values for all class members

Parameters

NameTypeDescription
memberNamestringString name for a class member
memberValueanyValue to compare with default value

Returns

boolean


defaultValues

defaultValues(memberName): number

Returns default values for all class members

Parameters

NameTypeDescription
memberName"version"String name for a class member

Returns

number

Default value

Overrides

PkiObject.defaultValues

defaultValues(memberName): EncryptedContentInfo

Parameters

NameType
memberName"encryptedContentInfo"

Returns

EncryptedContentInfo

Overrides

PkiObject.defaultValues

defaultValues(memberName): Attribute[]

Parameters

NameType
memberName"unprotectedAttrs"

Returns

Attribute[]

Overrides

PkiObject.defaultValues


fromBER

fromBER<T>(this, raw): T

Creates PKI object from the raw data

Type parameters

NameType
Textends PkiObject

Parameters

NameTypeDescription
thisPkiObjectConstructor<T>-
rawBufferSourceASN.1 encoded raw data

Returns

T

Initialized and filled current class object

Inherited from

PkiObject.fromBER


schema

schema(parameters?): any

Returns value of pre-defined ASN.1 schema for current class

Parameters

NameTypeDescription
parametersSchemaParameters<{ encryptedContentInfo?: EncryptedContentInfoSchema ; unprotectedAttrs?: string ; version?: string }>Input parameters for the schema

Returns

any

ASN.1 schema object

Overrides

PkiObject.schema