最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

PKCS7在Node.js中加密解密

IT培训 admin 5浏览 0评论

PKCS7在Node.js中加密解密

我在当前项目中使用pkcs7加密解密。我想从PHP更改为Node.js.在Node.js中是否有pkcs7加密/解密?

在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!

}
?>

解密

<?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");
}
?>

在Node.js中是否有类似的功能?

回答如下:

我遇到了同样的问题,花了太多时间,但我最终找到了办法。

我发现并使用了forge开源库。您可以通过以下方式简单地添加到项目中:

npm install node-forge

然后,下面的代码片段使用PKCS#7格式执行加密。

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();

此代码块将加密您提供的内容,并返回带有DER输出格式的字节数组。

您可以通过以下方式将字节数组转换为UTF-8字符串:

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

您可以按如下方式解密内容:

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

希望这可能有所帮助。

PKCS7在Node.js中加密解密

我在当前项目中使用pkcs7加密解密。我想从PHP更改为Node.js.在Node.js中是否有pkcs7加密/解密?

在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!

}
?>

解密

<?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");
}
?>

在Node.js中是否有类似的功能?

回答如下:

我遇到了同样的问题,花了太多时间,但我最终找到了办法。

我发现并使用了forge开源库。您可以通过以下方式简单地添加到项目中:

npm install node-forge

然后,下面的代码片段使用PKCS#7格式执行加密。

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();

此代码块将加密您提供的内容,并返回带有DER输出格式的字节数组。

您可以通过以下方式将字节数组转换为UTF-8字符串:

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

您可以按如下方式解密内容:

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

希望这可能有所帮助。

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论