crypter

package
v1.0.13 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2026 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	AesErrDataEmpty  = "AES data invalid: cannot be empty"
	AesErrDataDecode = "AES data invalid: can't decode"
	AesErrKey        = "AES key invalid: must to be 16 or 24 or 32 bytes long"
	AesErrIV         = "AES iv invalid: must be 16 bytes long"
)
View Source
const (
	DesErrDataEmpty  = "DES data invalid: cannot be empty"
	DesErrDataDecode = "DES data invalid: can't decode"
	DesErrKey        = "DES key invalid: must be 8 bytes long"
	DesErrIV         = "DES iv invalid: must be 8 bytes long"
)
View Source
const (
	RsaMakeKeysBitDefault int = 1024 // 密钥的位数: 512/1024/2048/4096...默认1024
)

------------------------ RSA 加密解密 RSA 的加密机制有两种方案: RSA-OAEP 和 RSA-PKCS1-v1_5 推荐在新的应用中使用 RSA-OAEP ------------------------ RSA 签名 RSA 的签名机制机有种方案:RSA-PSS 和 RSA-PKCS1-v1_5 推荐在新的应用中使用 RSA-PSS

Variables

View Source
var (
	ErrRsaDataEmpty       = errors.New("RSA data cannot be empty")
	ErrRsaBase64Decode    = errors.New("RSA data base64 decode error")
	ErrRsaPublicKeyEmpty  = errors.New("RSA publicKey cannot be empty")
	ErrRsaPrivateKeyEmpty = errors.New("RSA privateKey cannot be empty")
)
View Source
var DataCode = dataCode{}

Functions

func AesDecryptCBC

func AesDecryptCBC(data []byte, key []byte, iv []byte) (decData []byte, err error)

AesDecryptCBC 字节数组数据解密 (运算模式: CBC, 填充模式: PKCS7)

  • decData 待解密的字节数组数据
  • key 秘钥, 不同秘钥长度对应不同的加密位数: AES 128=> 16位Key / 192=> 24位Key / 256=> 32位Key
  • iv 向量, AES Block Size固定为16位,超过16位时截取前16位
  • 返回解密后的字节数组

func AesDecryptCBCBase64

func AesDecryptCBCBase64(data string, key string, iv string) (string, error)

AesDecryptCBCBase64 解密字符串数据 (AES/CBC/PKCS7Padding)

  • data 待解密的字符串(Base64格式)
  • key 秘钥, 不同秘钥长度对应不同的加密位数: AES 128=> 16位Key / 192=> 24位Key / 256=> 32位Key
  • iv 向量, AES Block Size固定为16位,超过16位时截取前16位
  • 返回解密后的字符串

func AesDecryptCBCHex

func AesDecryptCBCHex(data string, key string, iv string) (string, error)

AesDecryptCBCHex 解密字符串数据 (AES/CBC/PKCS7Padding)

  • data 待解密的字符串(Hex 16进制格式的加密结果(小写))
  • key 秘钥, 不同秘钥长度对应不同的加密位数: AES 128=> 16位Key / 192=> 24位Key / 256=> 32位Key
  • iv 向量, AES Block Size固定为16位,超过16位时截取前16位
  • 返回解密后的字符串

func AesDecryptCFB

func AesDecryptCFB(data []byte, key []byte, iv []byte) (decData []byte, err error)

AesDecryptCFB 数据解密 (AES/CFB/NoPadding)

  • data 待解密的数据
  • key 秘钥, 不同秘钥长度对应不同的加密位数: AES 128=> 16位Key / 192=> 24位Key / 256=> 32位Key
  • iv 向量, AES Block Size固定为16位,超过16位时截取前16位
  • 返回解密后的数据

func AesDecryptCFBBase64

func AesDecryptCFBBase64(data string, key string, iv string) (string, error)

AesDecryptCFBBase64 解密字符串数据(AES/CFB/NoPadding)

  • data 待解密的字符串(Base64格式)
  • key 秘钥, 不同秘钥长度对应不同的加密位数: AES 128=> 16位Key / 192=> 24位Key / 256=> 32位Key
  • iv 向量, AES Block Size固定为16位,超过16位时截取前16位
  • 返回解密后的字符串

