@@ -6,6 +6,7 @@ import (
"encoding/base64"
"encoding/json"
"errors"
+ "fmt"
"github.com/silenceper/wechat/v2/miniprogram/context"
)
@@ -90,6 +91,9 @@ func GetCipherText(sessionKey, encryptedData, iv string) ([]byte, error) {
if err != nil {
return nil, err
}
+ if len(ivBytes) != aes.BlockSize {
+ return nil, fmt.Errorf("bad iv length %d", len(ivBytes))
+ }
block, err := aes.NewCipher(aesKey)
@@ -0,0 +1,15 @@
+package encryptor
+
+import (
+ "encoding/base64"
+ "testing"
+ "github.com/stretchr/testify/assert"
+)
+func TestGetCipherText_BadIV(t *testing.T) {
+ keyData := base64.StdEncoding.EncodeToString([]byte("1234567890123456"))
+ badData := base64.StdEncoding.EncodeToString([]byte("1"))
+ _, err := GetCipherText(keyData, badData, badData)
+ assert.Error(t, err)
+}