encrypt aes in node js and decrypt in go

Viewed 80

So, in node js, I use this code to encrypt

exports.encryptCrypto = string => string ? crypto.AES.encrypt(string.toString(), process.env.CRYPTOSECRETKEY).toString() : ""

and in go I use this function to decrypt

func DecryptCrypto(str string) string {
    ciphertext := []byte(str)

    key := []byte(os.Getenv("CRYPTOSECRETKEY"))
    c, err := aes.NewCipher(key)
    if err != nil {
        panic(err)
    }
    gcm, _ := cipher.NewGCM(c)

    nonceSize := gcm.NonceSize()
    if len(ciphertext) < nonceSize {
        panic("ciphertext size is less than nonceSize")
    }

    nonce, ciphertext := ciphertext[:nonceSize], ciphertext[nonceSize:]
    plaintext, err := gcm.Open(nil, nonce, ciphertext, nil)

    return string(plaintext)
}

but I got cipher: message authentication failed error instead (when calling gcm.Open, is there anything should I fix?

0 Answers
Related