Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 本机加密抛出DomeException_Javascript_Node.js_Encryption_Cryptography - Fatal编程技术网

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
    只包含
    [“sign”]
    ,而不包含encrypion

让我们从异常开始……由于某些原因,您没有看到错误消息

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