goencryption

package module
v0.0.0-...-27d1fd1 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2024 License: MIT Imports: 15 Imported by: 5

README

goencryption

中文说明

install

go get github.com/agclqq/goencryption

summary

Supports symmetric encryption and asymmetric encryption algorithms.

symmetric encryption
  • aes
  • des
  • 3des
asymmetric encryption
  • rsa

usage

symmetric encryption

generic methods

EasyEncrypt(easyType, plaintext, key, iv)
EasyDecrypt(easyType, ciphertext, key, iv)

The parameter easyType should have a format like this:cryptoType/mode/padding or cryptoType/mode/padding/transcode

Note: Do not use AES and PKCS5 together. The block size of AES is 16, and the complement length needs to be 16, while the length of PKCS5 is 8. For example:

EasyEncrypt("des/CFB/Pkcs7/Base64", plaintext, key, iv)
EasyDecrypt("des/CFB/Pkcs7/Base64", ciphertext, key, iv)

EasyEncrypt("aes/CTR/Pkcs7", plaintext, key, iv)
EasyDecrypt("aes/CTR/Pkcs7", ciphertext, key, iv)

EasyEncrypt("3des/ECB/Pkcs5/Hex", plaintext, key, iv)
EasyDecrypt("3des/ECB/Pkcs5/Hex", ciphertext, key, iv)

The core approach to implementation is Encrypt() and Decrypt(). All other methods are facade.

about padding
  • noPadding: The data must be must be an integer multiple of the block
  • zeorPadding: It's going to fill in with zeros. If the original data ends in 0, there is a problem.
  • pkcs5Padding: The complement length is 8, which can be problematic in some cases
  • pkcs7Padding: Pkcs5Padding superset.Complement length is dynamic.
asymmetric encryption

Generate both private and public keys

GenKeys(bits)

Only the private key is generated

GenPrvKey(bits)

Generate a public key from a private key

GenPubKeyFromPrvKey(prvKey)

Public key encryption private key decryption

PubKeyEncrypt(pubKey, plainText)
PrvKeyDecrypt(prvKey, cipherText)

Private key signature, public key verification signature

PrvKeySign(prvKey, plainText, hash)
PubKeyVerifySign(pubKey, plainText, sign, hash)

Documentation

Index

Constants

View Source
const (
	Aes cryptoType = iota
	Des
	TriDes
)
View Source
const (
	ECB mode = iota
	CBC
	CFB
	OFB
	CTR
)
View Source
const (
	No padding = iota
	Zero
	Pkcs5
	Pkcs7
)
View Source
const (
	Base64 transcode = iota
	Hex
)

Variables

This section is empty.

Functions

func AesCBCPkcs7Decrypt

func AesCBCPkcs7Decrypt(src, key, iv []byte) ([]byte, error)

func AesCBCPkcs7Encrypt

func AesCBCPkcs7Encrypt(data, key, iv []byte) ([]byte, error)

func AesCFBPkcs7Decrypt

func AesCFBPkcs7Decrypt(src, key, iv []byte) ([]byte, error)

func AesCFBPkcs7Encrypt

func AesCFBPkcs7Encrypt(data, key, iv []byte) ([]byte, error)

func AesCTRPkcs7Decrypt

func AesCTRPkcs7Decrypt(src, key, iv []byte) ([]byte, error)

func AesCTRPkcs7Encrypt

func AesCTRPkcs7Encrypt(data, key, iv []byte) ([]byte, error)

func AesECBPkcs7Decrypt

func AesECBPkcs7Decrypt(src, key []byte) ([]byte, error)

ECB 模式解密 src密文,key加密时的密钥

func AesECBPkcs7Encrypt

func AesECBPkcs7Encrypt(data, key []byte) ([]byte, error)

Ecb加密模式 data要加密的数据,key要加密的密钥

func AesOFBPkcs7Decrypt

func AesOFBPkcs7Decrypt(src, key, iv []byte) ([]byte, error)

func AesOFBPkcs7Encrypt

func AesOFBPkcs7Encrypt(data, key, iv []byte) ([]byte, error)

func Base64Decode

func Base64Decode(s string) ([]byte, error)

func Base64Encode

func Base64Encode(src []byte) string

func Decrypt

func Decrypt(multiple cryptoType, cipherText, key, iv []byte, mode mode, padding padding) ([]byte, error)

func DesCBCPkcs7Encrypt

func DesCBCPkcs7Encrypt(data, key, iv []byte) ([]byte, error)

func DesCFBPkcs7Decrypt

func DesCFBPkcs7Decrypt(src, key, iv []byte) ([]byte, error)

func DesCFBPkcs7Encrypt

func DesCFBPkcs7Encrypt(data, key, iv []byte) ([]byte, error)

func DesCTRPkcs7Decrypt

func DesCTRPkcs7Decrypt(src, key, iv []byte) ([]byte, error)

