Files
tbf/NfcC7_DLL/NfcHanler/Utils/SensusRFKeyManager.cs
T

188 lines
4.5 KiB
C#

namespace NfcC7_DLL.NfcHanler.Utils
{
public class SensusRFKey
{
public string Name;
public byte[] Key;
public static bool CheckKey(string keyStr)
{
bool result = true;
string[] check = keyStr.Split(' ');
if(check.Length != 16)
{
return false;
}
for (int i = 0; i < check.Length; i++)
{
byte testResult;
if (!(byte.TryParse(check[i], System.Globalization.NumberStyles.HexNumber, null, out testResult))) //Check each bit.
{
return false;
}
if (testResult > 0xFF)
{
return false;
}
}
return result;
}
bool ParseKey(string keyStr)
{
bool result = true;
string[] check = keyStr.Split(' ');
byte[] key = new byte[check.Length];
//Check the length
if(key.Length != 16)
{
return false;
}
for (int i = 0; i < check.Length; i++) //Attempt to parse the key and shove it into a byte array
{
byte testResult;
if (!(byte.TryParse(check[i], System.Globalization.NumberStyles.HexNumber, null, out testResult))) //Check each bit.
{
result = false;
break;
}
if (testResult > 0xFF)
{
result = false;
break;
}
key[i] = testResult;
}
if (result) //If we didn't fail, load it into the actual Key.
{
Key = key;
}
return result;
}
public SensusRFKey(string name, byte[] key)
{
Name = name;
Key = key;
}
public SensusRFKey(string name, string keyStr)
{
Name = name;
if (!ParseKey(keyStr))
{
throw new ArgumentOutOfRangeException("Bad Key");
}
}
}
public class SensusRFKeyManager
{
public List<SensusRFKey> KeyList;
public bool AddKey(string name, string keyStr)
{
bool result = true;
foreach(SensusRFKey key in KeyList)
{
if(key.Name == name)
{
result = false;
}
}
if (result)
{
try
{
KeyList.Add(new SensusRFKey(name, keyStr));
}
catch (Exception e)
{
result = false;
}
}
return result;
}
public bool AddKey(string name, byte[] newKey)
{
bool result = true;
foreach (SensusRFKey key in KeyList)
{
if (key.Name == name)
{
result = false;
}
}
if(result)
{
try
{
KeyList.Add(new SensusRFKey(name, newKey));
}
catch (Exception e)
{
result = false;
}
}
return result;
}
public List<string> GetNameList()
{
List<string> nameList = new List<string>();
foreach(SensusRFKey key in KeyList)
{
nameList.Add(key.Name);
}
return nameList;
}
public byte[] GetKey(string name)
{
foreach(SensusRFKey key in KeyList)
{
if(key.Name == name)
{
return key.Key;
}
}
return null;
}
public SensusRFKeyManager()
{
KeyList = new List<SensusRFKey>();
}
}
class FactoryKey
{
public static byte[] Value { get { return new byte[] { 0x90, 0xbe, 0x46, 0xa2, 0xc7, 0xb3, 0x09, 0x02, 0x28, 0xcf, 0x74, 0x96, 0x61, 0x10, 0xd8, 0xf2 }; } }
}
class DefaultKey
{
public static byte[] Value { get { return new byte[] { 0xE6, 0xC8, 0x88, 0x00, 0xDE, 0xB8, 0x68, 0xC0, 0xD6, 0xA8, 0x48, 0x80, 0xCE, 0x98, 0x28, 0x40 }; } }
}
}