func AesDecryptCFBHex

func AesDecryptCFBHex(data string, key string, iv string) (string, error)

AesDecryptCFBHex 解密字符串数据 (AES/CFB/NoPadding)

  • data 待解密的字符串(Hex 16进制格式的加密结果(小写))
  • key 秘钥, 不同秘钥长度对应不同的加密位数: AES 128=> 16位Key / 192=> 24位Key / 256=> 32位Key
  • iv 向量, AES Block Size固定为16位,超过16位时截取前16位
  • 返回解密后的字符串

func AesEncryptCBC

func AesEncryptCBC(data []byte, key []byte, iv []byte) (encData []byte, err error)

AesEncryptCBC 字节数据加密 (AES/CBC/PKCS7Padding)

  • data 待加密的数据
  • key 秘钥, 不同秘钥长度对应不同的加密位数: AES 128=> 16位Key / 192=> 24位Key / 256=> 32位Key
  • iv 向量,AES Block Size固定为16位,超过16位时截取前16位
  • 返回加密后的字节数组

func AesEncryptCBCBase64

func AesEncryptCBCBase64(data string, key string, iv string) (string, error)

AesEncryptCBCBase64 字符串数据加密 (AES/CBC/PKCS7Padding)

  • data 待加密的字符串
  • key 秘钥, 不同秘钥长度对应不同的加密位数: AES 128=> 16位Key / 192=> 24位Key / 256=> 32位Key
  • iv 向量, AES Block Size固定为16位,超过16位时截取前16位
  • 返回 Base64格式的加密结果

func AesEncryptCBCHex

func AesEncryptCBCHex(data string, key string, iv string) (string, error)

AesEncryptCBCHex 字符串数据加密 (AES/CBC/PKCS7Padding)

  • data 待加密的字符串
  • key 秘钥, 不同秘钥长度对应不同的加密位数: AES 128=> 16位Key / 192=> 24位Key / 256=> 32位Key
  • iv 向量, AES Block Size固定为16位,超过16位时截取前16位
  • 返回 Hex 16进制格式的加密结果(小写)

func AesEncryptCFB

func AesEncryptCFB(data []byte, key []byte, iv []byte) (encData []byte, err error)

AesEncryptCFB 数据加密 (AES/CFB/NoPadding)

  • data 待加密的数据
  • key 秘钥, 不同秘钥长度对应不同的加密位数: AES 128=> 16位Key / 192=> 24位Key / 256=> 32位Key
  • iv 向量, AES Block Size固定为16位,超过16位时截取前16位
  • 返回加密后的数据

func AesEncryptCFBBase64

func AesEncryptCFBBase64(data string, key string, iv string) (string, error)

AesEncryptCFBBase64 字符串数据加密 (AES/CFB/NoPadding)

  • data 待加密的字符串
  • key 秘钥, 不同秘钥长度对应不同的加密位数: AES 128=> 16位Key / 192=> 24位Key / 256=> 32位Key
  • iv 向量, AES Block Size固定为16位,超过16位时截取前16位
  • 返回 Base64格式的加密结果

func AesEncryptCFBHex

func AesEncryptCFBHex(data string, key string, iv string) (string, error)

AesEncryptCFBHex 字符串数据加密 (AES/CFB/NoPadding)

  • data 待加密的字符串
  • key 秘钥, 不同秘钥长度对应不同的加密位数: AES 128=> 16位Key / 192=> 24位Key / 256=> 32位Key
  • iv 向量, AES Block Size固定为16位,超过16位时截取前16位
  • 返回 Hex 16进制格式的加密结果(小写)

func DesDecryptCBC

func DesDecryptCBC(data []byte, key []byte, iv []byte) (decData []byte, err error)

DesDecryptCBC 字节数组数据解密 (DES/CBC/PKCS7Padding)

  • key 秘钥, 8个字节的长度,DES Block Size固定为8位,超过8位时截取前8位
  • iv 向量,8个字节的长度,DES Block Size固定为8位,超过8位时截取前8位
  • 返回解密后的字节数组

func DesDecryptCBCBase64

func DesDecryptCBCBase64(data string, key string, iv string) (string, error)

