Files
David Štaleker ca229fbd89 nove verzije
2024-08-02 11:00:59 +02:00

134 lines
4.8 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace ResevalnaScanner.Classes
{
internal static class Encryption
{
public enum Type
{
General,
Licence,
}
public static string AesEncrypt(this string iPlain, Type iType = Type.General, string iKey = "", string iSalt = "")
{
if (iPlain.Length > 16 && iPlain.Length % 16 != 0)
{
var size = ((iPlain.Length / 16) + 1) * 16;
iPlain += new string(' ', size - iPlain.Length);
}
switch (iType)
{
case Type.General:
iKey = "DEWSCYUSBP2AQ6JnMc_InfosysPublisher_S9Gj3GU4hchg7J38zZ";
iSalt = "P4TqMkZ3FZd6Y5K5uNykmngCATDpP7PrxnACj2sFkfc6";
break;
case Type.Licence:
iKey = "2D849KZ6RjpQCG_ResevalnaLicense_crKwBTjnskwycCvy7N";
iSalt = "kUt7E6ngrYqA7watQ8YMPYNdzYFgLcCpuuchS96SZwC6";
break;
}
if (string.IsNullOrEmpty(iPlain))
{
return "";
}
var saltByes = Encoding.ASCII.GetBytes(iSalt);
var key = new Rfc2898DeriveBytes(iKey, saltByes);
var aesAlgorithm = Aes.Create();
aesAlgorithm.KeySize = 256;
aesAlgorithm.Key = key.GetBytes(aesAlgorithm.KeySize / 8);
aesAlgorithm.IV = key.GetBytes(aesAlgorithm.BlockSize / 8);
var msEncrypt = new MemoryStream();
using (var encrypt = aesAlgorithm.CreateEncryptor(aesAlgorithm.Key, aesAlgorithm.IV))
using (var csEncrypt = new CryptoStream(msEncrypt, encrypt, CryptoStreamMode.Write))
{
using (var swEncrypt = new StreamWriter(csEncrypt))
{
swEncrypt.Write(iPlain);
}
}
return Convert.ToBase64String(msEncrypt.ToArray());
}
public static string AesDecrypt(this string iCipherText, Type iType = Type.General, string iKey = "", string iSalt = "")
{
switch (iType)
{
case Type.General:
iKey = "DEWSCYUSBP2AQ6JnMc_InfosysPublisher_S9Gj3GU4hchg7J38zZ";
iSalt = "P4TqMkZ3FZd6Y5K5uNykmngCATDpP7PrxnACj2sFkfc6";
break;
case Type.Licence:
iKey = "2D849KZ6RjpQCG_ResevalnaLicense_crKwBTjnskwycCvy7N";
iSalt = "kUt7E6ngrYqA7watQ8YMPYNdzYFgLcCpuuchS96SZwC6";
break;
}
if (string.IsNullOrEmpty(iCipherText))
{
return "";
}
var saltByes = Encoding.ASCII.GetBytes(iSalt);
var key = new Rfc2898DeriveBytes(iKey, saltByes);
var aesAlgorithm = Aes.Create();
aesAlgorithm.KeySize = 256;
aesAlgorithm.Key = key.GetBytes(aesAlgorithm.KeySize / 8);
aesAlgorithm.IV = key.GetBytes(aesAlgorithm.BlockSize / 8);
var cipherTextBytes = Convert.FromBase64String(iCipherText);
var plainTextBytes = new byte[iCipherText.Length];
var byteCount = 0;
using (var decrypt = aesAlgorithm.CreateDecryptor(aesAlgorithm.Key, aesAlgorithm.IV))
using (var msDecrypt = new MemoryStream(cipherTextBytes))
using (var csDecrypt = new CryptoStream(msDecrypt, decrypt, CryptoStreamMode.Read))
{
byteCount = csDecrypt.Read(plainTextBytes, 0, plainTextBytes.Length);
}
return Encoding.UTF8.GetString(plainTextBytes, 0, byteCount).Trim();
}
public const int SALT_SIZE = 24; // size in bytes
public const int HASH_SIZE = 256; // size in bytes
public const int ITERATIONS = 20000; // number of pbkdf2 iterations
public static (byte[] Hash, byte[] Salt) CreatePasswordHash(string iText, byte[]? iSalt = null)
{
byte[] salt;
if (iSalt == null)
{
salt = new byte[SALT_SIZE];
using var rng = RandomNumberGenerator.Create();
rng.GetBytes(salt);
}
else
{
salt = iSalt;
}
var pbkdf2 = new Rfc2898DeriveBytes(iText, salt, ITERATIONS, HashAlgorithmName.SHA256);
return (pbkdf2.GetBytes(HASH_SIZE), salt);
}
public static bool CompareByteArrays(byte[] iArray, byte[] iArrayToCompare)
{
return iArray.Length == iArrayToCompare.Length && !iArray.Where((t, i) => t != iArrayToCompare[i]).ToList().Any();
}
}
}