Javascript 本机加密抛出DomeException
我使用的是Javascript 本机加密抛出DomeException,javascript,node.js,encryption,cryptography,Javascript,Node.js,Encryption,Cryptography,我使用的是nativecrypto包,这是一个跨平台加密的API(soweb和node.js) 我已生成公钥/私钥对 keyPair = crypto.generate('P-256'); …我正试图加密这样一条消息: let message = "Hello, World!"; let encrypted = crypto.rsa.encrypt(keyPair.privateKey, message); 但是,这不起作用,我收到一个DOMException(在浏览器环境中),没有进一步的
nativecrypto
包,这是一个跨平台加密的API(soweb和node.js)
我已生成公钥/私钥对
keyPair = crypto.generate('P-256');
…我正试图加密这样一条消息:
let message = "Hello, World!";
let encrypted = crypto.rsa.encrypt(keyPair.privateKey, message);
但是,这不起作用,我收到一个DOMException
(在浏览器环境中),没有进一步的细节
我如何解决这个问题
可能的问题:
- 可能我使用的函数组合不正确
- 这可能是因为我生成的密钥的
只包含密钥\u ops
,而不包含encrypion[“sign”]
Uncaught (in promise) DOMException: The required JWK member "kty" was missing
异步与同步
第一个错误是因为您试图同步使用异步API
您需要在keyPair
生成行中添加wait
关键字:
keyPair = await crypto.generate('P-256');
如果没有wait
关键字,则会将承诺分配给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
我帮不了你。我怀疑这是本机加密的问题。您可能需要在他们的计算机上提交错误报告。下面是一个仅使用Web Crypto API的大致相同的示例
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()
谢谢,我很快就会看的。糟糕的是,我有await
关键字,但忘了把它包括在问题中。@davidcalanan一切都好。你永远不知道。您正在使用哪个浏览器/为什么看不到附加到DomeException的错误消息?真有趣。。。3周后,我现在可以在我的浏览器中看到错误消息(从那以后我就再也没有接触过代码)@davidcalanan-wird!
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()