func DesCTRPkcs7Encrypt

func DesCTRPkcs7Encrypt(data, key, iv []byte) ([]byte, error)

func DesECBPkcs7Decrypt

func DesECBPkcs7Decrypt(src, key []byte) ([]byte, error)

ECB 模式解密 src密文,key加密时的密钥

func DesECBPkcs7Encrypt

func DesECBPkcs7Encrypt(data, key []byte) ([]byte, error)

Ecb加密模式 data要加密的数据,key要加密的密钥

func DesOFBPkcs7Decrypt

func DesOFBPkcs7Decrypt(src, key, iv []byte) ([]byte, error)

func DesOFBPkcs7Encrypt

func DesOFBPkcs7Encrypt(data, key, iv []byte) ([]byte, error)

func DescCBCPkcs7Decrypt

func DescCBCPkcs7Decrypt(src, key, iv []byte) ([]byte, error)

func EasyDecrypt

func EasyDecrypt(easyType, cipherText, key, iv string) (string, error)

func EasyEncrypt

func EasyEncrypt(easyType, plainText, key, iv string) (string, error)

easyType:cryptoType/mode/padding/transcode

func Encrypt

func Encrypt(multiple cryptoType, plainText, key, iv []byte, mode mode, padding padding) ([]byte, error)

func GenKeys

func GenKeys(bits int) ([]byte, []byte, error)

GenKeys Generate both private and public keys

func GenPKCS1PrvKey

func GenPKCS1PrvKey(bits int) ([]byte, error)

GenPKCS1PrvKey Generate an RSA private key in PKCS#1 format

func GenPrvKey

func GenPrvKey(bits int) ([]byte, error)

GenPrvKey Generate private key

func GenPubKeyFromPrvKey

func GenPubKeyFromPrvKey(prvKey []byte) ([]byte, error)

GenPubKeyFromPrvKey Generate a public key from a private key

func HexDecode

func HexDecode(s string) ([]byte, error)

func HexEncode

func HexEncode(src []byte) string

func Pkcs5Padding

func Pkcs5Padding(text []byte) []byte

func Pkcs5UnPadding

func Pkcs5UnPadding(src []byte) []byte

func Pkcs7Padding

func Pkcs7Padding(text []byte, blockSize int) []byte

func Pkcs7UnPadding

func Pkcs7UnPadding(src []byte) []byte

func PrvKeyDecrypt

func PrvKeyDecrypt(prvKey, cipherText []byte) ([]byte, error)

PrvKeyDecrypt private key decryption

func PrvKeySign

func PrvKeySign(prvKey, plainText []byte, hash crypto.Hash) ([]byte, error)

PrvKeySign private key signature

func PubKeyEncrypt

func PubKeyEncrypt(pubKey, plainText []byte) ([]byte, error)

PubKeyEncrypt public key encryption

func PubKeyVerifySign

func PubKeyVerifySign(pubKey, plainText, sign []byte, hash crypto.Hash) error

PubKeyVerifySign public key verification signature

func TripleDesCBCPkcs7Decrypt

func TripleDesCBCPkcs7Decrypt(src, key, iv []byte) ([]byte, error)

func TripleDesCBCPkcs7Encrypt

func TripleDesCBCPkcs7Encrypt(data, key, iv []byte) ([]byte, error)

func TripleDesCFBPkcs7Decrypt

func TripleDesCFBPkcs7Decrypt(src, key, iv []byte) ([]byte, error)

func TripleDesCFBPkcs7Encrypt

func TripleDesCFBPkcs7Encrypt(data, key, iv []byte) ([]byte, error)

func TripleDesCTRPkcs7Decrypt

func TripleDesCTRPkcs7Decrypt(src, key, iv []byte) ([]byte, error)

func TripleDesCTRPkcs7Encrypt

func TripleDesCTRPkcs7Encrypt(data, key, iv []byte) ([]byte, error)

func TripleDesECBPkcs7Decrypt

func TripleDesECBPkcs7Decrypt(src, key []byte) ([]byte, error)

ECB 模式解密 src密文,key加密时的密钥

func TripleDesECBPkcs7Encrypt

func TripleDesECBPkcs7Encrypt(data, key []byte) ([]byte, error)

Ecb加密模式 data要加密的数据,key要加密的密钥

func TripleDesOFBPkcs7Decrypt

func TripleDesOFBPkcs7Decrypt(src, key, iv []byte) ([]byte, error)

func TripleDesOFBPkcs7Encrypt

func TripleDesOFBPkcs7Encrypt(data, key, iv []byte) ([]byte, error)

func ZeroPadding

func ZeroPadding(text []byte, blockSize int) []byte

func ZeroUnPadding

func ZeroUnPadding(src []byte) []byte

Types

This section is empty.

Jump to

Keyboard shortcuts

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