javascript - WebCrypto API 在哪里存储 key ?

标签 javascript cryptography webcrypto-api

我正在使用 webcrypto API 成功地加密服务器和客户端之间的消息(假设我需要手动执行此操作)。

我的问题是我需要检查用户和服务器的 key 对是否已经存在,而不是一直生成新的 key 对。有没有办法检查它是否存在并检索它以解密服务器消息?

澄清一下,我的 privateKey 在浏览器上,publicKey 被发送到服务器。

我有一个 nodejs 服务器和纯 JS 前端。

提前致谢。

最佳答案

默认情况下,加密 key 不是永久性的。您需要将键存储在 IndexedDB 中,以便下次浏览器执行时可以使用它们。

IndexedDB是一个安全存储, key 可以在不暴露 key Material 的情况下存储、恢复和使用

参见 https://www.w3.org/TR/WebCryptoAPI/#concepts-key-storage

5.2. Key Storage

This specification does not explicitly provide any new storage mechanisms for CryptoKey objects. Instead, by allowing the CryptoKey to be used with the structured clone algorithm, any existing or future web storage mechanisms that support storing structured clonable objects can be used to store CryptoKey objects.

In practice, it is expected that most authors will make use of the Indexed Database API, which allows associative storage of key/value pairs, where the key is some string identifier meaningful to the application, and the value is a CryptoKey object. This allows the storage and retrieval of key material, without ever exposing that key material to the application or the JavaScript environment

这里有一个完整的例子 https://blog.engelke.com/2014/09/19/saving-cryptographic-keys-in-the-browser/

关于javascript - WebCrypto API 在哪里存储 key ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49478240/

相关文章:

java - RSA 私钥仅支持 RSAPrivate (Crt) KeySpec 和 PKCS8EncodedKeySpec

android - Web Crypto API crypto.subtle 在 Apache Cordova 中未定义

javascript - 使用纯 JavaScript 签署 PDF

javascript - Web Crypto API - IndexedDB 中的不可精确 CryptoKey 是否足够安全,不会从一个设备传递到下一个设备?

javascript - 使用css调整图像大小并以纵横比将其复制到JS

javascript - 如果变量未定义,则将新变量设置为已定义的变量,而不使用 'if' 语句?

使用 wolfssl 进行证书完整性检查

PHP 和 Objective C Blowfish 加密编码不同

javascript - Android 4.0浏览器溢出: scroll and touch/click events

JavaScript 代码不工作