Preserve encryption compatibility on .NET 10

This commit is contained in:
Andy Howard
2026-09-16 13:51:30 +01:00
parent 8ef6e12788
commit 6989c5f9b6
6 changed files with 142 additions and 21 deletions
@@ -1,4 +1,4 @@
using Sarand.Net.Standard.Base.App;
using Sarand.Net.Standard.Base.App;
using System;
using System.IO;
using System.Security.Cryptography;
@@ -24,8 +24,7 @@ namespace Sarand.Net.Standard.Base.Data
{
byte[] salt = new byte[] { 80, 70, 60, 50, 40, 30, 20, 10 };
int iterations = 300;
DeriveBytes keyGenerator = new Rfc2898DeriveBytes(password, salt, iterations, HashAlgorithmName.SHA256);
return keyGenerator.GetBytes(keyBytes);
return Rfc2898DeriveBytes.Pbkdf2(password, salt, iterations, HashAlgorithmName.SHA256, keyBytes);
}
//-------------------------------------------------------------------
@@ -1,4 +1,4 @@
using Sarand.Net.Standard.Base.Core;
using Sarand.Net.Standard.Base.Core;
using Sarand.Net.Standard.Base.Utils;
using System;
using System.Collections.Generic;
@@ -34,7 +34,7 @@ namespace Sarand.Net.Standard.Base.Helpers
}
public static String EncodePlainKey(String plainKey, int keyLength = 32, Encoding encoding = null)
{
if (StringUtil.IsEmpty(plainKey))
if (String.IsNullOrWhiteSpace(plainKey))
throw new NotSupportedException("Plain key must not be null/blank");
// Ensure key if 32 bytes long.
while (StringUtil.GetLength(plainKey) < keyLength)
@@ -56,7 +56,7 @@ namespace Sarand.Net.Standard.Base.Helpers
}
public static string InitSymmetricEncryptionIV(String key)
{
Aes cipher = CreateCipher(key);
using Aes cipher = CreateCipher(key);
var IVBase64 = Convert.ToBase64String(cipher.IV);
return IVBase64;
}
@@ -83,7 +83,6 @@ namespace Sarand.Net.Standard.Base.Helpers
cipher.Padding = PaddingMode.ISO10126;
byte[] rawKey = Convert.FromBase64String(keyBase64);
String rawKeyValue = System.Text.Encoding.Default.GetString(rawKey);
cipher.Key = rawKey;
return cipher;
@@ -99,10 +98,10 @@ namespace Sarand.Net.Standard.Base.Helpers
//-------------------------------------------------------------------
public static string Encrypt(string text, string IV, string key)
{
Aes cipher = CreateCipher(key);
using Aes cipher = CreateCipher(key);
cipher.IV = Convert.FromBase64String(IV);
ICryptoTransform cryptTransform = cipher.CreateEncryptor();
using ICryptoTransform cryptTransform = cipher.CreateEncryptor();
byte[] plaintext = Encoding.UTF8.GetBytes(text);
byte[] cipherText = cryptTransform.TransformFinalBlock(plaintext, 0, plaintext.Length);
@@ -112,10 +111,10 @@ namespace Sarand.Net.Standard.Base.Helpers
//-------------------------------------------------------------------
public static string Decrypt(string encryptedText, string IV, string key)
{
Aes cipher = CreateCipher(key);
using Aes cipher = CreateCipher(key);
cipher.IV = Convert.FromBase64String(IV);
ICryptoTransform cryptTransform = cipher.CreateDecryptor();
using ICryptoTransform cryptTransform = cipher.CreateDecryptor();
byte[] encryptedBytes = Convert.FromBase64String(encryptedText);
byte[] plainBytes = cryptTransform.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length);