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中文網其他相關文章!

Gohandlesinterfacesandtypeassertionseffectively,enhancingcodeflexibilityandrobustness.1)Typeassertionsallowruntimetypechecking,asseenwiththeShapeinterfaceandCircletype.2)Typeswitcheshandlemultipletypesefficiently,usefulforvariousshapesimplementingthe

Go語言的錯誤處理通過errors.Is和errors.As函數變得更加靈活和可讀。 1.errors.Is用於檢查錯誤是否與指定錯誤相同,適用於錯誤鏈的處理。 2.errors.As不僅能檢查錯誤類型,還能將錯誤轉換為具體類型,方便提取錯誤信息。使用這些函數可以簡化錯誤處理邏輯,但需注意錯誤鏈的正確傳遞和避免過度依賴以防代碼複雜化。

tomakegoapplicationsRunfasterandMorefly,useProflingTools,leverageConCurrency,andManageMoryfectily.1)usepprofforcpuorforcpuandmemoryproflingtoidentifybottlenecks.2)upitizegorizegoroutizegoroutinesandchannelstoparalletaparelalyizetasksandimproverperformance.3)

go'sfutureisbrightwithtrendslikeMprikeMprikeTooling,仿製藥,雲 - 納蒂維德象,performanceEnhancements,andwebassemblyIntegration,butchallengeSinclainSinClainSinClainSiNgeNingsImpliCityInsImplicityAndimimprovingingRornhandRornrorlling。

goroutinesarefunctionsormethodsthatruncurranceingo,啟用效率和燈威量。 1)shememanagedbodo'sruntimemultimusingmultiplexing,允許千sstorunonfewerosthreads.2)goroutinessimproverentimensImproutinesImproutinesImproveranceThroutinesImproveranceThrountinesimproveranceThroundinesImproveranceThroughEasySytaskParallowalizationAndeff

purposeoftheInitfunctionoIsistoInitializeVariables,setUpConfigurations,orperformneccesSetarySetupBeforEtheMainFunctionExeCutes.useInitby.UseInitby:1)placingitinyourcodetorunautoamenationally oneraty oneraty oneraty on inity in ofideShortAndAndAndAndForemain,2)keepitiTshortAntAndFocusedonSimImimpletasks,3)

Gointerfacesaremethodsignaturesetsthattypesmustimplement,enablingpolymorphismwithoutinheritanceforcleaner,modularcode.Theyareimplicitlysatisfied,usefulforflexibleAPIsanddecoupling,butrequirecarefulusetoavoidruntimeerrorsandmaintaintypesafety.

在Go中使用recover()函數可以從panic中恢復。具體方法是:1)在defer函數中使用recover()捕獲panic,避免程序崩潰;2)記錄詳細的錯誤信息以便調試;3)根據具體情況決定是否恢復程序執行;4)謹慎使用,以免影響性能。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

SublimeText3漢化版
中文版,非常好用

Dreamweaver Mac版
視覺化網頁開發工具