Skip to content

Instantly share code, notes, and snippets.

@lmars
Created January 3, 2019 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lmars/f92c3fdad2620903388656e8726602fd to your computer and use it in GitHub Desktop.
Save lmars/f92c3fdad2620903388656e8726602fd to your computer and use it in GitHub Desktop.
go-ethereum / ecies-parity example
package main
import (
"crypto/rand"
"fmt"
"log"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/crypto/ecies"
)
var sharedMACData = []byte{0, 0}
func main() {
key, err := crypto.HexToECDSA("54422e4b3278da00c4cc14b31cbbd1a8e047927735be2d1752ac134b7d9a2fef")
if err != nil {
log.Fatal(err)
}
data, err := ecies.Encrypt(
rand.Reader,
ecies.ImportECDSAPublic(&key.PublicKey),
[]byte("hello"),
nil,
sharedMACData,
)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%x\n", data)
}
$ go run encrypt.go
0403c6817ff2bab5755ea80f809ee0c7da8556d87066f6ba27b910bce190f79c7f75fa0a3d9ef241a02ac0f362910c8f68852f8ede1fa705729c0d237c2a1f5b10cf88aa1b489c0e811f214b89c4b5e0b1baaddfa05acc07dbea813f3f475ad46dc162bc4a2bc2fd6941dae99fc259d3446c881611ce
var privateKey = Buffer.from(
'54422e4b3278da00c4cc14b31cbbd1a8e047927735be2d1752ac134b7d9a2fef',
'hex'
);
var encrypted = Buffer.from(
'0403c6817ff2bab5755ea80f809ee0c7da8556d87066f6ba27b910bce190f79c7f75fa0a3d9ef241a02ac0f362910c8f68852f8ede1fa705729c0d237c2a1f5b10cf88aa1b489c0e811f214b89c4b5e0b1baaddfa05acc07dbea813f3f475ad46dc162bc4a2bc2fd6941dae99fc259d3446c881611ce',
'hex'
);
require("ecies-parity").decrypt(privateKey, encrypted).then(function(plaintext) {
console.log("Decrypted:", plaintext.toString());
});
$ node decrypt.js
Decrypted: hello
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment