卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章35148本站已运行394

使用 AES-GSM 方法解密在 GO 中编码的 C# 字符串

使用 aes-gsm 方法解密在 go 中编码的 c# 字符串

php小编西瓜为您介绍一种在GO语言中解密C#字符串的方法——AES-GSM。AES-GSM是一种高级加密标准,它结合了AES(高级加密标准)和GSM(全球系统移动通信)的优势。通过使用AES-GSM方法,我们可以有效地解密在GO中编码的C#字符串,从而实现数据的安全传输和保护。本文将详细介绍AES-GSM的原理和使用步骤,帮助读者轻松掌握这一加密解密技术。

问题内容

我有一个在 go 中经过 aes-gcm 加密的字符串及其密码短语,并尝试在 c# 中对其进行解密。但是,我无法找到正确的方法来在 c# 中对其进行解密。 我收到的错误提到 iv 的大小,块的长度不适合 c# 解密算法。以下是 go 中的值:


aes encr/decr passphrase:  this-is-a-test-passphrase

input string:  text to encrypt hello world 123

encrypted string:  94b681ef29d9a6d7-e6fa36c4c00977de1745fc63-a1ad0481bdbeeaa02c013a2dce82520ddd762355e18f1e2f20c0ea9d001ece24e9b8216ed4b9c6a06e1ef34c953f80

go代码:https://go.dev/play/p/jn8ie61ntzw

这是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# 中复制相同的解密登录,因此它将能够解密并生成最终的字符串。

我在 c# 中尝试了几种解密逻辑,它们可以在这里找到:

  • https://dotnetfiddle.net/32sb5m 此函数使用 system.security.cryptography 命名空间,但会导致 iv 大小错误。

  • https://dotnetfiddle.net/wxkuyr 上述针对 .net 5 的修改版本会产生相同的结果

  • https://dotnetfiddle.net/6iftps 使用充气城堡库会导致“gcm 中的 mac 检查失败”错误

  • https://dotnetfiddle.net/8mjs3g 使用 rfc2898derivebytes 方法的另一种方法会产生错误,提示“计算的身份验证标记与输入身份验证标记不匹配”

当前使用的方法是否正确,或者是否有其他方法可以在 c# 中解密 aes-gcm?当涉及到 c# 时,可以采取什么措施来绕过这些错误?

解决方法

您已经接近最后一个代码了。 go 将身份验证标签附加到生成的密文的末尾。您在这里正确提取它:

// extract the tag from the encrypted byte array
byte[] tag = new byte[16];
array.copy(encrypteddata, encrypteddata.length - 16, tag, 0, 16);

但是,您继续将具有实际加密文本+身份验证标记的数组视为仅包含加密文本。要修复,请将其也提取出来:

public static void Main() {
    // This is the encrypted string that you provided
    string encryptedString = "a6c0952b78967559-2953e738b9b005028bf4f6c0-7b8464d1ed75bc38b4503f6c8d25d6bfc22a19cc1a8a92bc6faa1ed6cd837b97072bc8e16fd95b6cfca67fccbad8fc";

    // This is the passphrase that you provided
    string passphrase = "this-is-a-test-passphrase";

    string[] splitStrs = encryptedString.Split('-');

    byte[] salt = Convert.FromHexString(splitStrs[0]);
    byte[] iv = Convert.FromHexString(splitStrs[1]);
    // this is encrypted data + tag
    byte[] encryptedDataWithTag = Convert.FromHexString(splitStrs[2]);
    // Extract the tag from the encrypted byte array
    byte[] tag = new byte[16];
    // But also extract actual encrypted data
    byte[] encryptedData = new byte[encryptedDataWithTag.Length - 16];
    Array.Copy(encryptedDataWithTag, 0, encryptedData, 0, encryptedData.Length);
    Array.Copy(encryptedDataWithTag, encryptedDataWithTag.Length - 16, tag, 0, 16);
    byte[] key = new Rfc2898DeriveBytes(passphrase, salt, 1000, HashAlgorithmName.SHA256).GetBytes(32);
    // Create an AesGcm object
    AesGcm aesGcm = new AesGcm(key);
    int textLength = encryptedData.Length;
    // Decrypt the ciphertext
    byte[] plaintext = new byte[textLength];
    aesGcm.Decrypt(iv, encryptedData, tag, plaintext);
    // Convert the plaintext to a string and print it
    string decryptedString = Encoding.UTF8.GetString(plaintext);
    Console.WriteLine(decryptedString);
}
卓越飞翔博客
上一篇: 如何将 TLS 1.2 与 MySql Go 驱动程序一起使用?
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