首頁  >  文章  >  後端開發  >  使用 AES-GSM 方法解密在 GO 中編碼的 C# 字串

使用 AES-GSM 方法解密在 GO 中編碼的 C# 字串

PHPz
PHPz轉載
2024-02-10 09:39:091119瀏覽

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

以上是使用 AES-GSM 方法解密在 GO 中編碼的 C# 字串的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:stackoverflow.com。如有侵權,請聯絡admin@php.cn刪除