DesDecryptCBCBase64 解密字符串数据 (DES/CBC/PKCS7Padding)

  • data 待解密的字符串(Base64格式)
  • key 秘钥, 8个字节的长度,DES Block Size固定为8位,超过8位时截取前8位
  • iv 向量,8个字节的长度,DES Block Size固定为8位,超过8位时截取前8位
  • 返回解密后的字符串

func DesDecryptCBCHex

func DesDecryptCBCHex(data string, key string, iv string) (string, error)

DesDecryptCBCHex 解密字符串数据 (DES/CBC/PKCS7Padding)

  • data 待解密的字符串(Hex 16进制格式的加密结果(小写))
  • key 秘钥, 8个字节的长度,DES Block Size固定为8位,超过8位时截取前8位
  • iv 向量,8个字节的长度,DES Block Size固定为8位,超过8位时截取前8位
  • 返回解密后的字符串

func DesDecryptCFB

func DesDecryptCFB(data []byte, key []byte, iv []byte) (decData []byte, err error)

DesDecryptCFB 数据解密 (DES/CFB/NoPadding)

  • data 待解密的数据
  • key 秘钥, 8个字节的长度,DES Block Size固定为8位,超过8位时截取前8位
  • iv 向量,8个字节的长度,DES Block Size固定为8位,超过8位时截取前8位
  • 返回解密后的数据

func DesDecryptCFBBase64

func DesDecryptCFBBase64(data string, key string, iv string) (string, error)

DesDecryptCFBBase64 解密字符串数据(DES/CFB/NoPadding)

  • data 待解密的字符串(Base64格式)
  • key 秘钥, 8个字节的长度,DES Block Size固定为8位,超过8位时截取前8位
  • iv 向量,8个字节的长度,DES Block Size固定为8位,超过8位时截取前8位
  • 返回解密后的字符串

func DesDecryptCFBHex

func DesDecryptCFBHex(data string, key string, iv string) (string, error)

DesDecryptCFBHex 解密字符串数据 (DES/CFB/NoPadding)

  • data 待解密的字符串(Hex 16进制格式的加密结果(小写))
  • key 秘钥, 8个字节的长度,DES Block Size固定为8位,超过8位时截取前8位
  • iv 向量,8个字节的长度,DES Block Size固定为8位,超过8位时截取前8位
  • 返回解密后的字符串

func DesEncryptCBC

func DesEncryptCBC(data []byte, key []byte, iv []byte) (encData []byte, err error)

DesEncryptCBC 字节数据加密 (DES/CBC/PKCS7Padding)

  • data 待加密的数据
  • key 秘钥, 8个字节的长度,DES Block Size固定为8位,超过8位时截取前8位
  • iv 向量,8个字节的长度,DES Block Size固定为8位,超过8位时截取前8位
  • 返回加密后的字节数组

func DesEncryptCBCBase64

func DesEncryptCBCBase64(data string, key string, iv string) (string, error)

DesEncryptCBCBase64 字符串数据加密 (DES/CBC/PKCS7Padding)

  • data 待加密的字符串
  • key 秘钥, 8个字节的长度,DES Block Size固定为8位,超过8位时截取前8位
  • iv 向量,8个字节的长度,DES Block Size固定为8位,超过8位时截取前8位
  • 返回 Base64格式的加密结果

func DesEncryptCBCHex

func DesEncryptCBCHex(data string, key string, iv string) (string, error)

DesEncryptCBCHex 字符串数据加密 (DES/CBC/PKCS7Padding)

  • data 待加密的字符串
  • key 秘钥, 8个字节的长度,DES Block Size固定为8位,超过8位时截取前8位
  • iv 向量,8个字节的长度,DES Block Size固定为8位,超过8位时截取前8位
  • 返回 Hex 16进制格式的加密结果(小写)

func DesEncryptCFB

func DesEncryptCFB(data []byte, key []byte, iv []byte) (encData []byte, err error)

DesEncryptCFB 数据加密 (DES/CFB/NoPadding)

  • data 待加密的数据
  • key 秘钥, 8个字节的长度,DES Block Size固定为8位,超过8位时截取前8位
  • iv 向量,8个字节的长度,DES Block Size固定为8位,超过8位时截取前8位
  • 返回加密后的数据

