The Go-Ethereum repository allows for Ethereum with the Geth command. Overall it uses the ECIES method to support the encrypting of data the public key of a user to then generate a symmetric key to encrypt and decrypt the document.
ECIES in Go-Ethereum |
Coding
We can use the Kryptology library developed by Coinbase to implement ECIES:
package main // Based on example here: https://stackoverflow.com/questions/51111605/how-do-i-recover-ecdsa-public-key-correctly-from-hashed-mymsg-and-signature-in import ( "crypto/elliptic" "crypto/rand" "fmt" "os" "strings" "github.com/ethereum/go-ethereum/crypto/ecies" ) func getCurve(s string) elliptic.Curve { if strings.Contains(s, "256") { return elliptic.P256() } else if strings.Contains(s, "521") { return elliptic.P521() } else if strings.Contains(s, "384") { return elliptic.P384() } else if strings.Contains(s, "224") { return elliptic.P224() } return elliptic.P256() } func main() { argCount := len(os.Args[1:]) msg := "hello" curveType := "P256" if argCount > 0 { msg = os.Args[1] } if argCount > 1 { curveType = os.Args[2] } fmt.Printf("=== ECIES Go-Ethereum ===\n") fmt.Printf("Plaintext: %s\n", msg) mymsg := []byte(msg) curve := getCurve(curveType) fmt.Printf("Curve: %s\n", curve.Params().Name) bob_priv, _ := ecies.GenerateKey(rand.Reader, curve, nil) fmt.Printf("Alice private key: %x\n", bob_priv.D.Bytes()) alice_priv, _ := ecies.GenerateKey(rand.Reader, curve, nil) fmt.Printf("Bob private key: %x\n", alice_priv.D.Bytes()) ct, _ := ecies.Encrypt(rand.Reader, &alice_priv.PublicKey, mymsg, nil, nil) pt, _ := alice_priv.Decrypt(ct, nil, nil) fmt.Printf("Ciphertext: %x\n", ct) fmt.Printf("Decrypted: %s\n", pt) }
A sample run:
=== ECIES Go-Ethereum === Plaintext: hello555 Curve: P-521 Alice private key: 01869ccb7be48418be4ccb1840ce9eb25ebe4f021b176e0bd072cf4fb389e9e46ceea97937c819a879eb94108e67ab5514823e027ae8e2902492ecd41fb2585874fe Bob private key: 01375dddc4aa312c47ea2ca57026956e32dd307f3e3ac2dc7049eac72e6d56122fc29907c4745f5406079794cf5901f1f28bfaafe4af213164ae6b475c1c7fc84ebd