diff --git a/uniper_hmi/Infineon.sln.DotSettings b/uniper_hmi/Infineon.sln.DotSettings new file mode 100644 index 0000000..dd67cff --- /dev/null +++ b/uniper_hmi/Infineon.sln.DotSettings @@ -0,0 +1,2 @@ + + True \ No newline at end of file diff --git a/uniper_hmi/UniperHMI/Common/L4ItXmlSerializer.cs b/uniper_hmi/UniperHMI/Common/L4ItXmlSerializer.cs index 3ee37b3..4c6dc75 100644 --- a/uniper_hmi/UniperHMI/Common/L4ItXmlSerializer.cs +++ b/uniper_hmi/UniperHMI/Common/L4ItXmlSerializer.cs @@ -1,127 +1,176 @@ 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 +namespace InfineonHMI.Common; + +public static class L4ItXmlSerializer { - public static class L4ItXmlSerializer + /// + /// Serializes an object. + /// + /// + /// + /// + /// + /// + + public static void SerializeObject(T serializableObject, string fileName, bool encrypt = false, string rootElementName = null) { - /// - /// Serializes an object. - /// - /// - /// - /// - /// - /// + if (string.IsNullOrEmpty(fileName)) + return; - public static void SerializeObject(T serializableObject, string fileName, bool encrypt = false, string rootElementName = null) + if (serializableObject == null) + return; + + XmlSerializer serializer; + if (rootElementName != null) { - if (string.IsNullOrEmpty(fileName)) - return; + var xmlRoot = new XmlRootAttribute(rootElementName); + serializer = new XmlSerializer(serializableObject.GetType(), xmlRoot); + } + else + { + serializer = new XmlSerializer(serializableObject.GetType()); + } - if (serializableObject == null) + 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); + } + } + + + /// + /// Deserializes an xml file into an object list + /// + /// + /// + /// + /// + /// + /// + /// + public static T DeSerializeObject(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 xmlRoot = new XmlRootAttribute(rootElementName); - serializer = new XmlSerializer(serializableObject.GetType(), xmlRoot); + var root = new XmlRootAttribute(rootElementName); + serializer = new XmlSerializer(outType, root); } else { - serializer = new XmlSerializer(serializableObject.GetType()); + serializer = new XmlSerializer(outType); } - 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); - } + using XmlReader reader = new XmlTextReader(read); + objectOut = (T)serializer.Deserialize(reader)!; } - - - /// - /// Deserializes an xml file into an object list - /// - /// - /// - /// - /// - /// - /// - /// - public static T DeSerializeObject(string fileName, bool decrypt = false, string rootElementName = null) + catch (Exception ex) { - 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; + 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; + } +} \ No newline at end of file diff --git a/uniper_hmi/UniperHMI/Common/MediaContainer.xaml b/uniper_hmi/UniperHMI/Common/MediaContainer.xaml index 6594243..cda2874 100644 --- a/uniper_hmi/UniperHMI/Common/MediaContainer.xaml +++ b/uniper_hmi/UniperHMI/Common/MediaContainer.xaml @@ -6,11 +6,9 @@ xmlns:common="clr-namespace:Common" xmlns:HMIToolkit="clr-namespace:HMIToolkit" d:DataContext="{d:DesignInstance Type=common:MediaContainerVm, IsDesignTimeCreatable=True}" - mc:Ignorable="d" - Width="Auto" - Height="Auto" - MinWidth="200"> - + mc:Ignorable="d"> + + @@ -19,65 +17,85 @@ - + - - + + + - - - - + + + + - + -