php的des-ecb加密,go中如何解密?麻烦各位大佬赐教

下面是PHP加密的代码

    /**
     * des-ecb加密
     * @param string  $data 要被加密的数据
     * @param string  $key 加密密钥(64位的字符串)
     */
    function des_ecb_encrypt($data, $key){
        return openssl_encrypt ($data, 'des-ecb', $key);
    }

请问在go中如何解密?
我在网上找了一些DEC ECB的解密方法,比如

func DesDecrypt(src, key []byte) ([]byte, error) {
    block, err := des.NewCipher(key)
    if err != nil {
        return nil, err
    }
    out := make([]byte, len(src))
    dst := out
    bs := block.BlockSize()
    if len(src)%bs != 0 {
        return nil, errors.New("crypto/cipher: input not full blocks")
    }
    for len(src) > 0 {
        block.Decrypt(dst, src[:bs])
        src = src[bs:]
        dst = dst[bs:]
    }
    // out = ZeroUnPadding(out)
    out = PKCS5UnPadding(out)
    return out, nil
}

func PKCS5UnPadding(origData []byte) []byte {
    length := len(origData)
    unpadding := int(origData[length-1])
    return origData[:(length - unpadding)]
}

始终会提示crypto/cipher: input not full blocks

谢谢deatil的提示!之前已经做过Base64Decode了,补充一下代码:

    key := "ta***n"
    cryptText := Base64Decode("TUS0+wONGnw=")

    fmt.Println([]byte(cryptText))
    newplaintext, err := des3.DesDecrypt([]byte(cryptText), []byte(key))
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println("DES的CBC模式解密完:", newplaintext)
    fmt.Println("DES的CBC模式解密完:", string(newplaintext))

这个是上面代码运行后打印的信息
php的des-ecb加密,go中如何解密?麻烦各位大佬赐教
这个是在网站上做解密的结果
php的des-ecb加密,go中如何解密?麻烦各位大佬赐教


谢谢deatil的包github.com/deatil/go-cryptobin
打印出数据了,赞!

cyptde := cryptobin.
        FromBase64String("TUS0+wONGnw=").
        SetIv("").
        SetKey("tan**han").
        Des().
        ECB().
        PKCS7Padding().
        Decrypt().
        ToString()
    fmt.Println("deatil的方法:", string(cyptde))
最佳答案

该说问题是php生成的加密值是base64的值,go的话需要先base64decode还原为加密后的数据再传到你这个方法里

2年前 评论
deatil (作者) 2年前
deatil (作者) 2年前
宋高峰 (楼主) 2年前
宋高峰 (楼主) 2年前
讨论数量: 7

可以看下这个库 github.com/golang-module/dongle

2年前 评论
宋高峰 (楼主) 2年前

该说问题是php生成的加密值是base64的值,go的话需要先base64decode还原为加密后的数据再传到你这个方法里

2年前 评论
deatil (作者) 2年前
deatil (作者) 2年前
宋高峰 (楼主) 2年前
宋高峰 (楼主) 2年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!