func DesEncryptCFBBase64

func DesEncryptCFBBase64(data string, key string, iv string) (string, error)

DesEncryptCFBBase64 字符串数据加密 (DES/CFB/NoPadding)

  • data 待加密的字符串
  • key 秘钥, 8个字节的长度,DES Block Size固定为8位,超过8位时截取前8位
  • iv 向量,8个字节的长度,DES Block Size固定为8位,超过8位时截取前8位
  • 返回 Base64格式的加密结果

func DesEncryptCFBHex

func DesEncryptCFBHex(data string, key string, iv string) (string, error)

DesEncryptCFBHex 字符串数据加密 (DES/CFB/NoPadding)

  • data 待加密的字符串
  • key 秘钥, 8个字节的长度,DES Block Size固定为8位,超过8位时截取前8位
  • iv 向量,8个字节的长度,DES Block Size固定为8位,超过8位时截取前8位
  • 返回 Hex 16进制格式的加密结果(小写)

func PKCS7Padding

func PKCS7Padding(ciphertext []byte, blockSize int) []byte

PKCS7Padding 填充补全码

func PKCS7UnPadding

func PKCS7UnPadding(origData []byte, blockSize int) []byte

PKCS7UnPadding 移除补全码

func ParsePrivateKey

func ParsePrivateKey(privateKey []byte) (*rsa.PrivateKey, error)

ParsePrivateKey 转化私钥为rsa.PrivateKey对象

  • 支持 PEM 格式
  • 兼容 PKCS#1 或 PKCS#8 标准

func ParsePublicKey

func ParsePublicKey(publicKey []byte) (*rsa.PublicKey, error)

ParsePublicKey 转化公钥为 rsa.PublicKey 对象

  • 支持 PEM 格式
  • 兼容 PKCS#1 或 PKCS#8 标准

Types

type RsaCrypto

type RsaCrypto struct {
	// contains filtered or unexported fields
}

func Rsa

func Rsa() *RsaCrypto

func (*RsaCrypto) Decrypt

func (r *RsaCrypto) Decrypt(data []byte) ([]byte, error)

Decrypt 私钥解密(PKCS标准)

  • 遵循 PKCS#1 v1.5 标准(兼容性较好,在一些旧的系统或者对安全性要求不是极高的场景下使用)

func (*RsaCrypto) DecryptBase64

func (r *RsaCrypto) DecryptBase64(data string) ([]byte, error)

DecryptBase64 私钥解密(PKCS标准), 待解密的数据为 base64 编码的字符串

  • 遵循 PKCS#1 v1.5 标准(兼容性较好,在一些旧的系统或者对安全性要求不是极高的场景下使用)

func (*RsaCrypto) DecryptOAEP

func (r *RsaCrypto) DecryptOAEP(data []byte, hashType crypto.Hash, labels ...[]byte) ([]byte, error)

DecryptOAEP 私钥解密(OAEP 标准)

  • 遵循 OAEP 标准(广泛应用于对安全性要求较高的场景,如金融交易、数字签名认证、安全通信协议, 如 SSL/TLS 在某些模式下)
  • hashType 散列算法类型
  • labels参数为可选参数, label用于与明文消息一起进行混淆操作, 加密时指定加密标签, 解密时用于验证标签是否一致

func (*RsaCrypto) DecryptOAEPBase64

func (r *RsaCrypto) DecryptOAEPBase64(data string, hashType crypto.Hash, labels ...[]byte) ([]byte, error)

DecryptOAEPBase64 私钥解密(OAEP 标准), 待解密的数据为 base64 编码的字符串

  • 遵循 OAEP 标准(广泛应用于对安全性要求较高的场景,如金融交易、数字签名认证、安全通信协议, 如 SSL/TLS 在某些模式下)
  • hashType 散列算法类型
  • labels参数为可选参数, label用于与明文消息一起进行混淆操作, 加密时指定加密标签, 解密时用于验证标签是否一致

func (*RsaCrypto) Encrypt

func (r *RsaCrypto) Encrypt(data []byte) ([]byte, error)

Encrypt 公钥加密 (PKCS 标准)

  • 遵循 PKCS#1 v1.5 标准(兼容性较好,在一些旧的系统或者对安全性要求不是极高的场景下使用)

