W3C XML 为 Node.js 加密执行http://www.w3.org/TR/xmlenc-core/ (中文(简体) )(中文(简体) )
节点 18+ 不支持三重DES 算法,因为 https://github.com/nodejs/node/issues/52017 (英语)
使用量
npm install xml-encryption
加密
var xmlenc = require('xml-encryption');
var options = {
rsa_pub: fs.readFileSync(__dirname + '/your_rsa.pub'),
pem: fs.readFileSync(__dirname + '/your_public_cert.pem'),
encryptionAlgorithm: 'http://www.w3.org/2001/04/xmlenc#aes256-cbc',
keyEncryptionAlgorithm: 'http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p',
keyEncryptionDigest: 'sha1',
disallowEncryptionWithInsecureAlgorithm: true,
warnInsecureAlgorithm: true
};
xmlenc.encrypt('content to encrypt', options, function(err, result) {
console.log(result);
}
结果 :
<xenc:EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element" xmlns:xenc="http://www.w3.org/2001/04/xmlenc#">
<xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes-256-cbc" />
<KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
<e:EncryptedKey xmlns:e="http://www.w3.org/2001/04/xmlenc#">
<e:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p">
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
</e:EncryptionMethod>
<KeyInfo>
<X509Data><X509Certificate>MIIEDzCCAveg... base64 cert... q3uaLvlAUo=</X509Certificate></X509Data>
</KeyInfo>
<e:CipherData>
<e:CipherValue>sGH0hhzkjmLWYYY0gyQMampDM... encrypted symmetric key ...gewHMbtZafk1MHh9A==</e:CipherValue>
</e:CipherData>
</e:EncryptedKey>
</KeyInfo>
<xenc:CipherData>
<xenc:CipherValue>V3Vb1vDl055Lp92zvK..... encrypted content.... kNzP6xTu7/L9EMAeU</xenc:CipherValue>
</xenc:CipherData>
</xenc:EncryptedData>
解密
var options = {
key: fs.readFileSync(__dirname + '/your_private_key.key'),
disallowDecryptionWithInsecureAlgorithm: true,
warnInsecureAlgorithm: true
};
xmlenc.decrypt('<xenc:EncryptedData ..... </xenc:EncryptedData>', options, function(err, result) {
console.log(result);
}
// result
decrypted content
支持的算法
目前,图书馆支持:
-
使用 :
-
使用 :
不可靠的算法可以通过 disallowEncryptionWithInsecureAlgorithm编号 :disallowDecryptionWithInsecureAlgorithm 标记在加密/解密时设置为虚假。 在4.0版本后,这些旗帜默认为真实(禁止不安全算法).
警告会被传到 stderr 默认使用 Coper.warn () 当使用前述算法时, 上面提到的旗帜是虚假的 。 这可以通过 warnInsecureAlgorithm 旗帜.
我们建议采用AES-256-GCM(伽卢瓦/顾问模式),以采取最强的安全姿态,并配合当前行业最佳做法.
请注意: xml-encryption 4.0前的版本支持AES-128-CBC和AES-256-CBC作为安全算法. 在4.0版本起,这些都被视为不安全,因为它们使用Cipher Block chaining(CBC)的加密模式,不提供完整性保障. 继续使用AES128-CBC和AES256-CBC,从而能够通过下列手段支持不安全算法: disallowEncryptionWithInsecureAlgorithm/disallowDecryptionWithInsecureAlgorithm。 。 。 .
RSA- OAEP 口罩生成( MGF1)
http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p 将掩码生成函数修正为 MGF1 与 SHA-1单位 XML 加密 1.1 § 5.5.2。 。 。 . keyEncryptionDigest 只选择 OAEP 信件摘要, 所以 keyEncryptionDigest: 'sha256' 指OAEP-SHA256与MGF1-SHA1.
要使用不同的 MGF1 文摘,请使用 XML 加密 1.1 标识符,该标识符带有明确的 <MGF> 元素 :
var options = {
keyEncryptionAlgorithm: 'http://www.w3.org/2009/xmlenc11#rsa-oaep',
keyEncryptionDigest: 'sha256',
keyEncryptionMgf: 'sha256' // sha1 | sha224 | sha256 | sha384 | sha512, default sha1
};
keyEncryptionMgf 接受短文摘名称(sha1, (中文(简体) ), sha224, (中文(简体) ), sha256, (中文(简体) ), sha384,或 sha512或全数 http://www.w3.org/2009/xmlenc11#mgf1* URI (英语). 拒绝 rsa-oaep-mgf1p,除SHA-1外,它没有有效的多边治理框架.
可选择 OAEP 标签作为 keyEncryptionOaepParams (a 缓冲或基数64字符串);其发射方式为 <OAEPparams> 以解密为荣.
注:用于节点的文摘/MGF1组合 crypto 无法表达, OAEP 编码在 JavaScript 中以原始的 RSA 原始计算 。 当节点使用一个符合FIPS的加密提供者时,该代码路径被屏蔽了,无法为OpenSSL的C执行提供常时保证. 只有当MGF1摘要与信件摘要不同时才使用;所有其他组合都经过 crypto.privateDecrypt 不变.
中断更改 : 3.1.0至5.0.0版本, rsa-oaep-mgf1p 与 keyEncryptionDigest: 'sha256' 或者说 'sha512' 使用MGF1-SHA256或MGF1-SHA512生产密码文本,从未符合W3C规格. rsa-oaep-mgf1p 现生产MGF1-SHA1密码文本,不论 keyEncryptionDigest。与早期行为加密的文档不会与当前版本解密;它们从未与Java xmlsec,.NET互通 System.Security.Cryptography.Xml,或者其他符合规格的同级。 真正需要MGF1-SHA256或MGF1-SHA512的呼叫者应使用 http://www.w3.org/2009/xmlenc11#rsa-oaep 与 keyEncryptionMgf 选项.
允许在解密时列出特定的算法
如果用 disallowEncryptionWithInsecureAlgorithm: true,您可能只希望支持一组不安全的算法(例如,仅支持AES-256-CBC)。 这可以通过使用以下代码提取出加密算法并按需要应用验证来实现.
const xmldom = require('@xmldom/xmldom');
const xpath = require('xpath');
const doc = new xmldom.DOMParser().parseFromString(xmlString);
const encryptionMethod = xpath.select("//*[local-name(.)='EncryptedData']/*[local-name(.)='EncryptionMethod']", doc)[0];
const encryptionAlgorithm = encryptionMethod.getAttribute('Algorithm');
问题报告
如果您找到错误或有特性请求, 请在此寄存器问题节中报告 。 请不要在公共 GitHub 问题跟踪器上报告安全漏洞 。 这个 负责任的披露程序 详细说明披露安全问题的程序.
作者
许可证
这个项目根据麻省理工学院的许可证获得许可。 见 自由主义 需要更多信息的文件.
释放
在 github 发布页下可找到发布注释 : https://github.com/auth0/node-xml-加密/放行
