goEncrypt_master

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 15, 2023 License: MIT Imports: 19 Imported by: 0

README

GoECC

golang语言的ecc实现。
基于仓库:goEncrypt

PEM密钥使用指南

PEM密钥是类似于如下的密钥:

-----BEGIN EC PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE7JMlqc5lUMKLSS8U5+NANi7qjewn
FhPsZT67kzVBgdxZGnPjbF3+2n8F8lxl/+sckTDyXTnevT6Q6iBBJyHkvg==
-----END EC PUBLIC KEY-----

-----BEGIN EC PRIVATE KEY-----
MHcCAQEEIBjEsBOdIT5m5eJ9jIQ9lrUBCsXLMTC5QgmdDXKsV1oMoAoGCCqGSM49
AwEHoUQDQgAE7JMlqc5lUMKLSS8U5+NANi7qjewnFhPsZT67kzVBgdxZGnPjbF3+
2n8F8lxl/+sckTDyXTnevT6Q6iBBJyHkvg==
-----END EC PRIVATE KEY-----

使用PEM密钥加密:

func cryptPem() {
	msg := "ABCDEFG"
	eccKey, err := ecc.GenerateEccKeyPem()
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Printf("Pri: %s\n", eccKey.PrivateKey)
	fmt.Printf("Pub: %s\n", eccKey.PublicKey)

	text, err := ecc.EccEncrypt([]byte(msg), eccKey.PublicKey)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Printf("Secret:%s\n", text)

	plaintext, err := ecc.EccDecrypt(text, eccKey.PrivateKey)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Printf("Plain:%s\n", string(plaintext))
}

GenerateEccKeyPem生成密钥。 EccEncrypt加密字节,返回字节。 EccDecrypt解密字节。

使用PEM签名:

func signPem() {
	msg := "ABCDEFG"
	eccKey, err := ecc.GenerateEccKeyPem()
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Printf("Pri: %s\n", eccKey.PrivateKey)
	fmt.Printf("Pub: %s\n", eccKey.PublicKey)

	rSign, sSign, err := ecc.EccSign([]byte(msg), eccKey.PrivateKey)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Printf("R_Sign: %s\nS_Sign: %s\n", rSign, sSign)
	res := ecc.EccVerifySign([]byte(msg), rSign, sSign, eccKey.PublicKey)
	fmt.Printf("Sign: %v\n", res)
}

EccSign生成签名的rSignsSignEccSign验证签名的(传入文件字节)。

HEX密钥使用指南

类似于PEM密钥,函数带有Hex后缀。

Base64密钥使用指南

类似于PEM密钥,函数带有Base64后缀。

测试程序

package main

import (
	"fmt"
	ecc "github.com/SuperH-0630/Goecc"
)

func main() {
	fmt.Printf("CryptPem:\n")
	cryptPem()

	fmt.Printf("CryptBase64:\n")
	cryptBase64()

	fmt.Printf("CryptHex:\n")
	cryptHex()

	fmt.Printf("SignPem:\n")
	signPem()

	fmt.Printf("SignBase64:\n")
	signBase64()

	fmt.Printf("SignHex:\n")
	signHex()
}

func cryptPem() {
	msg := "ABCDEFG"
	eccKey, err := ecc.GenerateEccKeyPem()
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Printf("Pri: %s\n", eccKey.PrivateKey)
	fmt.Printf("Pub: %s\n", eccKey.PublicKey)

	text, err := ecc.EccEncrypt([]byte(msg), eccKey.PublicKey)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Printf("Secret:%s\n", text)

	plaintext, err := ecc.EccDecrypt(text, eccKey.PrivateKey)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Printf("Plain:%s\n", string(plaintext))
}

func cryptBase64() {
	msg := "ABCDEFG"
	eccBase64Key, err := ecc.GenerateEccKeyBase64()
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Printf("Pri: %s\n", eccBase64Key.PrivateKey)
	fmt.Printf("Pub: %s\n", eccBase64Key.PublicKey)

	text, err := ecc.EccEncryptToBase64([]byte(msg), eccBase64Key.PublicKey)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Printf("Secret:%s\n", text)

	plaintext, err := ecc.EccDecryptByBase64(text, eccBase64Key.PrivateKey)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Printf("Plain:%s\n", string(plaintext))
}