func (*RsaCrypto) EncryptBase64

func (r *RsaCrypto) EncryptBase64(data []byte) (string, error)

EncryptBase64 公钥加密 (PKCS 标准), 加密结果使用 base64 编码后返回

  • 遵循 PKCS#1 v1.5 标准(兼容性较好,在一些旧的系统或者对安全性要求不是极高的场景下使用)

func (*RsaCrypto) EncryptOAEP

func (r *RsaCrypto) EncryptOAEP(data []byte, hashType crypto.Hash, labels ...[]byte) ([]byte, error)

EncryptOAEP 公钥加密(OAEP 标准)

  • 遵循 OAEP 标准(广泛应用于对安全性要求较高的场景,如金融交易、数字签名认证、安全通信协议, 如 SSL/TLS 在某些模式下)
  • hashType 散列算法类型
  • labels参数为可选参数, label用于与明文消息一起进行混淆操作, 加密时指定加密标签, 解密时用于验证标签是否一致

func (*RsaCrypto) EncryptOAEPBase64

func (r *RsaCrypto) EncryptOAEPBase64(data []byte, hashType crypto.Hash, labels ...[]byte) (string, error)

EncryptOAEPBase64 公钥加密(OAEP 标准), 加密结果使用 base64 编码后返回

  • 遵循 OAEP 标准(广泛应用于对安全性要求较高的场景,如金融交易、数字签名认证、安全通信协议, 如 SSL/TLS 在某些模式下)
  • hashType 散列算法类型
  • labels参数为可选参数, label用于与明文消息一起进行混淆操作, 加密时指定加密标签, 解密时用于验证标签是否一致

func (*RsaCrypto) GetPrivateKey

func (r *RsaCrypto) GetPrivateKey() []byte

GetPrivateKey 获取私钥

func (*RsaCrypto) GetPrivateKeyToString

func (r *RsaCrypto) GetPrivateKeyToString() string

GetPrivateKeyToString 获取私钥(字符串)

func (*RsaCrypto) GetPublicKey

func (r *RsaCrypto) GetPublicKey() []byte

GetPublicKey 获取公钥

func (*RsaCrypto) GetPublicKeyToString

func (r *RsaCrypto) GetPublicKeyToString() string

GetPublicKeyToString 获取公钥(字符串)

func (*RsaCrypto) MakeKeys

func (r *RsaCrypto) MakeKeys(bits ...int) (PrivateBytes []byte, PublicBytes []byte, err error)

