目录

AES算法(八)Golang 实战

本篇将对之前所属的 AES 算法基于 Golang 语言做实战讲解,由于 ECB 工作模式相对其他工作模式安全性低(不推荐使用),本文将不对其进行展示,如在工作中确实需要使用,请自行实现。

由于篇幅限制,所有的演示代码将只展示关键步骤,如果想要看全面的过程,请移步:https://github.com/aurthurxlc/go-utils

CBC 工作模式

由于该工作模式下明文分块数据最后一块需要进行对齐处理,所以需要适用选用相应的填充方法,本文仅演示 PKCS5Padding 和 PKCS7Padding 两种模式。

该工作模式下需要准备的参数:keyivpaddingMode

加密关键步骤如下:

func (a *CryptoCBC) EncryptWithIV(plainText []byte, iv []byte) []byte {
	switch a.paddingMode {
	case Pkcs5Padding:
		plainText = __pkcs5Padding(plainText)
	case Pkcs7Padding:
		plainText = __pkcs7Padding(plainText, a.block.BlockSize())
	}

	cipherText := make([]byte, len(plainText))
	crypto := cipher.NewCBCEncrypter(a.block, iv)
	crypto.CryptBlocks(cipherText, plainText)

	return cipherText
}

解密关键步骤如下(解密后不要忘记把填充的数据移除):

func (a *CryptoCBC) DecryptWithIV(cipherText []byte, iv []byte) []byte {
	plainText := make([]byte, len(cipherText))
	crypto := cipher.NewCBCDecrypter(a.block, iv)
	crypto.CryptBlocks(plainText, cipherText)
	plainText = __pkcsUnPadding(plainText)

	return plainText
}

CFB 工作模式

该工作模式下需要准备的参数:keyiv

加密/解密关键步骤如下:

func (a *CryptoCFB) EncryptWithIV(plainText []byte, iv []byte) []byte {
	cipherText := make([]byte, len(plainText))
	crypto := cipher.NewCFBEncrypter(a.block, iv)
	crypto.XORKeyStream(cipherText, plainText)
	return cipherText
}

func (a *CryptoCFB) DecryptWithIV(cipherText []byte, iv []byte) []byte {
	plainText := make([]byte, len(cipherText))
	crypto := cipher.NewCFBDecrypter(a.block, iv)
	crypto.XORKeyStream(plainText, cipherText)

	return plainText
}

OFB 工作模式

该工作模式下需要准备的参数:keyiv

加密/解密关键步骤如下:

func (a *CryptoOFB) EncryptWithIV(plainText []byte, iv []byte) []byte {
	cipherText := make([]byte, len(plainText))
	crypto := cipher.NewOFB(a.block, iv)
	crypto.XORKeyStream(cipherText, plainText)
	return cipherText
}

func (a *CryptoOFB) DecryptWithIV(cipherText []byte, iv []byte) []byte {
	plainText := make([]byte, len(cipherText))
	crypto := cipher.NewOFB(a.block, iv)
	crypto.XORKeyStream(plainText, cipherText)
	return plainText
}

CTR 工作模式

该工作模式下需要准备的参数:keyiv

加密/解密关键步骤如下:

func (a *CryptoCTR) EncryptWithIV(plainText []byte, iv []byte) []byte {
	cipherText := make([]byte, len(plainText))
	crypto := cipher.NewCTR(a.block, iv)
	crypto.XORKeyStream(cipherText, plainText)
	return cipherText
}

func (a *CryptoCTR) DecryptWithIV(cipherText []byte, iv []byte) []byte {
	plainText := make([]byte, len(cipherText))
	crypto := cipher.NewCTR(a.block, iv)
	crypto.XORKeyStream(plainText, cipherText)

	return plainText
}

GCM 工作模式

该工作模式下需要准备的参数:keyiv

加密/解密关键步骤如下:

func (a *CryptoGCM) EncryptWithIV(plainText []byte, iv []byte) []byte {
	crypto, err := cipher.NewGCMWithNonceSize(a.block, len(iv))
	if err != nil {
		panic(err.Error())
	}

	cipherText := crypto.Seal(nil, iv, plainText, nil)
	return cipherText
}

func (a *CryptoGCM) DecryptWithIV(cipherText []byte, iv []byte) []byte {
	crypto, err := cipher.NewGCMWithNonceSize(a.block, len(iv))
	if err != nil {
		panic(err.Error())
	}
	plainText, err := crypto.Open(nil, iv, cipherText, nil)
	if err != nil {
		panic(err.Error())
	}
	return plainText
}