crypto

package
v0.0.0-...-279ef49 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2024 License: GPL-3.0 Imports: 15 Imported by: 0

README

crypto

go crypto library

加密算法

AES支持三种长度的密钥:128位,192位,256位,即AES128,AES192,AES256 AES256安全性最高 AES128性能最高

  • IV支持不传入值,默认生成且返回
  • 生成随机key,根据不同算法生成

IV

// 在除ECB以外的所有加密方式中,都需要用到IV对加密结果进行随机化。在使用同一种加密同一个密钥时不应该使用相同的IV,否则会失去一定甚至全部的安全性。

BlockMode

ECB 电码本模式 Electronic Codebook Book

ECB模式有一个显著的安全问题:如果使用相同的密钥, 那么相同的明文块就会生成相同的密文块,不能很好的隐藏数据模式,因此,在密码协议中不建议使用ECB模式

CBC 密码分组链接模式 Cipher Block Chaining

// BlockModeCBC CBC模式加密过程是串行的,不能并行化,速度比较慢,但是解密可以并行。另外,如果密文的某一位被修改了,只会使这个密文块所对应的明文块完全改变并且改变下一个明文块的对应位,安全性仍然有一定的欠缺。

CFB 密码反馈模式 Cipher FeedBack
CTR 计算器模式 Counter
OFB 输出反馈模式 Output FeedBack

参考资料:

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CBC cbc

CBC 密码块链模式(Cipher-block chaining) 每个明文块先与前一个密文块进行异或后,再进行加密。在这种方法中,每个密文块都依赖于它前面的所有平文块。 同时,为了保证每条消息的唯一性,在第一个块中需要使用初始化向量。 CBC是最为常用的工作模式。它的主要缺点在于加密过程是串行的,无法被并行化,而且消息必须被填充到块大小的整数倍。 加密时,平文中的微小改变会导致其后的全部密文块发生改变,而在解密时,从两个邻接的密文块中即可得到一个平文块。 因此,解密过程可以被并行化,而解密时,密文中一位的改变只会导致其对应的平文块完全改变和下一个平文块中对应位发生改变, 不会影响到其它平文的内容

View Source
var CFB cfb
View Source
var CTR ctr
View Source
var ECB ecbBlockMode

ECB 电子密码本模式(lectronic codebook) 最简单的模式,也是最不安全的模式。需要加密的消息按照块密码的块大小被分为数个块,并对每个块进行独立加密,相同明文会产生同样的密文组。 这会暴露明文数据的格式和统计特征,从而有潜在的安全风险,但是用于短数据(如加密密钥)时非常理想

View Source
var GCM gcm

GCM AE,Authenticated Encryption

View Source
var OFB ofb
View Source
var PKCS7Padding pkcs7Padding
View Source
var ZeroPadding zeroPadding

Functions

func ParsePrivateKey

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

func ParseRSAPublicKey

func ParseRSAPublicKey(publickey []byte) (*rsa.PublicKey, error)

func ParseRSAPublicKeyFromCert

func ParseRSAPublicKeyFromCert(certPEM []byte) (*rsa.PublicKey, error)

func RC4Encrypt

func RC4Encrypt(key, src []byte) []byte

func RSADecryptNoPadding

func RSADecryptNoPadding(ciphertext, privateKey []byte) ([]byte, error)

TODO:RSADecryptNoPadding RSA无填充私钥解密

func RSADecryptOAEP

func RSADecryptOAEP(cipherText, privateKey []byte) ([]byte, error)

RSADecryptOAEP RSA私钥解密

func RSADecryptPKCS1v15

func RSADecryptPKCS1v15(cipherText, privateKey []byte) ([]byte, error)

RSADecryptPKCS1v15 RSA私钥解密 支持长文本加密

func RSAEncryptNoPadding

func RSAEncryptNoPadding(plaintext, publicKey []byte) ([]byte, error)

RSAEncryptNoPadding RSA无填充公钥加密

func RSAEncryptOAEP

func RSAEncryptOAEP(plainText, publicKey []byte) ([]byte, error)

RSAEncryptOAEP RSA RSAES-OAEP加密方案是RFC3447推荐新应用使用的标准, RSAES-PKCS1-v1_5 是为了与已存在的应用兼容,并且不建议用于新应用。 详细参考:https://www.rfc-editor.org/rfc/rfc3447.html#section-7

func RSAEncryptPKCS1v15

func RSAEncryptPKCS1v15(plainText, publicKey []byte) ([]byte, error)

RSAEncryptPKCS1v15 RSA公钥加密 支持长文本加密

func RSAPrivateEncrypt

func RSAPrivateEncrypt(plainText, privateKey []byte) ([]byte, error)

RSAPrivateEncrypt RSA私钥加密

func RSAPublicDecrypt

func RSAPublicDecrypt(cipherText, publicKey []byte) ([]byte, error)

RSAPublicDecrypt RSA公钥解密

Types

type AESCipher

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

AESCipher AES Advanced Encryption Standard 支持 ECB、CBC、CFB、CTR、GCM、OFB key 长度 16、24、32 位,即 128、192、256 bit(AES-128、AES-192、AES-256),位数越大安全性越高但加密速度越慢

func (*AESCipher) Decrypt

func (c *AESCipher) Decrypt(cipherTxt []byte) ([]byte, error)

func (*AESCipher) Encrypt

func (c *AESCipher) Encrypt(plainTxt []byte) ([]byte, error)

type BlockMode

type BlockMode interface {
	Encrypt(*cipherConfig, cipher.Block, []byte) ([]byte, error)
	Decrypt(*cipherConfig, cipher.Block, []byte) ([]byte, error)
}

type Cipher

type Cipher interface {
	Encrypt(plainTxt []byte) ([]byte, error)
	Decrypt(cipherTxt []byte) ([]byte, error)
}

func NewAES

func NewAES(key []byte, opts ...Option) Cipher

func NewDES

func NewDES(key []byte, opts ...Option) Cipher

func NewTripleDES

func NewTripleDES(key []byte, opts ...Option) Cipher

type DESCipher

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

func (*DESCipher) Decrypt

func (c *DESCipher) Decrypt(cipherTxt []byte) ([]byte, error)

func (*DESCipher) Encrypt

func (c *DESCipher) Encrypt(plainTxt []byte) ([]byte, error)

type Option

type Option func(*cipherConfig)

func WithAdditionalData

func WithAdditionalData(additionalData []byte) Option

gcm

func WithBlockMode

func WithBlockMode(bc BlockMode) Option

func WithIV

func WithIV(iv []byte) Option

func WithNonce

func WithNonce(nonce []byte) Option

func WithPaddingMode

func WithPaddingMode(padding PaddingMode) Option

func WithTagSize

func WithTagSize(tagSize int) Option

type PaddingMode

type PaddingMode interface {
	Padding([]byte, int) []byte
	UnPadding([]byte, int) ([]byte, error)
}

type TripleDESCipher

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

func (*TripleDESCipher) Decrypt

func (c *TripleDESCipher) Decrypt(cipherTxt []byte) ([]byte, error)

func (*TripleDESCipher) Encrypt

func (c *TripleDESCipher) Encrypt(plainTxt []byte) ([]byte, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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