Pergunta

I am using pkcs7 encrypt decrypt in current project. I want to change from PHP to Node.js. Is there pkcs7 encrypt/decrypt in Node.js ?

In PHP,

<?php

$data = <<<EOD
Hello world
EOD;

// load key
$key = file_get_contents("mypublickey.crt");

// save message to file
$fp = fopen("msg.txt", "w");
fwrite($fp, $data);
fclose($fp);

// encrypt it
if (openssl_pkcs7_encrypt("msg.txt", "enc.txt", $key,array())) {
    // message encrypted - send it!

}
?>

to decrypt

<?php
// The certification stuff
$public = file_get_contents("mypublickey.crt");
$private = array(file_get_contents("myprivatekey.pem"), "mypassword");

$infile = tempnam("", "enc");
file_put_contents($infile, $encrypted); 
$outfile = tempnam("", "dec");

if(openssl_pkcs7_decrypt("enc.txt", "dec.txt", $public, $private))
{
    // Decryption successful
    echo file_get_contents("dec.txt");
}
?>

Is there any similar function like this in Node.js ?

Foi útil?

Solução

I've faced the same issue and spent too much time but I found a way in the end.

I found and used forge open source lib. You can simply add to your project by following:

npm install node-forge

Then, code snippet below performs encryption with PKCS#7 format.

var forge = require('node-forge');

// create cert object
var cert = forge.pki.certificateFromPem(certOrPemString);
// create envelop data
var p7 = forge.pkcs7.createEnvelopedData();
// add certificate as recipient
p7.addRecipient(cert);
// set content 
p7.content = forge.util.createBuffer();
p7.content.putString('content to be encrypted');

// encrypt
p7.encrypt();

// obtain encrypted data with DER format
var bytes = forge.asn1.toDer(p7.toAsn1()).getBytes();

This code block will encrypt the content you provided and return a byte array with DER output format.

You can convert byte array to UTF-8 string by following:

var str = Buffer.from(bytes, 'binary').toString('utf8');

And you can decrypt the content as follows:

var recipient = p7.findRecipient(cert);
// decrypt
p7.decrypt(p7.recipients[0], privateKey); 

Hope this may help.

Outras dicas

To Decrypt Data

Example Data:

MIICTgYJKoZIhvcNAQcDoIICPzCCAjsCAQAxggHGMIIBwgIBADCBqTCBmzELMAkGA1UEBhMCREUxEjAQBgNVBAgMCUZyYW5jb25pYTEQMA4GA1UEBwwHQW5zYmFjaDEVMBMGA1UECgwMU3RlZmFuIFNpZWdsMRIwEAYDVQQLDAlHZWllcmxlaW4xFjAUBgNVBAMMDUdlaWVybGVpbiBERVYxIzAhBgkqhkiG9w0BCQEWFHN0ZXNpZUBicm9rZW5waXBlLmRlAgkA1FQcQNg14vMwDQYJKoZIhvcNAQEBBQAEggEAJhWQz5SniCd1w3A8uKVZEfc8Tp21I7FMfFqou+UOVsZCq7kcEa9uv2DIj3o7zD8wbLK1fuyFi4SJxTwxkR0a6V4bbonIpXPPJ1f615dc4LydAi2tv5w14LJ1Js5XCgGVnkAmQHDaW3EHXB7XT4w9PR3+tcS/5YAnWaM6Es38zCKHd7TnHpuakplIkwSK9rBFAyA1g/IyTPI+ktrEEHcVuJcz/7eTlF6wJEa2HL8F1TVWuL0p/0GsJP/8y0MYGdCdtr+TIVo//3YGhoBlN4tnheFT/jRAzfCZtflDdgAukW24CekrJ1sG2M42p5cKQ5rGFQtzNy/n8EjtUutOHD5YITBsBgkqhkiG9w0BBwEwHQYJYIZIAWUDBAEqBBBmlpfy3WrYj3uWW7+xNEiHgEAm2mfSF5xFPLEqqFkvKTM4w8PfhnF0ehmfQNApvoWQRQanNWLCT+Q9GHx6DCFjTUHl+53x88BrCl1E7FhYPs92

        let data = '-----BEGIN PKCS7-----\r\n' + body + '\r\n-----END PKCS7-----\r\n';
        let p7d = forge.pkcs7.messageFromPem(data)
        let privateCert = forge.pki.decryptRsaPrivateKey(fs.readFileSync(privateCertPath),'password');
        p7d.decrypt(p7d.recipients[0], privateCert);
        console.log(p7d.content)
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top