Skip to content

Commit

Permalink
解决 AesGcm 在 .NET 8 中报 Obsolete
Browse files Browse the repository at this point in the history
  • Loading branch information
roc916 committed Jun 25, 2024
1 parent 9746027 commit e856c61
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Essensoft.Paylink.Security/AEAD_AES_256_GCM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,16 @@ public static string Decrypt(string nonce, string ciphertext, string associatedD
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);
Expand Down

0 comments on commit e856c61

Please sign in to comment.