176 lines
4.7 KiB
C#
176 lines
4.7 KiB
C#
using System.IO;
|
|
using System.Reflection;
|
|
using System.Runtime.InteropServices;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Xml;
|
|
using System.Xml.Serialization;
|
|
|
|
namespace InfineonHMI.Common;
|
|
|
|
public static class L4ItXmlSerializer
|
|
{
|
|
/// <summary>
|
|
/// Serializes an object.
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="serializableObject"></param>
|
|
/// <param name="fileName"></param>
|
|
/// <param name="encrypt"></param>
|
|
/// <param name="rootElementName"></param>
|
|
|
|
public static void SerializeObject<T>(T serializableObject, string fileName, bool encrypt = false, string? rootElementName = null)
|
|
{
|
|
if (string.IsNullOrEmpty(fileName))
|
|
return;
|
|
|
|
if (serializableObject == null)
|
|
return;
|
|
|
|
XmlSerializer serializer;
|
|
if (rootElementName != null)
|
|
{
|
|
var xmlRoot = new XmlRootAttribute(rootElementName);
|
|
serializer = new XmlSerializer(serializableObject.GetType(), xmlRoot);
|
|
}
|
|
else
|
|
{
|
|
serializer = new XmlSerializer(serializableObject.GetType());
|
|
}
|
|
|
|
try
|
|
{
|
|
var dir = new FileInfo(fileName).DirectoryName;
|
|
if (dir != null && !Directory.Exists(dir)) // Überprüfen Sie, ob dir nicht null ist, bevor Sie es verwenden
|
|
Directory.CreateDirectory(dir);
|
|
|
|
var xmlDocument = new XmlDocument();
|
|
using var stream = new MemoryStream();
|
|
serializer.Serialize(stream, serializableObject);
|
|
stream.Position = 0;
|
|
xmlDocument.Load(stream);
|
|
if (encrypt && false)
|
|
{
|
|
//FileEncryption.SaveEncryptedToFile(fileName, xmlDocument.OuterXml);
|
|
return;
|
|
}
|
|
xmlDocument.Save(fileName);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.Write(ex);
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Deserializes an xml file into an object list
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="fileName"></param>
|
|
/// <param name="decrypt"></param>
|
|
/// <param name="rootElementName"></param>
|
|
/// <returns></returns>
|
|
///
|
|
///
|
|
public static T DeSerializeObject<T>(string fileName, bool decrypt = false, string? rootElementName = null)
|
|
{
|
|
if (string.IsNullOrEmpty(fileName) || !File.Exists(fileName)) return default!;
|
|
|
|
T objectOut;
|
|
|
|
try
|
|
{
|
|
string xmlString;
|
|
if (decrypt && false)
|
|
{
|
|
//xmlString = FileEncryption.ReadDecryptedFromFile(fileName)!;
|
|
}
|
|
else
|
|
{
|
|
var xmlDocument = new XmlDocument();
|
|
xmlDocument.Load(fileName);
|
|
xmlString = xmlDocument.OuterXml;
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(xmlString))
|
|
{
|
|
// Handle empty xmlString if necessary
|
|
return default!;
|
|
}
|
|
|
|
using var read = new StringReader(xmlString);
|
|
var outType = typeof(T);
|
|
|
|
XmlSerializer serializer;
|
|
if (rootElementName != null)
|
|
{
|
|
var root = new XmlRootAttribute(rootElementName);
|
|
serializer = new XmlSerializer(outType, root);
|
|
}
|
|
else
|
|
{
|
|
serializer = new XmlSerializer(outType);
|
|
}
|
|
|
|
using XmlReader reader = new XmlTextReader(read);
|
|
objectOut = (T)serializer.Deserialize(reader)!;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.Write(ex);
|
|
return default!;
|
|
}
|
|
|
|
return objectOut;
|
|
}
|
|
|
|
public static string Encrypt(string encryptString)
|
|
{
|
|
string EncryptionKey = "0ram@1234xxxxxxxxxxtttttuuuuuiiiiio"; //we can change the code converstion key as per our requirement
|
|
byte[] clearBytes = Encoding.Unicode.GetBytes(encryptString);
|
|
using (Aes encryptor = Aes.Create())
|
|
{
|
|
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] {
|
|
0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76
|
|
});
|
|
encryptor.Key = pdb.GetBytes(32);
|
|
encryptor.IV = pdb.GetBytes(16);
|
|
using (MemoryStream ms = new MemoryStream())
|
|
{
|
|
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
|
|
{
|
|
cs.Write(clearBytes, 0, clearBytes.Length);
|
|
cs.Close();
|
|
}
|
|
encryptString = Convert.ToBase64String(ms.ToArray());
|
|
}
|
|
}
|
|
return encryptString;
|
|
}
|
|
|
|
public static string Decrypt(string cipherText)
|
|
{
|
|
string EncryptionKey = "0ram@1234xxxxxxxxxxtttttuuuuuiiiiio"; //we can change the code converstion key as per our requirement, but the decryption key should be same as encryption key
|
|
cipherText = cipherText.Replace(" ", "+");
|
|
byte[] cipherBytes = Convert.FromBase64String(cipherText);
|
|
using (Aes encryptor = Aes.Create())
|
|
{
|
|
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] {
|
|
0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76
|
|
});
|
|
encryptor.Key = pdb.GetBytes(32);
|
|
encryptor.IV = pdb.GetBytes(16);
|
|
using (MemoryStream ms = new MemoryStream())
|
|
{
|
|
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
|
|
{
|
|
cs.Write(cipherBytes, 0, cipherBytes.Length);
|
|
cs.Close();
|
|
}
|
|
cipherText = Encoding.Unicode.GetString(ms.ToArray());
|
|
}
|
|
}
|
|
return cipherText;
|
|
}
|
|
} |