func cryptHex() {
	msg := "ABCDEFG"
	eccHexKey, err := ecc.GenerateEccKeyHex()
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Printf("Pri: %s\n", eccHexKey.PrivateKey)
	fmt.Printf("Pub: %s\n", eccHexKey.PublicKey)

	text, err := ecc.EccEncryptToHex([]byte(msg), eccHexKey.PublicKey)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Printf("Secret:%s\n", text)

	plaintext, err := ecc.EccDecryptByHex(text, eccHexKey.PrivateKey)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Printf("Plain:%s\n", string(plaintext))
}

func signPem() {
	msg := "ABCDEFG"
	eccKey, err := ecc.GenerateEccKeyPem()
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Printf("Pri: %s\n", eccKey.PrivateKey)
	fmt.Printf("Pub: %s\n", eccKey.PublicKey)

	rSign, sSign, err := ecc.EccSign([]byte(msg), eccKey.PrivateKey)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Printf("R_Sign: %s\nS_Sign: %s\n", rSign, sSign)
	res := ecc.EccVerifySign([]byte(msg), rSign, sSign, eccKey.PublicKey)
	fmt.Printf("Sign: %v\n", res)
}

func signBase64() {
	msg := "ABCDEFG"
	eccBase64Key, err := ecc.GenerateEccKeyBase64()
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Printf("Pri: %s\n", eccBase64Key.PrivateKey)
	fmt.Printf("Pub: %s\n", eccBase64Key.PublicKey)

	rSign, sSign, err := ecc.EccSignBase64([]byte(msg), eccBase64Key.PrivateKey)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Printf("R_Sign: %s\nS_Sign: %s\n", rSign, sSign)
	res := ecc.EccVerifySignBase64([]byte(msg), rSign, sSign, eccBase64Key.PublicKey)
	fmt.Printf("Sign:%v\n", res)
}

func signHex() {
	msg := "ABCDEFG"
	eccHexKey, err := ecc.GenerateEccKeyHex()
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Printf("Pri: %s\n", eccHexKey.PrivateKey)
	fmt.Printf("Pub: %s\n", eccHexKey.PublicKey)

	rSign, sSign, err := ecc.EccSignHex([]byte(msg), eccHexKey.PrivateKey)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Printf("R_Sign: %s\nS_Sign: %s\n", rSign, sSign)
	res := ecc.EccVerifySignHex([]byte(msg), rSign, sSign, eccHexKey.PublicKey)
	fmt.Printf("Sign: %v\n", res)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidCurve               = fmt.Errorf("ecies: invalid elliptic curve")
	ErrInvalidPublicKey           = fmt.Errorf("ecies: invalid public key")
	ErrSharedKeyIsPointAtInfinity = fmt.Errorf("ecies: shared key is point at infinity")
	ErrSharedKeyTooBig            = fmt.Errorf("ecies: shared key params are too big")
	ErrUnsupportedECIESParameters = fmt.Errorf("ecies: unsupported ECIES parameters")
)
View Source
var (
	ErrKeyDataTooLong = fmt.Errorf("ecies: can't supply requested key data")
	ErrInvalidMessage = fmt.Errorf("ecies: invalid message")
)
View Source
var (
	ECIESAes128Sha256 = &ECIESParams{
		Hash:      sha256.New,
		hashAlgo:  crypto.SHA256,
		Cipher:    aes.NewCipher,
		BlockSize: aes.BlockSize,
		KeyLen:    16,
	}

	ECIESAes256Sha384 = &ECIESParams{
		Hash:      sha512.New384,
		hashAlgo:  crypto.SHA384,
		Cipher:    aes.NewCipher,
		BlockSize: aes.BlockSize,
		KeyLen:    32,
	}

	ECIESAes256Sha512 = &ECIESParams{
		Hash:      sha512.New,
		hashAlgo:  crypto.SHA512,
		Cipher:    aes.NewCipher,
		BlockSize: aes.BlockSize,
		KeyLen:    32,
	}
)

Functions

func EccDecrypt

func EccDecrypt(cipherText []byte, priKey string) (plainText []byte, err error)

func EccDecryptByBase64

func EccDecryptByBase64(base64CipherText, base64PriKey string) (plainText []byte, err error)

func EccDecryptByHex

