- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一些使用 SubtleCrypto 的代码来加密 key 并将其存储在数据库中。
对于另一个功能,我必须能够在 Node 12 中对其进行加密。如果没有 SubtleCrypto,我必须在 Crypto 中重新创建该功能。
我得到了相同大小的输出,但它似乎无法被 SubtleCrypto 解密,我正在尝试找出哪里出了问题。
这是在浏览器上使用 SubtleCrypto 运行的代码:
key = getKeyMaterial(password)];
salt = base64ToArraybuffer(optionalSalt)
aesKey = crypto.subtle.deriveKey(
{
name: constants.algorithms.pbkdf2,
salt: salt,
iterations: pbkdf2Iterations,
hash: { name: constants.hash.sha256 },
},
key,
{
name: constants.algorithms.aesGcm,
length: aesWrapKeyBitsLength,
},
true,
['wrapKey', 'unwrapKey']
),
return {
salt: arraybufferTobase64(salt),
aesKey: aesKey,
}
function getKeyMaterial(password) {
var enc = new TextEncoder();
crypto.subtle.importKey(
constants.format.raw,
enc.encode(password),
{
name: constants.algorithms.pbkdf2,
},
false,
['deriveKey']
)
如果没有 SubtleCrypto,在 Node 12 中,我被迫使用 Crypto 库。这是我当前的代码迭代。
const ivByteLength = 12;
function wrapKey(privateKey, aesKey) {
const IV = crypto.randomBytes(ivByteLength);
const ALGORITHM = 'aes-256-gcm';
const cipher = crypto.createCipheriv(ALGORITHM, aesKey, IV);
let encrypted = cipher.update(privateKey, undefined, 'binary');
encrypted += cipher.final('binary');
const authTag = cipher.getAuthTag();
encrypted += authTag;
const output = {
wrappedKey: arraybufferTobase64(Buffer.from(encrypted, 'ascii')),
iv: arraybufferTobase64(IV),
};
return output;
}
async function deriveAesGcmKey(password, salt) {
return new Promise((resolve, reject) => {
crypto.pbkdf2(password, salt, 100000, 32, 'sha256', (err, derivedKey) => {
if (err) reject(err);
else resolve(derivedKey);
});
});
}
function arraybufferTobase64(buffer) {
let binary = '';
const bytes = new Uint8Array(buffer);
const len = bytes.byteLength;
for (let i = 0; i < len; i += 1) {
binary += String.fromCharCode(bytes[i]);
}
return btoa(binary);
}
function base64ToArraybuffer(base64) {
const binary = atob(base64);
const len = binary.length;
const bytes = new Uint8Array(len);
for (let i = 0; i < len; i += 1) {
bytes[i] = binary.charCodeAt(i);
}
return bytes;
}
两种实现中包装的 key 具有相同的大小,但 Node 生成的 key 无法在浏览器中解开包装。
我假设某些默认值是错误的还是其他什么?
最佳答案
NodeJS 在 crypto
包中有一个名为 webcrypto
类,它具有微妙的加密实现。
示例来自Docs :
const { subtle } = require('crypto').webcrypto;
async function digest(data, algorithm = 'SHA-512') {
const ec = new TextEncoder();
const digest = await subtle.digest(algorithm, ec.encode(data));
return digest;
}
另请查看:https://nodejs.org/api/webcrypto.html#webcrypto_class_subtlecrypto
编辑:
正如低版本 Node 的评论中所述,您可以使用像 https://www.npmjs.com/package/@peculiar/webcrypto 这样的填充程序。它是 Nodejs 版本高于 Node10 的 webcrypto 实现
关于javascript - 在 Crypto 中重现 SubtleCrypto 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68282852/
所以我正在尝试实现一些方法来加密然后解密一些数据。我在这方面没有任何经验,我尝试按照一些在线帖子了解如何解决这个问题。 当我将加密的“hello”传递给解密函数时,我得到了这个: let a = im
所以我正在尝试实现一些方法来加密然后解密一些数据。我在这方面没有任何经验,我尝试按照一些在线帖子了解如何解决这个问题。 当我将加密的“hello”传递给解密函数时,我得到了这个: let a = im
我想调用 SubtleCrypto 的方法从一个 worker 。 通常,可以通过窗口上下文中可用的 Crypto.subtle 属性: 例如:window.crypto.subtle.encrypt
我学习一整天the documentation of SubtleCrypto在 Mozilla 工作,无法找到一种使用一个版本的 RSA key 同时进行加密和签名的方法。 我很困惑,因为我能够写
我正在尝试使用 SubtleCrypto.unwrapkey 解开 key ...我的解开算法是: {name: 'RSA-OAEP', hash: Object{name: 'SHA-1'}, mo
我注意到以下 example (last one)在 MDN 上,这让我相信有一种方法可以将 SubtleCrypto 函数的结果分配给变量。但据我所知/研究过 async/await 只能在 asy
我一直在寻找用于加密/解密文本消息的 SublteCrypto,并想提取用于字符串的 key ,但使用相同的接口(interface)“SubtleCrypto”会为 key 生成奇怪的字符串。 现在
所以是的,我的老板想将加密放入他的系统,他希望消息以 js 签名并在 php 中验证。目前,我正在使用 mozilla 的 tinycrypto api 生成 RSA-PSS key 并签名和 php
我有一些使用 SubtleCrypto 的代码来加密 key 并将其存储在数据库中。 对于另一个功能,我必须能够在 Node 12 中对其进行加密。如果没有 SubtleCrypto,我必须在 Cry
我正在尝试使用 Ionic 中的 SubtleCrypto Web API 来使用公钥加密数据。我以 PEM 格式导入 key ,然后将其传递给 window.crypto.subtle.import
我正在尝试让 SubtleCrypto 与 IE 11 一起工作。具体来说,我只是想简单地加密一些东西,让我开始,我已经能够为 AES-CBC 生成一个 key ,但是当我尝试进行加密时出现错误:“类
经过docs之后我找不到使用 WithMD5AndMGF1Padding 使用 RSA-OAEP 加密数据的方法。所以基本上我不知道他们使用什么填充 最佳答案 对于 MGF1,默认值通常是 SHA-1
我正在尝试使用 Web Crypto API 的 SubtleCrypto 接口(interface)验证 JWT 的签名。 我的代码不会验证 token 签名,而 JWT.io 的调试工具会,我不知
这真的很尴尬 几乎在互联网上的任何网站上, window.crypto.subtle 返回 SubtleCrypto {} __proto__: SubtleCrypto 在 chrome 控制台
根据doc使用 PBKDF2 派生密码的简单示例是 return window.crypto.subtle.importKey( 'raw', encoder.encode(pas
我正在使用安全嵌入式芯片 (ATECC508) 使用 secp256r1 曲线生成 ECDSA 消息/签名组合。 Uint8Arrays中芯片输出的信息:message[32]、signature[6
在服务器端我使用PyCryptodome使用 RSA-OAEP(使用 SHA-256)加密消息。 我正在尝试使用 SubtleCrypto 解密消息客户端的 Web Crypto API,但它给了我一
我已经尝试破解这个问题有一段时间了,但没有成功。 服务器端解密使用RSACryptoServiceProvider RSA-OAEP。我无法改变这一点 public void SetEncrypted
我是一名优秀的程序员,十分优秀!