解密的值由 Go 中不需要的字符组成

问题描述 投票:0回答:1

我尝试使用 JavaScript 代码进行编码,解密发生在 Golang 中。 当我使用 Javascript 代码解密时,解密的输出是正确的。 我已将 Javascript 解密函数复制到 Golang 中,但是当在 Golang 中进行解密时,我看到一些不需要的字符以及我想要修剪或删除的解密输出。 我该怎么办?

我有用golang编写的解密逻辑。

package main

import (
    "crypto/aes"
    "crypto/cipher"
    "encoding/hex"
    "fmt"
)

func main() {

    encKey := "c38036f65157cb6db0e8fd855aa28ada074be71917d1c8eedc2ae4d85e3c9da6"
    iv := "79b67e539e7fcaefa7abf167de5c06ed"
    cipherText := "c02eccfc514a0b7fae830586dd56e0fcebb81fc49f41fa6dedf099c3645793bef7ec7075eca30063f9c0ef395d5ee2d44e4f3490114280abb7cf86d6eb525e2ec9bd2b781388986480f8b3df95f7b10e"

    encKeyDecoded, err := hex.DecodeString(encKey)
    if err != nil {
        panic(err)
    }
    cipherTextDecoded, err := hex.DecodeString(cipherText)
    if err != nil {
        panic(err)
    }
    ivDecoded, err := hex.DecodeString(iv)
    if err != nil {
        panic(err)
    }
    block, err := aes.NewCipher([]byte(encKeyDecoded))
    if err != nil {
        panic(err)
    }

    mode := cipher.NewCBCDecrypter(block, []byte(ivDecoded))

    mode.CryptBlocks([]byte(cipherTextDecoded), []byte(cipherTextDecoded))

    fmt.Println(string(cipherTextDecoded))
}

同样可以在text找到 解密参考线程:text

输出是:

*▒▒?▒j-▒▒▒▒session={"user":{"email":"[email protected]","username":"ishanjain28"}} *▒▒?▒j-▒▒▒▒

从输出中可以看出,有一些额外不需要的字符,我想从解密的输出中删除它们。

go encryption cryptography aes
1个回答
0
投票

将解密的文本传递给下面的函数删除了解密文本末尾和开头不需要的字符。

cipherTextDecoded = PKCS7Unpad(cipherTextDecoded, len(ivDecoded))

func PKCSUnPadding(src []byte, lenDecIv int) []byte {
length := len(src)
unpadding := int(src[length-1])
return src[lenDecIv:(length - unpadding)]
}
© www.soinside.com 2019 - 2024. All rights reserved.