This is a creation in Article, where the information may have evolved or changed.
Package Mainimport ("Crypto/aes" "Crypto/cipher" "FMT") type tobytes struct {CIP cipher. Block Pdgtext []byte}func Main () {CIP, _: = AES. Newcipher ([]byte("1234567891234567")) Enc: = &tobytes{cip:cip, Pdgtext:make ([]byteCip. BlockSize ())} src: = Enc. Encrypt ([]byte("Czxichen")) DST: = Enc. Decrypt (SRC) fmt. Println (string(DST))}//Use AES to encrypt text, encrypted text cannot be emptyFunc (a*tobytes) Encrypt (src []byte) (DST []byte) {src =a. padding (src) DST = make ([]byte,Len(SRC)) var index int =0 for Len(SRC) >0{a. CIP. Encrypt (dst[index:index+a. CIP. BlockSize ()], src) index + =a. CIP. BlockSize () src = src[a. CIP. BlockSize ():]}returnDst//Decrypt text with AESFunc (a*tobytes) Decrypt (src []byte) (DST []byte) {if Len(SRC)%a. CIP. BlockSize ()! =0{returnSRC} DST = make ([]byte,Len(SRC)) var index int =0 for Len(SRC) >0{a. CIP. Decrypt (dst[index:index+a. CIP. BlockSize ()], src) index + =a. CIP. BlockSize () src = src[a. CIP. BlockSize ():]}return a. unpadding (DST)}//When encrypting text with AES, the text must be fixed long, i.e. it must be an integer multiple of 16,24,32,Func (a*tobytes) padding (src []byte) (DST []byte) {PDG: =a. CIP. BlockSize ()-Len(SRC)%a. CIP. BlockSize () P: =a. pdgtext[:p DG] p[pdg-1] =byte(PDG)returnAppend (src, p ...)}//Use AES to decrypt text, decrypt deleted padding textFunc (a*tobytes) unpadding (src []byte) (DST []byte) {length:=Len(SRC)if length<=0{returnSRC}returnSrc[:(length-Int (src[length-1]))]}