This commit is contained in:
David Štaleker
2023-07-18 11:30:02 +02:00
parent a816459e5b
commit edab522e0e
12 changed files with 821 additions and 0 deletions

View File

@@ -0,0 +1,106 @@
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();
}
}
}