BusinessMgr 3.0.2 .Net8

This commit is contained in:
Andy Howard
2025-11-20 17:17:40 +00:00
parent e3cb2b26fe
commit 796dd7cc2b
24 changed files with 142 additions and 136 deletions
@@ -1,10 +1,7 @@
using Sarand.Net.Standard.Base.App;
using Sarand.Net.Standard.Base.Core;
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace Sarand.Net.Standard.Base.Data
{
@@ -23,11 +20,11 @@ namespace Sarand.Net.Standard.Base.Data
// Either generate a 32 byte key or use any string and convert it into a key with CreateKey.
// NB The salt will remain the same here for each key generated - it does not need to be randomized as with hashing.
//-------------------------------------------------------------------
private static byte[] CreateKey (string password, int keyBytes = 32)
private static byte[] CreateKey(string password, int keyBytes = 32)
{
byte[] salt = new byte[] { 80, 70, 60, 50, 40, 30, 20, 10 };
int iterations = 300;
DeriveBytes keyGenerator = new Rfc2898DeriveBytes(password, salt, iterations);
DeriveBytes keyGenerator = new Rfc2898DeriveBytes(password, salt, iterations, HashAlgorithmName.SHA256);
return keyGenerator.GetBytes(keyBytes);
}
@@ -37,7 +34,7 @@ namespace Sarand.Net.Standard.Base.Data
// In AES, the IV is a randomizer, making sure that no two identical messages ever give the same encrypted output.
// The IV can be public and does not need to be hidden.
//-------------------------------------------------------------------
public static string Encrypt (this string clearValue, string encryptionKey)
public static string Encrypt(this string clearValue, string encryptionKey)
{
using (Aes aes = Aes.Create())
{
@@ -48,7 +45,7 @@ namespace Sarand.Net.Standard.Base.Data
}
}
//-------------------------------------------------------------------
public static byte[] Encrypt (string plainText, byte[] key, byte[] iv)
public static byte[] Encrypt(string plainText, byte[] key, byte[] iv)
{
if (plainText == null || plainText.Length <= 0) throw new ArgumentNullException($"{nameof(plainText)}");
if (key == null || key.Length <= 0) throw new ArgumentNullException($"{nameof(key)}");
@@ -79,7 +76,7 @@ namespace Sarand.Net.Standard.Base.Data
// assume that the IV is at the end of the string passed in.
// To decrypt a message encrypted by another function, the IV will have to be passed explicitly.
//-------------------------------------------------------------------
public static string Decrypt (this string encryptedValue, string encryptionKey)
public static string Decrypt(this string encryptedValue, string encryptionKey)
{
String[] valueParts = encryptedValue.Split(new String[] { AesIvDelimiter }, StringSplitOptions.None);
if (valueParts.Length != 2)
@@ -91,7 +88,7 @@ namespace Sarand.Net.Standard.Base.Data
return Decrypt(Convert.FromBase64String(valueParts[0]), CreateKey(encryptionKey), Convert.FromBase64String(valueParts[1]));
}
//-------------------------------------------------------------------
public static string Decrypt (byte[] cipherText, byte[] key, byte[] iv)
public static string Decrypt(byte[] cipherText, byte[] key, byte[] iv)
{
if (cipherText == null || cipherText.Length <= 0) throw new ArgumentNullException($"{nameof(cipherText)}");
if (key == null || key.Length <= 0) throw new ArgumentNullException($"{nameof(key)}");