Bjoern Meier
Bjoern Meier

Reputation: 33

How to decrypt CMS /PKCS#7 with node forge

I try to decode a CMS /PKCS#7 with node forge. Private key and data are in files. Key is PEM, data is DER. That's my code:

import forge from 'node-forge'
import fs from 'fs';

let data = fs.readFileSync( __dirname + '/data');
let Key = fs.readFileSync( __dirname + '/key'); 
let pkeyAsn1 = forge.pki.privateKeyFromPem(Key); 

let p7 = forge.pkcs7;
let asndata =forge.util.createBuffer(data);
let msg = p7.messageFromAsn1(forge.asn1.fromDer(asndata));
console.log(msg.decrypt(msg.recipients[0], pkeyAsn1));

But the output is undefined. The openssl equivalent

openssl cms -decrypt -inform DER -in data -inkey key -out test

works

Upvotes: 1

Views: 1702

Answers (1)

justacodemonkey
justacodemonkey

Reputation: 620

Just to help all those who may come across this in the future, the solution is as follows:

...
msg.decrypt(msg.recipients[0], pkeyAsn1);
let data = msg.content.data;

Upvotes: 1

Related Questions