You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用C#解密Go语言通过AES-GCM加密的字符串?

问题:Go AES-GCM加密内容的C#解密实现问题

Go端加密/解密关键信息

  • AES加解密密码:this-is-a-test-passphrase
  • 明文:text to encrypt hello world 123
  • 密文:94b681ef29d9a6d7-e6fa36c4c00977de1745fc63-a1ad0481bdbeeaa02c013a2dce82520ddd762355e18f1e2f20c0ea9d001ece24e9b8216ed4b9c6a06e1ef34c953f80

Go端解密代码

func AppDecryptImpl(passphrase, ciphertext string) string {
    arr := strings.Split(ciphertext, "-")
    salt, _ := hex.DecodeString(arr[0])
    iv, _ := hex.DecodeString(arr[1])
    data, _ := hex.DecodeString(arr[2])
    key, _ := appDeriveKey(passphrase, salt)
    b, _ := aes.NewCipher(key)
    aesgcm, _ := cipher.NewGCM(b)
    data, _ = aesgcm.Open(nil, iv, data, nil)
    return string(data)
}

func appDeriveKey(passphrase string, salt []byte) ([]byte, []byte) {
    if salt == nil {
        salt = make([]byte, 8)
        rand.Read(salt)
    }
    return pbkdf2.Key([]byte(passphrase), salt, 1000, 32, sha256.New), salt
}

Go端加密代码

func AppEncryptImpl(passphrase string, plaintext string) string {
    key, salt := appDeriveKey(passphrase, nil)
    iv := make([]byte, 12)
    rand.Read(iv)
    b, _ := aes.NewCipher(key)
    aesgcm, _ := cipher.NewGCM(b)
    data := aesgcm.Seal(nil, iv, []byte(plaintext), nil)
    return hex.EncodeToString(salt) + "-" + hex.EncodeToString(iv) + "-" + hex.EncodeToString(data)
}

已尝试的C#解密方案及错误

  • 使用System.Security.Cryptography命名空间:报IV尺寸错误
  • .NET 5修改版:错误相同
  • 使用Bouncy Castle库:报“mac check in GCM failed”错误
  • 使用Rfc2898DeriveBytes方法:报“The computed authentication tag did not match the input authentication tag”错误

疑问

  1. 当前思路是否正确?
  2. 有没有其他方法在C#中解密该AES-GCM密文?
  3. 如何解决这些错误?

内容的提问来源于stack exchange,提问作者Ayan S

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 11:10:13