26ff8d9b4f
Set up repository with legacy .NET Framework 4.8 source (OLD/), new .NET 10 Blazor solution (NEW/), OpenSpec specifications, documentation, and project configuration.
141 lines
5.8 KiB
C#
Executable File
141 lines
5.8 KiB
C#
Executable File
using System;
|
|
using System.IO;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
|
|
namespace DataModel.Helpers
|
|
{
|
|
/// <summary>
|
|
/// Helper methods for encryption / decryption
|
|
/// </summary>
|
|
public class EncryptionHelper
|
|
{
|
|
/// <summary>
|
|
/// Default salt value
|
|
/// </summary>
|
|
private const string DEFAULT_SALT = ")kjdnl3k1jh234a9";
|
|
|
|
/// <summary>
|
|
/// Default hashing algorithm to use
|
|
/// </summary>
|
|
private const string DEFAULT_HASH_ALGORITHM = "SHA1";
|
|
|
|
/// <summary>
|
|
/// Default number of iterations to do
|
|
/// </summary>
|
|
private const int DEFAULT_PASSWORD_ITERATION = 2;
|
|
|
|
/// <summary>
|
|
/// Default initialization vector
|
|
/// </summary>
|
|
private const string DEFAULT_INITIAL_VECTOR = "X8pgVu239uOjdKH1";
|
|
|
|
/// <summary>
|
|
/// Default encryption keysize
|
|
/// </summary>
|
|
private const int DEFAULT_KEYSIZE = 256;
|
|
|
|
/// <summary>
|
|
/// Encrypts a string
|
|
/// </summary>
|
|
/// <param name="plainText">Text to be encrypted</param>
|
|
/// <param name="password">Password to encrypt with</param>
|
|
/// <param name="salt">Salt to encrypt with</param>
|
|
/// <param name="hashAlgorithm">Can be either SHA1 or MD5</param>
|
|
/// <param name="passwordIterations">Number of iterations to do</param>
|
|
/// <param name="initialVector">Needs to be 16 ASCII characters long</param>
|
|
/// <param name="keySize">Can be 128, 192, or 256</param>
|
|
/// <returns>An encrypted string</returns>
|
|
public static string Encrypt(string plainText, string password, string salt = DEFAULT_SALT, string hashAlgorithm = DEFAULT_HASH_ALGORITHM, int passwordIterations = DEFAULT_PASSWORD_ITERATION, string initialVector = DEFAULT_INITIAL_VECTOR, int keySize = DEFAULT_KEYSIZE)
|
|
{
|
|
if (string.IsNullOrEmpty(plainText))
|
|
{
|
|
return "";
|
|
}
|
|
|
|
byte[] initialVectorBytes = Encoding.ASCII.GetBytes(initialVector);
|
|
byte[] saltValueBytes = Encoding.ASCII.GetBytes(salt);
|
|
byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
|
|
|
|
PasswordDeriveBytes derivedPassword = new PasswordDeriveBytes(password, saltValueBytes, hashAlgorithm, passwordIterations);
|
|
byte[] keyBytes = derivedPassword.GetBytes(keySize / 8);
|
|
|
|
RijndaelManaged symmetricKey = new RijndaelManaged
|
|
{
|
|
Mode = CipherMode.CBC
|
|
};
|
|
byte[] cipherTextBytes = null;
|
|
|
|
using (ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initialVectorBytes))
|
|
{
|
|
using (MemoryStream memStream = new MemoryStream())
|
|
{
|
|
using (CryptoStream cryptoStream = new CryptoStream(memStream, encryptor, CryptoStreamMode.Write))
|
|
{
|
|
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
|
|
cryptoStream.FlushFinalBlock();
|
|
cipherTextBytes = memStream.ToArray();
|
|
memStream.Close();
|
|
cryptoStream.Close();
|
|
}
|
|
}
|
|
}
|
|
|
|
symmetricKey.Clear();
|
|
|
|
return Convert.ToBase64String(cipherTextBytes);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Decrypts a string
|
|
/// </summary>
|
|
/// <param name="encrypted">Text to be decrypted</param>
|
|
/// <param name="password">Password to decrypt with</param>
|
|
/// <param name="salt">Salt to decrypt with</param>
|
|
/// <param name="hashAlgorithm">Can be either SHA1 or MD5</param>
|
|
/// <param name="passwordIterations">Number of iterations to do</param>
|
|
/// <param name="initialVector">Needs to be 16 ASCII characters long</param>
|
|
/// <param name="keySize">Can be 128, 192, or 256</param>
|
|
/// <returns>A decrypted string</returns>
|
|
public static string Decrypt(string encrypted, string password, string salt = DEFAULT_SALT, string hashAlgorithm = DEFAULT_HASH_ALGORITHM, int passwordIterations = DEFAULT_PASSWORD_ITERATION, string initialVector = DEFAULT_INITIAL_VECTOR, int keySize = DEFAULT_KEYSIZE)
|
|
{
|
|
if (string.IsNullOrEmpty(encrypted))
|
|
{
|
|
return "";
|
|
}
|
|
|
|
byte[] initialVectorBytes = Encoding.ASCII.GetBytes(initialVector);
|
|
byte[] saltValueBytes = Encoding.ASCII.GetBytes(salt);
|
|
byte[] cipherTextBytes = Convert.FromBase64String(encrypted);
|
|
|
|
PasswordDeriveBytes derivedPassword = new PasswordDeriveBytes(password, saltValueBytes, hashAlgorithm, passwordIterations);
|
|
byte[] keyBytes = derivedPassword.GetBytes(keySize / 8);
|
|
|
|
RijndaelManaged symmetricKey = new RijndaelManaged
|
|
{
|
|
Mode = CipherMode.CBC
|
|
};
|
|
|
|
byte[] plainTextBytes = new byte[cipherTextBytes.Length];
|
|
int byteCount = 0;
|
|
|
|
using (ICryptoTransform decrypt = symmetricKey.CreateDecryptor(keyBytes, initialVectorBytes))
|
|
{
|
|
using (MemoryStream memStream = new MemoryStream(cipherTextBytes))
|
|
{
|
|
using (CryptoStream cryptoStream = new CryptoStream(memStream, decrypt, CryptoStreamMode.Read))
|
|
{
|
|
|
|
byteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
|
|
memStream.Close();
|
|
cryptoStream.Close();
|
|
}
|
|
}
|
|
}
|
|
|
|
symmetricKey.Clear();
|
|
|
|
return Encoding.UTF8.GetString(plainTextBytes, 0, byteCount);
|
|
}
|
|
}
|
|
} |