公告:魔扣目录网为广大站长提供免费收录网站服务,提交前请做好本站友链:【 网站目录:https://www.morko.net 】, 免友链快审服务(50元/站),

点击这里在线咨询客服
新站提交
  • 网站:51998
  • 待审:31
  • 小程序:12
  • 文章:1030137
  • 会员:747

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);
}

登录后复制

分享到:
标签:Go语言 安全传输
用户无头像

网友整理

注册时间:

网站:5 个   小程序:0 个  文章:12 篇

  • 51998

    网站

  • 12

    小程序

  • 1030137

    文章

  • 747

    会员

赶快注册账号,推广您的网站吧!
最新入驻小程序

数独大挑战2018-06-03

数独一种数学游戏,玩家需要根据9

答题星2018-06-03

您可以通过答题星轻松地创建试卷

全阶人生考试2018-06-03

各种考试题,题库,初中,高中,大学四六

运动步数有氧达人2018-06-03

记录运动步数,积累氧气值。还可偷

每日养生app2018-06-03

每日养生,天天健康

体育训练成绩评定2018-06-03

通用课目体育训练成绩评定