219 lines
6.7 KiB
C#
219 lines
6.7 KiB
C#
using FileProcessClient.domain;
|
|
using System.Runtime.InteropServices;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Xml.Linq;
|
|
|
|
namespace edu.huvtc.utils
|
|
{
|
|
internal class CommonUtils
|
|
{
|
|
[DllImport("kernel32")]
|
|
private static extern long WritePrivateProfileString(string name, string key, string val,
|
|
string filePath);
|
|
[DllImport("kernel32")]
|
|
private static extern int GetPrivateProfileString(string section, string key, string def,
|
|
StringBuilder retVal, int size,
|
|
string filePath);
|
|
|
|
private const int AesBlockByteSize = 128 / 8;
|
|
|
|
private const int PasswordSaltByteSize = 128 / 8;
|
|
|
|
private const int PasswordByteSize = 256 / 8;
|
|
|
|
private const int PasswordIterationCount = 100_000;
|
|
|
|
private static readonly Encoding StringEncoding = Encoding.UTF8;
|
|
|
|
private static readonly RandomNumberGenerator Random = RandomNumberGenerator.Create();
|
|
|
|
private static String password = "from huvtc passwords 123412;,[p;[; 172634812";
|
|
|
|
private static string INIPath = Convert.ToString(System.AppDomain.CurrentDomain.BaseDirectory) + "Config.ini";
|
|
|
|
private static string Token = "";
|
|
|
|
private static string serverUrl = "";
|
|
|
|
private static User user;
|
|
|
|
public static User getUser()
|
|
{
|
|
return user;
|
|
}
|
|
|
|
public static void setUser(User data)
|
|
{
|
|
user = data;
|
|
}
|
|
|
|
|
|
public static void setToken(string token)
|
|
{
|
|
writeToIni("security", "token", token);
|
|
Token = token;
|
|
}
|
|
|
|
public static string getToken()
|
|
{
|
|
if (Token == null || Token == "")
|
|
{
|
|
Token = readFromFile("security", "token");
|
|
}
|
|
return Token;
|
|
}
|
|
|
|
public static void writeToIni(string name, string key, string value)
|
|
{
|
|
WritePrivateProfileString(name, key, value, INIPath);
|
|
}
|
|
|
|
public static string readFromFile(string name, string key)
|
|
{
|
|
StringBuilder builder = new StringBuilder(255);
|
|
int ini = GetPrivateProfileString(name, key, "", builder, 255, INIPath);
|
|
return builder.ToString();
|
|
}
|
|
|
|
|
|
public static byte[] EncryptString(string toEncrypt)
|
|
{
|
|
using (var aes = Aes.Create())
|
|
{
|
|
var keySalt = GenerateRandomBytes(PasswordSaltByteSize);
|
|
var key = GetKey(keySalt);
|
|
var iv = GenerateRandomBytes(AesBlockByteSize);
|
|
|
|
using (var encryptor = aes.CreateEncryptor(key, iv))
|
|
{
|
|
var plainText = StringEncoding.GetBytes(toEncrypt);
|
|
var cipherText = encryptor
|
|
.TransformFinalBlock(plainText, 0, plainText.Length);
|
|
|
|
var result = MergeArrays(keySalt, iv, cipherText);
|
|
return result;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
public static string DecryptToString(byte[] encryptedData)
|
|
{
|
|
using (var aes = Aes.Create())
|
|
{
|
|
var keySalt = encryptedData.Take(PasswordSaltByteSize).ToArray();
|
|
var key = GetKey(keySalt);
|
|
var iv = encryptedData
|
|
.Skip(PasswordSaltByteSize).Take(AesBlockByteSize).ToArray();
|
|
var cipherText = encryptedData
|
|
.Skip(PasswordSaltByteSize + AesBlockByteSize).ToArray();
|
|
|
|
using (var encryptor = aes.CreateDecryptor(key, iv))
|
|
{
|
|
var decryptedBytes = encryptor
|
|
.TransformFinalBlock(cipherText, 0, cipherText.Length);
|
|
return StringEncoding.GetString(decryptedBytes);
|
|
}
|
|
}
|
|
}
|
|
|
|
private static byte[] GetKey(byte[] passwordSalt)
|
|
{
|
|
var keyBytes = StringEncoding.GetBytes(password);
|
|
|
|
using (var derivator = new Rfc2898DeriveBytes(
|
|
keyBytes, passwordSalt,
|
|
PasswordIterationCount, HashAlgorithmName.SHA256))
|
|
{
|
|
return derivator.GetBytes(PasswordByteSize);
|
|
}
|
|
}
|
|
|
|
private static byte[] GenerateRandomBytes(int numberOfBytes)
|
|
{
|
|
var randomBytes = new byte[numberOfBytes];
|
|
Random.GetBytes(randomBytes);
|
|
return randomBytes;
|
|
}
|
|
|
|
private static byte[] MergeArrays(params byte[][] arrays)
|
|
{
|
|
var merged = new byte[arrays.Sum(a => a.Length)];
|
|
var mergeIndex = 0;
|
|
for (int i = 0; i < arrays.GetLength(0); i++)
|
|
{
|
|
arrays[i].CopyTo(merged, mergeIndex);
|
|
mergeIndex += arrays[i].Length;
|
|
}
|
|
|
|
return merged;
|
|
}
|
|
|
|
public static string getServerUrl()
|
|
{
|
|
if (serverUrl == null || serverUrl.Length == 0)
|
|
{
|
|
serverUrl = readFromFile("common", "server_url");
|
|
}
|
|
return serverUrl;
|
|
}
|
|
|
|
public static string getTokenFormIni()
|
|
{
|
|
return readFromFile("security", "token");
|
|
}
|
|
|
|
public static void savePassword(string username, string password)
|
|
{
|
|
username = Convert.ToBase64String(CommonUtils.EncryptString(username));
|
|
|
|
password = Convert.ToBase64String(CommonUtils.EncryptString(password));
|
|
|
|
writeToIni("userData", "userName", username);
|
|
|
|
writeToIni("userData", "password", password);
|
|
}
|
|
|
|
public static string getApiUrl(string name)
|
|
{
|
|
return readFromFile("ApiUrls", name);
|
|
|
|
}
|
|
|
|
public static string getFtpServer()
|
|
{
|
|
return readFromFile("ftp_server", "server_ip");
|
|
}
|
|
|
|
public static string getFtpUsr()
|
|
{
|
|
return getDecryptValue("ftp_server", "ftp_user");
|
|
}
|
|
|
|
public static string getFtpPassword()
|
|
{
|
|
return getDecryptValue("ftp_server", "ftp_password");
|
|
|
|
}
|
|
|
|
private static string getDecryptValue(string name, string item)
|
|
{
|
|
string str = readFromFile(name, item);
|
|
if (str == null)
|
|
{
|
|
return "";
|
|
}
|
|
else
|
|
{
|
|
return DecryptToString(Convert.FromBase64String(str));
|
|
}
|
|
}
|
|
|
|
public static int getFtpServerPort()
|
|
{
|
|
return int.Parse(readFromFile("ftp_server", "server_port"));
|
|
}
|
|
}
|
|
}
|