Body Changes Only
This commit is contained in:
@ -9,25 +9,28 @@ namespace Phares.Shared;
|
||||
|
||||
public static class RijndaelEncryption
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Change the Inputkey GUID when you use this code in your own program.
|
||||
/// Keep this inputkey very safe and prevent someone from decoding it some way!!
|
||||
/// Change the input key GUID when you use this code in your own program.
|
||||
/// Keep this input key very safe and prevent someone from decoding it some way!!
|
||||
/// Generated 2021-08-10
|
||||
/// </summary>
|
||||
internal const string _Inputkey = "970CCEF6-4307-4F6A-9AC8-377DADB889BD";
|
||||
internal const string _InputKey = "970CCEF6-4307-4F6A-9AC8-377DADB889BD";
|
||||
|
||||
/// <summary>
|
||||
/// Encrypt the given text and give the byte array back as a BASE64 string
|
||||
/// </summary>
|
||||
/// <param name="text">The text to encrypt</param>
|
||||
/// <param name="salt">The pasword salt</param>
|
||||
/// <param name="salt">The password salt</param>
|
||||
/// <returns>The encrypted text</returns>
|
||||
public static string Encrypt(string text, string salt)
|
||||
{
|
||||
string result;
|
||||
if (string.IsNullOrEmpty(text))
|
||||
throw new NullReferenceException(nameof(text));
|
||||
throw new ArgumentNullException(nameof(text));
|
||||
#pragma warning disable
|
||||
RijndaelManaged aesAlg = NewRijndaelManaged(salt);
|
||||
#pragma warning restore
|
||||
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
|
||||
MemoryStream msEncrypt = new();
|
||||
using (CryptoStream csEncrypt = new(msEncrypt, encryptor, CryptoStreamMode.Write))
|
||||
@ -46,7 +49,9 @@ public static class RijndaelEncryption
|
||||
{
|
||||
bool result;
|
||||
base64String = base64String.Trim();
|
||||
#pragma warning disable
|
||||
result = (base64String.Length % 4 == 0) && Regex.IsMatch(base64String, @"^[a-zA-Z0-9\+/]*={0,3}$", RegexOptions.None);
|
||||
#pragma warning restore
|
||||
return result;
|
||||
}
|
||||
|
||||
@ -54,16 +59,18 @@ public static class RijndaelEncryption
|
||||
/// Decrypts the given text
|
||||
/// </summary>
|
||||
/// <param name="cipherText">The encrypted BASE64 text</param>
|
||||
/// <param name="salt">The pasword salt</param>
|
||||
/// <param name="salt">The password salt</param>
|
||||
/// <returns>De gedecrypte text</returns>
|
||||
public static string Decrypt(string cipherText, string salt)
|
||||
{
|
||||
if (string.IsNullOrEmpty(cipherText))
|
||||
throw new NullReferenceException(nameof(cipherText));
|
||||
throw new ArgumentNullException(nameof(cipherText));
|
||||
if (!IsBase64String(cipherText))
|
||||
throw new Exception("The cipherText input parameter is not base64 encoded");
|
||||
string text;
|
||||
#pragma warning disable
|
||||
RijndaelManaged aesAlg = NewRijndaelManaged(salt);
|
||||
#pragma warning restore
|
||||
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
|
||||
byte[] cipher = Convert.FromBase64String(cipherText);
|
||||
using (MemoryStream msDecrypt = new(cipher))
|
||||
@ -76,19 +83,22 @@ public static class RijndaelEncryption
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// GetPersonName a new RijndaelManaged class and initialize it
|
||||
/// Create a new RijndaelManaged class and initialize it
|
||||
/// </summary>
|
||||
/// <param name="salt">The pasword salt</param>
|
||||
/// <param name="salt">The password salt</param>
|
||||
/// <returns></returns>
|
||||
#pragma warning disable
|
||||
private static RijndaelManaged NewRijndaelManaged(string salt)
|
||||
{
|
||||
if (salt == null)
|
||||
throw new NullReferenceException(nameof(salt));
|
||||
throw new ArgumentNullException(nameof(salt));
|
||||
byte[] saltBytes = Encoding.ASCII.GetBytes(salt);
|
||||
Rfc2898DeriveBytes key = new(_Inputkey, saltBytes);
|
||||
Rfc2898DeriveBytes key = new(_InputKey, saltBytes);
|
||||
RijndaelManaged aesAlg = new();
|
||||
#pragma warning restore
|
||||
aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8);
|
||||
aesAlg.IV = key.GetBytes(aesAlg.BlockSize / 8);
|
||||
return aesAlg;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user