TBF : Local settings encryption, ver. 2.18.1262
This commit is contained in:
+103
-20
@@ -1,8 +1,9 @@
|
|||||||
///
|
///
|
||||||
/// Copyright (c) 2013-2015 Sensus Metering Systems
|
/// Copyright (c) 2013-2019 Sensus Slovensko a.s.
|
||||||
///
|
///
|
||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Security.Cryptography;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Xml.Serialization;
|
using System.Xml.Serialization;
|
||||||
|
|
||||||
@@ -19,6 +20,11 @@ namespace TBF
|
|||||||
public class LocalSettings
|
public class LocalSettings
|
||||||
{
|
{
|
||||||
static XmlSerializer serializer = XmlSerializer.FromTypes(new[] { typeof(LocalSettings) })[0];
|
static XmlSerializer serializer = XmlSerializer.FromTypes(new[] { typeof(LocalSettings) })[0];
|
||||||
|
|
||||||
|
static byte[] cryptoKey = new byte[] { 235, 7, 236, 58, 24, 225, 97, 102, 231, 1, 121, 12, 177, 145, 87, 200,
|
||||||
|
102, 85, 152, 73, 230, 203, 101, 169, 242, 213, 228, 219, 239, 157, 245, 156 };
|
||||||
|
|
||||||
|
static byte[] cryptoIV = new byte[] { 189, 23, 137, 56, 204, 241, 242, 118, 28, 89, 9, 198, 224, 111, 186, 125 };
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// User interface language (used as CultureInfo(..) constructor argument).
|
/// User interface language (used as CultureInfo(..) constructor argument).
|
||||||
@@ -262,25 +268,66 @@ namespace TBF
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Load public fields of this class from the XML file.
|
/// Load public fields of this class from the XML file.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>LocalSettings object or null when Load() fails</returns>
|
/// <param name="fileName">Name of the file.</param>
|
||||||
|
/// <returns>
|
||||||
|
/// LocalSettings object or null when Load() fails
|
||||||
|
/// </returns>
|
||||||
public static LocalSettings Load(string fileName)
|
public static LocalSettings Load(string fileName)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
using (TextReader reader = new StreamReader(fileName))
|
bool isEncrypted = true;
|
||||||
|
using (StreamReader reader = new StreamReader(fileName))
|
||||||
{
|
{
|
||||||
LocalSettings ls = serializer.Deserialize(reader) as LocalSettings;
|
isEncrypted = !reader.ReadLine().StartsWith("<?xml version=");
|
||||||
|
reader.Close();
|
||||||
|
}
|
||||||
|
|
||||||
if (ls.RealDensity < 500 || ls.RealDensity > 1500)
|
if (isEncrypted)
|
||||||
{
|
{
|
||||||
/// Use default value if density was not set yet
|
int length = (int)(new FileInfo(fileName).Length);
|
||||||
ls.RealDensity = 998.2008; /// kg/m3
|
|
||||||
ls.AtTemperature = 20.0; /// deg.C
|
|
||||||
}
|
|
||||||
|
|
||||||
UpdateResultsOutputData(ls);
|
Stream stream = File.OpenRead(fileName);
|
||||||
|
using (BinaryReader binaryReader = new BinaryReader(stream))
|
||||||
|
{
|
||||||
|
/// Load encrypted binary data into an array
|
||||||
|
byte[] encrypted = new byte[length];
|
||||||
|
int bytesRead = binaryReader.Read(encrypted, 0, length);
|
||||||
|
if (bytesRead != length) return null;
|
||||||
|
|
||||||
return ls;
|
using (RijndaelManaged myRijndael = new RijndaelManaged())
|
||||||
|
{
|
||||||
|
// Create a decryptor to perform the stream transform.
|
||||||
|
ICryptoTransform decryptor = myRijndael.CreateDecryptor(cryptoKey, cryptoIV);
|
||||||
|
|
||||||
|
// Create the streams used for decryption.
|
||||||
|
using (MemoryStream msDecrypt = new MemoryStream(encrypted))
|
||||||
|
{
|
||||||
|
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
|
||||||
|
{
|
||||||
|
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
|
||||||
|
{
|
||||||
|
TextReader txtReader = new StringReader(srDecrypt.ReadToEnd());
|
||||||
|
LocalSettings ls = serializer.Deserialize(txtReader) as LocalSettings;
|
||||||
|
|
||||||
|
ls.CondInitRealDensity();
|
||||||
|
ls.UpdateResultsOutputData();
|
||||||
|
return ls;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
using (StreamReader reader = new StreamReader(fileName))
|
||||||
|
{
|
||||||
|
LocalSettings ls = serializer.Deserialize(reader) as LocalSettings;
|
||||||
|
ls.CondInitRealDensity();
|
||||||
|
ls.UpdateResultsOutputData();
|
||||||
|
return ls;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
@@ -297,11 +344,39 @@ namespace TBF
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
UpdateResultsOutputData(this);
|
UpdateResultsOutputData();
|
||||||
|
|
||||||
using (TextWriter writer = new StreamWriter(Program.LocalSettingsFileName))
|
/// Write settings to a string
|
||||||
|
StringBuilder plain = new StringBuilder();
|
||||||
|
using (TextWriter writer = new StringWriter(plain))
|
||||||
{
|
{
|
||||||
serializer.Serialize(writer, this);
|
serializer.Serialize(writer, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Encrypt
|
||||||
|
using (RijndaelManaged myRijndael = new RijndaelManaged())
|
||||||
|
{
|
||||||
|
/// Create an encryptor to perform the stream transform.
|
||||||
|
ICryptoTransform encryptor = myRijndael.CreateEncryptor(cryptoKey, cryptoIV);
|
||||||
|
|
||||||
|
/// Create the streams used for encryption.
|
||||||
|
using (MemoryStream msEncrypt = new MemoryStream())
|
||||||
|
{
|
||||||
|
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
|
||||||
|
{
|
||||||
|
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
|
||||||
|
{
|
||||||
|
/// Write all data to the stream.
|
||||||
|
swEncrypt.Write(plain);
|
||||||
|
}
|
||||||
|
|
||||||
|
Stream stream = File.OpenWrite(Program.LocalSettingsFileName);
|
||||||
|
using (BinaryWriter writer = new BinaryWriter(stream))
|
||||||
|
{
|
||||||
|
writer.Write(msEncrypt.ToArray());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
@@ -310,11 +385,20 @@ namespace TBF
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CondInitRealDensity()
|
||||||
private static void UpdateResultsOutputData(LocalSettings ls)
|
|
||||||
{
|
{
|
||||||
Results.WMeterRsltItemSpec.RealDensity = ls.RealDensity;
|
if (RealDensity < 500 || RealDensity > 1500)
|
||||||
Results.WMeterRsltItemSpec.AtTemperature = ls.AtTemperature;
|
{
|
||||||
|
/// Use default value if density value was not set yet
|
||||||
|
RealDensity = 998.2008; /// kg/m3
|
||||||
|
AtTemperature = 20.0; /// deg.C
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void UpdateResultsOutputData()
|
||||||
|
{
|
||||||
|
Results.WMeterRsltItemSpec.RealDensity = this.RealDensity;
|
||||||
|
Results.WMeterRsltItemSpec.AtTemperature = this.AtTemperature;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -364,6 +448,5 @@ namespace TBF
|
|||||||
Save();
|
Save();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,5 +29,5 @@ using System.Runtime.InteropServices;
|
|||||||
// Build Number
|
// Build Number
|
||||||
// Revision
|
// Revision
|
||||||
//
|
//
|
||||||
[assembly: AssemblyVersion("2.18.1261.0")]
|
[assembly: AssemblyVersion("2.18.1262.0")]
|
||||||
[assembly: AssemblyFileVersion("2.18.1261.0")]
|
[assembly: AssemblyFileVersion("2.18.1262.0")]
|
||||||
|
|||||||
@@ -134,6 +134,7 @@
|
|||||||
<Reference Include="System.Deployment" />
|
<Reference Include="System.Deployment" />
|
||||||
<Reference Include="System.Drawing" />
|
<Reference Include="System.Drawing" />
|
||||||
<Reference Include="System.Numerics" />
|
<Reference Include="System.Numerics" />
|
||||||
|
<Reference Include="System.Security" />
|
||||||
<Reference Include="System.Web" />
|
<Reference Include="System.Web" />
|
||||||
<Reference Include="System.Windows.Forms" />
|
<Reference Include="System.Windows.Forms" />
|
||||||
<Reference Include="System.Xml" />
|
<Reference Include="System.Xml" />
|
||||||
|
|||||||
Reference in New Issue
Block a user