NodeJS,ECC,我是否正确地将此 PrivateKey 存储到 JSON 文件中?

问题描述

所以我使用 elliptic 库使用 secp256k 私钥使用椭圆曲线方案进行加密。我的应用程序要求我将私钥存储到一个文件中,我选择了 json。但是,我不确定椭圆库是否使用带有私钥的二进制数据,我认为 JSON 文件不适用于二进制数据。尽管如此,我在生成公钥的函数中放入什么密钥并不重要,所以我什至不知道我从文件获取的密钥是否有效。

这是我将密钥读取和写入文件的方式:

index.js

const secret = () =>
{
    console.log('secret function ran');
    const rawSkey = fs.readFileSync(skeyPath);
    
    const skey = JSON.parse(rawSkey);
    
    const strSkey = JSON.stringify(rawSkey);
    
    console.log(strSkey);
    
    var secret;
    
    if(skey.start === null)
    {
        secret = null;
        console.log('skey is null');
        return secret;
    }
    else
    {
        console.log('skey is not null');
        secret = skey;//Buffer.from(skey);
        return secret;
    }
};
const wallet = new Wallet(secret);

wallet.js

class Wallet {
  constructor(secret) {
      //ADDED SECRET ParaMETER
    this.secret = secret;
    
    this.balance = STARTING_BALANCE;

    if(this.secret === null || this.secret === undefined)
    {
        console.log("secret key is null");
        this.keyPair = ec.genKeyPair();
        
        fs.writeFileSync(skeyPath,JSON.stringify(this.keyPair));
    
        this.publicKey = this.keyPair.getPublic().encode('hex');
    }
    else
    {
        console.log("secret key is defined");
        this.keyPair = ec.keyFromPrivate(this.secret);
        
        //fs.writeFileSync('../secret.json');
        this.publicKey = this.keyPair.getPublic().encode('hex');
    }

    
  }
//etc etc etc etc etc code continues

这是否会正确存储原始私钥,还是我犯了一个与加密安全性背道而驰的严重错误

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)