Jelajahi Sumber

解决 AesGcm 在 .NET 8 中报 Obsolete

roc 1 tahun lalu
induk
melakukan
e856c613e0
1 mengubah file dengan 6 tambahan dan 1 penghapusan
  1. 6 1
      src/Essensoft.Paylink.Security/AEAD_AES_256_GCM.cs

+ 6 - 1
src/Essensoft.Paylink.Security/AEAD_AES_256_GCM.cs

@@ -28,11 +28,16 @@ namespace Essensoft.Paylink.Security
                 throw new ArgumentNullException(nameof(key));
             }
 
+#if NET8_0_OR_GREATER
+            var keyBytes = Encoding.UTF8.GetBytes(key);
+            using (var aesGcm = new AesGcm(keyBytes, 16))
+#else
             using (var aesGcm = new AesGcm(Encoding.UTF8.GetBytes(key)))
+#endif
             {
                 var nonceBytes = Encoding.UTF8.GetBytes(nonce);
                 var ciphertextWithTagBytes = Convert.FromBase64String(ciphertext); // ciphertext 实际包含了 tag,即尾部16字节
-                var ciphertextBytes = ciphertextWithTagBytes[0..^16]; // 排除尾部16字节
+                var ciphertextBytes = ciphertextWithTagBytes[..^16]; // 排除尾部16字节
                 var tagBytes = ciphertextWithTagBytes[^16..]; // 获取尾部16字节
                 var plaintextBytes = new byte[ciphertextBytes.Length];
                 var associatedDataBytes = Encoding.UTF8.GetBytes(associatedData);