func EccDecryptByHex(hexCipherText, hexPriKey string) (plainText []byte, err error)

func EccEncrypt

func EccEncrypt(plainText []byte, pubKey string) (cipherText []byte, err error)

func EccEncryptToBase64

func EccEncryptToBase64(plainText []byte, base64PubKey string) (base64CipherText string, err error)

func EccEncryptToHex

func EccEncryptToHex(plainText []byte, hexPubKey string) (hexCipherText string, err error)

func EccSign

func EccSign(msg []byte, priKey string) (hexrSign, hexsSign string, err error)

func EccSignBase64

func EccSignBase64(msg []byte, base64PriKey string) (base64rSign, base64sSign string, err error)

func EccSignHex

func EccSignHex(msg []byte, hexPriKey string) (hexrSign, hexsSign string, err error)

func EccVerifySign

func EccVerifySign(msg []byte, hexrSign, hexsSign, pubKey string) bool

func EccVerifySignBase64

func EccVerifySignBase64(msg []byte, base64rSign, base64sSign, base64PubKey string) bool

func EccVerifySignHex

func EccVerifySignHex(msg []byte, hexrSign, hexsSign, hexPubKey string) bool

func Encrypt

func Encrypt(rand io.Reader, pub *PublicKey, m, s1, s2 []byte) (ct []byte, err error)

Encrypt encrypts a message using ECIES as specified in SEC 1, 5.1.

s1 and s2 contain shared information that is not part of the resulting ciphertext. s1 is fed into key derivation, s2 is fed into the MAC. If the shared information parameters aren't being used, they should be nil.

func MaxSharedKeyLength

func MaxSharedKeyLength(pub *PublicKey) int

MaxSharedKeyLength returns the maximum length of the shared key the public key can produce.

Types

type ECIESParams

type ECIESParams struct {
	Hash func() hash.Hash // hash function

	Cipher    func([]byte) (cipher.Block, error) // symmetric cipher
	BlockSize int                                // block size of symmetric cipher
	KeyLen    int                                // length of symmetric key
	// contains filtered or unexported fields
}

func ParamsFromCurve

func ParamsFromCurve(curve elliptic.Curve) (params *ECIESParams)

以下为从以太坊源码/crypt/ecies/params.go中截取过来

type EccKey

type EccKey struct {
	PrivateKey string
	PublicKey  string
}

func GenerateEccKeyBase64

func GenerateEccKeyBase64() (EccKey, error)

func GenerateEccKeyHex

func GenerateEccKeyHex() (EccKey, error)

func GenerateEccKeyPem

func GenerateEccKeyPem() (EccKey, error)

type PrivateKey

type PrivateKey struct {
	PublicKey
	D *big.Int
}

PrivateKey is a representation of an elliptic curve private key.

func GenerateKey

func GenerateKey(rand io.Reader, curve elliptic.Curve, params *ECIESParams) (prv *PrivateKey, err error)

Generate an elliptic curve public / private keypair. If params is nil, the recommended default parameters for the key will be chosen.

func ImportECDSA

func ImportECDSA(prv *ecdsa.PrivateKey) *PrivateKey

Import an ECDSA private key as an ECIES private key.

func (*PrivateKey) Decrypt

func (prv *PrivateKey) Decrypt(c, s1, s2 []byte) (m []byte, err error)

Decrypt decrypts an ECIES ciphertext.

func (*PrivateKey) ExportECDSA

func (prv *PrivateKey) ExportECDSA() *ecdsa.PrivateKey

Export an ECIES private key as an ECDSA private key.

func (*PrivateKey) GenerateShared

func (prv *PrivateKey) GenerateShared(pub *PublicKey, skLen, macLen int) (sk []byte, err error)

ECDH key agreement method used to establish secret keys for encryption.

type PublicKey

type PublicKey struct {
	X *big.Int
	Y *big.Int
	elliptic.Curve
	Params *ECIESParams
}

PublicKey is a representation of an elliptic curve public key.

func ImportECDSAPublic

func ImportECDSAPublic(pub *ecdsa.PublicKey) *PublicKey

Import an ECDSA public key as an ECIES public key.

func (*PublicKey) ExportECDSA

func (pub *PublicKey) ExportECDSA() *ecdsa.PublicKey

Export an ECIES public key as an ECDSA public key.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL