如何使用种子确定性地生成安全的RSA键?

发布于 2025-01-24 19:25:41 字数 455 浏览 2 评论 0 原文

我们如何使用单词的 mnemonic列表作为种子(就像我们已经习惯了使用加密货币钱包的习惯)能够恢复私钥,以防私钥丢失,不小心擦除或粘在破损的设备?

这对于客户端之间的E2E加密可能很有用:应在客户端上生成密钥,只有公共密钥才能与服务器共享。

用户可以在需要时再生键,只要他们能够再次提供助记符,显然可以安全地存储和离线。

疯子种子应足够长以提供安全量的熵。

some q& a似乎已经过时了:我们如何在JavaScript/Typescript(可能使用维护的库)中实现这一目标?

How do we use a mnemonic list of words as a seed (like we have been become accustomed using cryptocurrency wallets) to be able to recover a private key in case it gets lost, accidentally erased, or stuck on a broken device?

This could be useful for e2e encryption between clients: the keys are supposed to be generated on the client, only the public key will be shared with the server.

Users could be regenerating the keys offline when needed, as long as they are able to provide the mnemonic again, obviously to be stored safely and offline.

The mnemonic seed should be long enough to provide a safe amount of entropy.

Some Q&A appear to be very outdated: how can we achieve this in Javascript/Typescript, possibly using maintained libraries?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

铁憨憨 2025-01-31 19:25:41

解决方案: bip39 node-forge

quote 此答案引导我实现了此解决方案:

在这种情况下,由此产生的公共密钥本质上是公开的,并且
因此可以用于离线词典攻击。攻击者只是
尝试可能的密码,直到找到相同的公钥为止。就是这样
您想要实现的固有。

我们可以假设 128位熵应该足够在可预见的将来,我们可以预防这种攻击,但是我们可以幸运的是,决定我们的助记符的强度。

1。

首先,我们可以使用 bip-39 bip-39 JS实现。

import { generateMnemonic } from "bip39";

const mnemonic = generateMnemonic(256) // 256 to be on the _really safe_ side. Default is 128 bit.

console.log(mnemonic) // prints 24 words

2。创建确定性prng函数

现在我们可以使用 node-forge 生成我们的钥匙。
pki.rsa.generateKeypair 函数接受a 确定性),而是从mnemonic 中返回计算的值

import { mnemonicToSeed } from "bip39";
import { pki, random } from "node-forge";

const seed = (await mnemonicToSeed(mnemonic)).toString('hex')

const prng = random.createInstance();
prng.seedFileSync = () => seed

3。生成键盘,

馈送 GenerateKeyPair 函数,

const { privateKey, publicKey } = pki.rsa.generateKeyPair({ bits: 4096, prng, workers: 2 })

我们现在可以使用我们的“ aidged” prng: etvoilà!

我们现在拥有直接在客户端和客户端生成的安全和确定性的RSA键与输入相同的助记符可靠。
请考虑使用确定性密钥涉及的风险,并确保您的用户将不要在线或其他任何地方存储助记符(通常建议将其写下来并将其存储在纸上并将其存储在安全的地方) 。

Solution: bip39 and node-forge

To quote this answer which has guided me to achieve this solution:

in this scenario, the resulting public key is, by nature, public, and
thus can serve for offline dictionary attacks. The attacker just has
to try possible passwords until he finds the same public key. That's
intrinsic to what you want to achieve.

We can assume that 128 bits of entropy should be enough for preventing this kind of attacks, in the foreseeable future, however we can fortunately decide how strong our mnemonic will be.

1. Generate mnemonic

First of all we can generate a mnemonic using the bip-39 JS implementation.

import { generateMnemonic } from "bip39";

const mnemonic = generateMnemonic(256) // 256 to be on the _really safe_ side. Default is 128 bit.

console.log(mnemonic) // prints 24 words

2. Create deterministic PRNG function

Now we can use node-forge to generate our keys.
The pki.rsa.generateKeyPair function accepts a pseudo-random number generator function in input. The goal is getting this function to NOT compute a pseudo-random number (this would not be deterministic anymore), but rather return a value computed from the mnemonic.

import { mnemonicToSeed } from "bip39";
import { pki, random } from "node-forge";

const seed = (await mnemonicToSeed(mnemonic)).toString('hex')

const prng = random.createInstance();
prng.seedFileSync = () => seed

3. Generating keypair

We can now feed the generateKeyPair function with our "rigged" prng:

const { privateKey, publicKey } = pki.rsa.generateKeyPair({ bits: 4096, prng, workers: 2 })

Et voilà!

We now have safe and deterministic RSA keys, directly generated on the client and restorable with the same mnemonic as a input.
Please consider the risks involved using deterministic keys and make sure your users will NOT store the mnemonic online or anywhere else on their client (generally, it is suggested to write it down on paper and store it somewhere safe).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文