Golang語言 加密系列之AES
阿新 • • 發佈:2022-05-05
加密程式碼:
func Encrypt(plantText, key []byte) ([]byte, error) { block, err := aes.NewCipher(key) //選擇加密演算法 if err != nil { return nil, err } plantText = PKCS7Padding(plantText, block.BlockSize()) blockModel := cipher.NewCBCEncrypter(block, key) ciphertext := make([]byte, len(plantText)) blockModel.CryptBlocks(ciphertext, plantText) return ciphertext, nil } func PKCS7Padding(ciphertext []byte, blockSize int) []byte { padding := blockSize - len(ciphertext)%blockSize padtext := bytes.Repeat([]byte{byte(padding)}, padding) return append(ciphertext, padtext...) }
解密程式碼:
func Decrypt(ciphertext, key []byte) ([]byte, error) { keyBytes := []byte(key) block, err := aes.NewCipher(keyBytes) //選擇加密演算法 if err != nil { return nil, err } blockModel := cipher.NewCBCDecrypter(block, keyBytes) plantText := make([]byte, len(ciphertext)) blockModel.CryptBlocks(plantText, ciphertext) plantText = PKCS7UnPadding(plantText, block.BlockSize()) return plantText, nil } func PKCS7UnPadding(plantText []byte, blockSize int) []byte { length := len(plantText) unpadding := int(plantText[length-1]) return plantText[:(length - unpadding)] }