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);
}
登錄后復制






