package mainimport ( "bytes" "crypto/aes" "encoding/base64" "fmt")func main() { // 原始数据 plaintext := []byte("BJYJ{\"studyId\":\"1240202004\",\"studyType\":\"GH\"}20272022200728") // 密钥 key := []byte("uazKYCTHHY23@0vz") // 创建一个AES块,使用给定的密钥 block, err := aes.NewCipher(key) if err != nil { panic(err) } // 对明文进行填充,确保长度是块大小的倍数 plaintext = PKCS7Padding(plaintext, block.BlockSize()) // 加密数据 ciphertext := make([]byte, len(plaintext)) for offset := 0; offset < len(plaintext); offset += block.BlockSize() { block.Encrypt(ciphertext[offset:], plaintext[offset:]) } // 将密文进行Base64编码 encodedCiphertext := base64.StdEncoding.EncodeToString(ciphertext) fmt.Println("加密后示例:", encodedCiphertext) // 解码Base64密文 decodedCiphertext, err := base64.StdEncoding.DecodeString(encodedCiphertext) if err != nil { panic(err) } // 创建一个解密器 decryptedText := make([]byte, len(decodedCiphertext)) for offset := 0; offset < len(decodedCiphertext); offset += block.BlockSize() { block.Decrypt(decryptedText[offset:], decodedCiphertext[offset:]) } // 去除填充 decryptedText = PKCS7UnPadding(decryptedText) fmt.Println("解密后示例:", string(decryptedText))}// PKCS7Padding 使用PKCS7填充对明文进行填充func PKCS7Padding(plaintext []byte, blockSize int) []byte { padding := blockSize - (len(plaintext) % blockSize) padText := bytes.Repeat([]byte{byte(padding)}, padding) return append(plaintext, padText...)}// PKCS7UnPadding 使用PKCS7去除填充func PKCS7UnPadding(plaintext []byte) []byte { padding := int(plaintext[len(plaintext)-1]) return plaintext[:len(plaintext)-padding]}