MakeKeys 随机生成一对指定位数的RSA私钥和公钥(PEM格式/PKCS#8标准)

  • 生成密钥的位数: bits => 512/1024/2048/4096...默认1024
  • 密钥格式: PEM
  • 密钥标准: PKCS#8

func (*RsaCrypto) MakeKeysPCKS1

func (r *RsaCrypto) MakeKeysPCKS1(bits ...int) (PrivateBytes []byte, PublicBytes []byte, err error)

MakeKeysPCKS1 随机生成一对指定位数的RSA私钥和公钥(PEM格式/PKCS#1标准)

  • 生成密钥的位数: bits => 512/1024/2048/4096...默认1024
  • 密钥格式: PEM
  • 密钥标准: PKCS#1

func (*RsaCrypto) MakeKeysPKCS1ToString

func (r *RsaCrypto) MakeKeysPKCS1ToString(bits ...int) (PrivateKey string, PublicKey string, err error)

MakeKeysPKCS1ToString 随机生成一对指定位数的RSA私钥和公钥(PEM格式/PKCS#1标准)

  • 生成密钥的位数: bits => 512/1024/2048/4096...默认1024
  • 密钥格式: PEM
  • 密钥标准: PKCS#1

func (*RsaCrypto) MakeKeysToString

func (r *RsaCrypto) MakeKeysToString(bits ...int) (PrivateKey string, PublicKey string, err error)

MakeKeysToString 随机生成一对指定位数的RSA私钥和公钥(PEM格式/PKCS#8标准)

  • 生成密钥的位数: bits => 512/1024/2048/4096...默认1024
  • 密钥格式: PEM
  • 密钥标准: PKCS#8

func (*RsaCrypto) SetPrivateKey

func (r *RsaCrypto) SetPrivateKey(privateKey []byte) (err error)

SetPrivateKey 设置私钥(支持PEM格式, 兼容PKCS#8和PKCS#1标准)

  • 支持 PEM 格式
  • 兼容 PKCS#1 或 PKCS#8 标准

func (*RsaCrypto) SetPrivateKeyWithFile

func (r *RsaCrypto) SetPrivateKeyWithFile(file1Path string) (err error)

SetPrivateKeyWithFile 设置私钥(支持PEM格式, 兼容PKCS#8和PKCS#1标准)

  • 支持 PEM 格式
  • 兼容 PKCS#1 或 PKCS#8 标准

func (*RsaCrypto) SetPrivateKeyWithString

func (r *RsaCrypto) SetPrivateKeyWithString(privateKey string) (err error)

SetPrivateKeyWithString 设置私钥(支持PEM格式, 兼容PKCS#8和PKCS#1标准)

  • 支持 PEM 格式
  • 兼容 PKCS#1 或 PKCS#8 标准

func (*RsaCrypto) SetPublicKey

func (r *RsaCrypto) SetPublicKey(publicKey []byte) (err error)

SetPublicKey 设置公钥(支持PEM格式, 兼容PKCS#8和PKCS#1标准)

  • 支持 PEM 格式
  • 兼容 PKCS#1 或 PKCS#8 标准

func (*RsaCrypto) SetPublicKeyWithFile

func (r *RsaCrypto) SetPublicKeyWithFile(filePath string) (err error)

SetPublicKeyWithFile 设置公钥(支持PEM格式, 兼容PKCS#8和PKCS#1标准)

  • 支持 PEM 格式
  • 兼容 PKCS#1 或 PKCS#8 标准

func (*RsaCrypto) SetPublicKeyWithString

func (r *RsaCrypto) SetPublicKeyWithString(publicKey string) (err error)

SetPublicKeyWithString 设置公钥(支持PEM格式, 兼容PKCS#8和PKCS#1标准)

  • 支持 PEM 格式
  • 兼容 PKCS#1 或 PKCS#8 标准

func (*RsaCrypto) Sign

func (r *RsaCrypto) Sign(data []byte, hashType crypto.Hash) (string, error)

Sign (PKCS标准) 使用指定算法进行签名(私匙签名 Base64)

  • 遵循 PKCS#1 v1.5 标准(兼容性较好,在一些旧的系统或者对安全性要求不是极高的场景下使用)
  • hashType 散列算法类型
  • 秘钥格式支持 PKCS#1 或 PKCS#8 格式
  • 返回签名结果 Base64格式的字符串

func (*RsaCrypto) SignPSS

func (r *RsaCrypto) SignPSS(data []byte, hashType crypto.Hash) (string, error)

SignPSS (SignPSS标准) 使用指定算法进行签名(私匙签名 Base64)

  • hashType 散列算法类型
  • 秘钥格式支持 PKCS#1 或 PKCS#8 格式
  • 返回签名结果 Base64格式的字符串

func (*RsaCrypto) VerifySign

func (r *RsaCrypto) VerifySign(data []byte, hashType crypto.Hash, signature string) error

VerifySign (PKCS标准) 使用指定算法验证签名(公匙验签 Base64)

  • 遵循 PKCS#1 v1.5 标准(兼容性较好,在一些旧的系统或者对安全性要求不是极高的场景下使用)
  • hashType 散列算法类型
  • 待验证签名是 Base64格式的字符串
  • 秘钥格式支持 PKCS#1 或 PKCS#8 格式
  • 返回验证结果, 为nil表示验证成功

func (*RsaCrypto) VerifySignPSS

func (r *RsaCrypto) VerifySignPSS(data []byte, hashType crypto.Hash, signature string) error

VerifySignPSS (SignPSS标准) 使用指定算法验证签名(公匙验签 Base64)

  • hashType 散列算法类型
  • 待验证签名是 Base64格式的字符串
  • 秘钥格式支持 PKCS#1 或 PKCS#8 格式
  • 返回验证结果, 为nil表示验证成功

Jump to

Keyboard shortcuts

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