From 501739c366a277b9aed2d1190dc11ed6c6a9de35 Mon Sep 17 00:00:00 2001 From: alexiosli Date: Thu, 7 Jul 2022 11:46:28 +0800 Subject: [PATCH] =?UTF-8?q?add:=E7=94=B5=E5=AD=90=E7=AD=BE=E9=9B=86?= =?UTF-8?q?=E6=88=90=E7=89=88=E5=9B=9E=E8=B0=83=E8=A7=A3=E5=AF=86demo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/ess/v20201111/ess_callback.go | 50 ++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 examples/ess/v20201111/ess_callback.go diff --git a/examples/ess/v20201111/ess_callback.go b/examples/ess/v20201111/ess_callback.go new file mode 100644 index 0000000000..9b0d8cdfcb --- /dev/null +++ b/examples/ess/v20201111/ess_callback.go @@ -0,0 +1,50 @@ +package v20201111 + +import ( + "crypto/aes" + "crypto/cipher" + "encoding/base64" + "fmt" + "testing" +) + +func AesDecrypt(crypted, key []byte) ([]byte, error) { + block, err := aes.NewCipher(key) + if err != nil { + return nil, err + } + blockSize := block.BlockSize() + blockMode := cipher.NewCBCDecrypter(block, key[:blockSize]) + origData := make([]byte, len(crypted)) + blockMode.CryptBlocks(origData, crypted) + origData = PKCS7UnPadding(origData) + return origData, nil +} + +// PKCS7UnPadding 去除填充 +func PKCS7UnPadding(origData []byte) []byte { + length := len(origData) + unPadding := int(origData[length-1]) + return origData[:(length - unPadding)] +} + +func TestDecrypt(t *testing.T) { + // 传入CallbackUrlKey + key := "***********" + // 传入密文 + content := "***********" + + // base64解密 + crypted, err := base64.StdEncoding.DecodeString(content) + if err != nil { + fmt.Printf("base64 DecodeString returned: %s", err) + return + } + + origData, err := AesDecrypt(crypted, []byte(key)) + if err != nil { + fmt.Printf("AesDecrypt returned: %s", err) + return + } + fmt.Printf("%s", string(origData)) +}