我使用的是native-crypto包,它是跨平台密码学(so和node.js)的API。
let crypto = require("native-crypto");我创造了一个公开的私钥对.
keyPair = crypto.generate('P-256');...and我正试图加密这样的消息:
let message = "Hello, World!";
let encrypted = crypto.rsa.encrypt(keyPair.privateKey, message);但是,这是不起作用的,我正在接收一个DOMException (在浏览器环境中),没有进一步的细节。
我该怎么解决这个问题?
可能的问题:
key_ops只包含["sign"],而没有加密。发布于 2020-03-24 03:32:18
让我们从exception...for开始,因为您没有看到错误消息。
Uncaught (in promise) DOMException: The required JWK member "kty" was missing异步与同步
第一个错误是因为您试图同步使用异步API。
您需要在await生成行中添加keyPair关键字:
keyPair = await crypto.generate('P-256');如果没有await关键字,承诺将分配给keyPair,而不是包含kty的对象。
错键类型
一旦修复了这个问题,您将看到另一个错误:
The JWK "kty" member was not "RSA"这是因为ECDSA密钥与RSA加密一起使用。
还有一个问题
一旦修复了这个问题,您就会看到另一个错误
The JWK "key_ops" member was inconsistent with that specified by the Web Crypto call. The JWK usage must be a superset of those requested我帮不上这个忙。我怀疑这是native-crypto的一个问题。您可能必须在他们的github回购上提交错误报告。下面是一个大致等价的示例,它只使用Web。
const crypto = window.crypto.subtle;
async function main() {
const keyPair = await crypto.generateKey(
{
name: "RSA-OAEP",
modulusLength: 4096,
publicExponent: new Uint8Array([1, 0, 1]),
hash: "SHA-256"
},
true,
["encrypt", "decrypt"]
);
let message = "hello";
message = new TextEncoder().encode(message);
const encrypted = await crypto.encrypt({ name: "RSA-OAEP" }, keyPair.publicKey, message);
console.log(encrypted);
}
main()https://stackoverflow.com/questions/60779353
复制相似问题