douliang1369 2018-07-11 07:23
浏览 90

Golang Nacl Secretbox中的Bip32密钥加密

I have created the Assymetric struct which generates a BIP39 and BIP32 Master public and private key. Now I want to use these keys and their derived children to encrypt files or strings depending upon the use case. Please correct me if I am wrong — the keys generated from Mnemonics BIP32 are Elliptic curve keys(secp256k1). I am trying to use the NACL Go implementation to encrypt text with these keys, but it only accepts keys which are 32 bytes but the keys that are being generated from Mnemonics are 256 Bytes. Please help. The bip32 implementation can be found here https://github.com/tyler-smith/go-bip32/blob/master/bip32.go#L59 After going through the implementation, I have found out that the resulting key is a combination of Key and chaincode, key.Key is a 32byte key that must be used for encryption. Just wanted to check if this is the right way to do it and guarantees security.

package encryption


import (
   "github.com/tyler-smith/go-bip39"
   "github.com/tyler-smith/go-bip32"
   "crypto/ecdsa"
   "crypto/elliptic"
   "golang.org/x/crypto/nacl/secretbox"
   "crypto/rand"
   "encoding/hex"
   "io"
   "log"
 )


type Encryption interface {
     GenerateMnemonic() ( *bip32.Key, *bip32.Key)
}

type Assymetric struct{
   RootPrivateKey *bip32.Key
   RootPublicKeyKey *bip32.Key
   RootMnemonic string
   RootPassphrase string
}

func (c *Assymetric) GenerateMnemonic() (*bip32.Key, *bip32.Key){

  entropy, _ := bip39.NewEntropy(256)
  mnemonic, _ := bip39.NewMnemonic(entropy)

  seed := bip39.NewSeed(mnemonic, c.RootPassphrase)

  rootPrivateKey, _ := bip32.NewMasterKey(seed)
  rootPublicKey := rootPrivateKey.PublicKey()

  // Display mnemonic and keys
  //c.RootMnemonic = mnemonic
  c.RootPrivateKey = rootPrivateKey
  c.RootPublicKeyKey = rootPublicKey


  key, _ := rootPrivateKey.NewChildKey(0)
  log.Printf("Private key 0 is %s", key)
  log.Printf("Public key 0 is %s", key.PublicKey())


    // You must use a different nonce for each message you encrypt with the
    // same key. Since the nonce here is 192 bits long, a random value
    // provides a sufficiently small probability of repeats.

  log.Printf("This is the secret key %s", secretKey)

  var nonce [24]byte
    if _, err := io.ReadFull(rand.Reader, nonce[:]); err != nil {
        panic(err)
    }

  log.Printf("This is the nonce %s", nonce)

   var a [32]byte
  copy(a[:], key.Key)

  log.Printf("
len=%d cap=%d %v
", len(a), cap(a), a)


  encrypted := secretbox.Seal(nonce[:], []byte("hello world"), &nonce, &a)

  log.Println(encrypted)
  return rootPrivateKey, rootPublicKey
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 武汉岩海低应变分析软件,导数据库里不显示波形图
    • ¥15 CreateBitmapFromWicBitmap内存释放问题。
    • ¥30 win c++ socket
    • ¥30 CanMv K210开发板实现功能
    • ¥15 C# datagridview 栏位进度
    • ¥15 vue3页面el-table页面数据过多
    • ¥100 vue3中融入gRPC-web
    • ¥15 kali环境运行volatility分析android内存文件,缺profile
    • ¥15 写uniapp时遇到的问题
    • ¥15 vs 2008 安装遇到问题