golang-module / dongle

A simple, semantic and developer-friendly golang package for encoding&decoding and encryption&decryption
https://pkg.go.dev/github.com/golang-module/dongle
MIT License
875 stars 67 forks source link

Decrypt not handle illegal string error #10

Closed JiajunDou closed 1 year ago

JiajunDou commented 1 year ago

Hello,

I encountered an issue with the following code:

func Decode(s string) (string, error) {
    cipher := dongle.NewCipher()
    cipher.SetMode(dongle.CBC)      // CBC、CFB、OFB、CTR、ECB
    cipher.SetPadding(dongle.PKCS7) // No、Zero、PKCS5、PKCS7
    cipher.SetKey(c.AUTH_KEY)       // key must be 16, 24 or 32 bytes
    cipher.SetIV(c.IV_KEY)
    e := dongle.Decode.FromString(s).ByBase62()
    if e.Error != nil {
        return "", e.Error
    }
    raw := e.ToBytes()

    f := dongle.Decrypt.FromRawBytes(raw).ByAes(cipher)
    if f.Error != nil {
        return "", f.Error
    }
    return f.ToString(), nil
}

_, mustError := Decode("123")
if mustError != nil {
    fmt.Println(mustError.Error())
}

golang version: 1.19

dongle version: 0.2.0

I expected to get:

Error.Error()

But I actually get:

 (*cbcDecrypter).CryptBlocks: panic("crypto/cipher: input not full blocks")
go/pkg/mod/github.com/golang-module/dongle@v0.2.0/cipher.go:126 (0xddbec1)

Thanks!

gouguoyin commented 1 year ago

The cause of the bug has been found and will fix in the next version

image
gouguoyin commented 1 year ago

Fixed in v0.2.1