简体   繁体   English

Javascript。 ECIES方案如何有效存储Secp256k1私钥

[英]Javascript. How To Efficiently Store Secp256k1 Private Key for ECIES Scheme

I've been having a really hard time figuring out how to store a Secp256k1 privateKey from multiple libraries (currently on this one for ECIES encryption: https://npm.io/package/@toruslabs/eccrypto ).我一直很难弄清楚如何从多个库中存储一个 Secp256k1 privateKey(目前在这个库上用于 ECIES 加密: https://npm.io/package/@toruslabs/eccrypto )。

I have tried encoding and decoding with base64, many implementations of functions that copy array buffer for input encoded string to localStoarge and corresponding output Uint8Array from localStorage, I tried it with IndexedDB, JSON.stringify and parse do not work with binary data, and so many more variations. I have tried encoding and decoding with base64, many implementations of functions that copy array buffer for input encoded string to localStoarge and corresponding output Uint8Array from localStorage, I tried it with IndexedDB, JSON.stringify and parse do not work with binary data, and so更多的变化。

When I go through the array buffer elements individually to copy it into a new Uint8Array, I get a similar private key, but with two missing key/field's (parent and offset) which I believe is why every library I have tried so far returns something a long the lines of "bad private key" when I try generating the public key from them.当我通过数组缓冲区元素 go 分别将其复制到新的 Uint8Array 中时,我得到了一个类似的私钥,但是缺少两个键/字段(父级和偏移量),我相信这就是为什么我到目前为止尝试过的每个库都会返回一些东西当我尝试从它们生成公钥时,会出现很长的“坏私钥”行。

I am exhausted and I would like some professional insight for my lack of skill in this particular subject.我已经筋疲力尽了,我想要一些专业的见解,因为我在这个特定的主题上缺乏技能。 So how can I store (in any way as long as it's client/local) a Secp256k1 private key in a way that if I call it from that persistent client sided data base, they can be used to generate the public keys?那么如何存储(只要它是客户端/本地的)Secp256k1 私钥,如果我从该持久客户端数据库调用它,它们可以用于生成公钥?

Apparently, the library that uses the private/public key (in this case being @toruslabs/eccrypto ) requires a buffer parameter for the keys.显然,使用私钥/公钥的库(在本例中为@toruslabs/eccrypto )需要密钥的缓冲区参数。

A simple solution would be to make the NodeJS Buffer available in the browser, through browserify.一个简单的解决方案是通过 browserify 使NodeJS 缓冲区在浏览器中可用。 You will only need to include the NodeJS Buffer class to the window object when creating the browserify file, as shown:创建 browserify 文件时,您只需要将 NodeJS 缓冲区 class 包含到 window object 中,如下所示:

const eccrypto = require('./index');
window.eccrypto = eccrypto;
window.Buffer = Buffer;

Then, generate the bundle file using browserify: browserify main.js -o bundle.js然后,使用 browserify 生成包文件: browserify main.js -o bundle.js

After this, you will be able to use the Buffer class in your browser, which will make loading the private/public key possible.在此之后,您将能够在浏览器中使用缓冲区 class,这将使加载私钥/公钥成为可能。 Sample code here:示例代码在这里:

<script src="bundle.js"></script>
<script>
  const eccrypto = window.eccrypto;

  const privateKey = eccrypto.generatePrivate();
  const publicKey = eccrypto.getPublic(privateKey);

  // hex string output of private key
  const hexPrivateKey = privateKey.toString('hex')
  console.log(hexPrivateKey); // we can do this as privateKey is a Buffer

  // load private key again
  const newPrivateKey = Buffer.from(hexPrivateKey, 'hex');
 
  const enc = new TextEncoder();

  // code referenced from @toruslabs/eccrypto README
  // Encrypting the message.
  eccrypto.encrypt(publicKey, enc.encode("my testing msg")).then(function (encrypted) {
    // Decrypting the message.
    eccrypto.decrypt(newPrivateKey, encrypted).then(function (plaintext) {
      console.log("Message:", plaintext.toString());
    });
  });
</script>

This should be sufficient to store the hex string of the private key in the localStorage or any client-side database/storage that you will be using.这应该足以将私钥的十六进制字符串存储在 localStorage 或您将使用的任何客户端数据库/存储中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM