diff --git a/BatCharging.Service/ChargerMgrTool.cs b/BatCharging.Service/ChargerMgrTool.cs index 09f2c5e..d678d01 100644 --- a/BatCharging.Service/ChargerMgrTool.cs +++ b/BatCharging.Service/ChargerMgrTool.cs @@ -1014,8 +1014,8 @@ namespace BatCharging.Service if (CMD4 != null) { byte type = 0; - uint startAddress = 0; - byte[] setData = null; + uint startAddress = 1; + byte[] setData = new byte[4]; //(CMD=3)后台服务器下发充电桩字符型参数 CmnChargerParam._CHR00TCPCLIENT.Send3(type, startAddress, setData); @@ -1035,7 +1035,7 @@ namespace BatCharging.Service byte type = 0; uint startAddress = 0; byte num = 0; - byte[] setData = null; + byte[] setData = new byte[4]; //(CMD=5)后台服务器下发充电桩控制命令 CmnChargerParam._CHR00TCPCLIENT.Send5(type, startAddress, num, setData); diff --git a/BatCharging.Service/ChargerSendMsg.cs b/BatCharging.Service/ChargerSendMsg.cs index ef1870a..c0b0c91 100644 --- a/BatCharging.Service/ChargerSendMsg.cs +++ b/BatCharging.Service/ChargerSendMsg.cs @@ -331,7 +331,7 @@ namespace BatCharging.Service CMD3.value2 = 0; CMD3.value3 = type; CMD3.value4 = startAddress; - CMD3.value5 = pubCommonClass.cmd3StartAddress[startAddress]; + CMD3.value5 = PubCommonClass.cmd3StartAddress[startAddress]; CMD3.value6 = setData; byte[] bytes = new ChargerSendCMD().SendRCMD3(out APDUModel, CMD3); diff --git a/BatCharging.Service/Common/PublicProgram.cs b/BatCharging.Service/Common/PublicProgram.cs index d9f7af4..2e95aed 100644 --- a/BatCharging.Service/Common/PublicProgram.cs +++ b/BatCharging.Service/Common/PublicProgram.cs @@ -48,7 +48,7 @@ namespace BatCharging.Service { int checksum = 0; - for (int i = 8; i < data.Count(); i++)//这里没校验域所以不减一 + for (int i = 6; i < data.Count(); i++)//这里没校验域所以不减一 { checksum += data[i]; } diff --git a/BatCharging.Service/发送方法/ChargerSendCMD.cs b/BatCharging.Service/发送方法/ChargerSendCMD.cs index 4193890..955528a 100644 --- a/BatCharging.Service/发送方法/ChargerSendCMD.cs +++ b/BatCharging.Service/发送方法/ChargerSendCMD.cs @@ -17,7 +17,7 @@ namespace BatCharging.Service APDUModel model = new APDUModel(); model.OriginDomain1 = 0xAA; - model.OriginDomain1 = 0xF5; + model.OriginDomain2 = 0xF5; model.infoDomain = 0x02; model.SerialNumberDomain = 0xff; model.CMD = 1; @@ -74,7 +74,7 @@ namespace BatCharging.Service APDUModel model = new APDUModel(); model.OriginDomain1 = 0xAA; - model.OriginDomain1 = 0xF5; + model.OriginDomain2 = 0xF5; model.infoDomain = 0x02; model.SerialNumberDomain = 0xff; model.CMD = 1; @@ -128,7 +128,7 @@ namespace BatCharging.Service APDUModel model = new APDUModel(); model.OriginDomain1 = 0xAA; - model.OriginDomain1 = 0xF5; + model.OriginDomain2 = 0xF5; model.infoDomain = 0x02; model.SerialNumberDomain = 0xff; model.CMD = 1; @@ -182,7 +182,7 @@ namespace BatCharging.Service APDUModel model = new APDUModel(); model.OriginDomain1 = 0xAA; - model.OriginDomain1 = 0xF5; + model.OriginDomain2 = 0xF5; model.infoDomain = 0x02; model.SerialNumberDomain = 0xff; model.CMD = 1; @@ -240,7 +240,7 @@ namespace BatCharging.Service APDUModel model = new APDUModel(); model.OriginDomain1 = 0xAA; - model.OriginDomain1 = 0xF5; + model.OriginDomain2 = 0xF5; model.infoDomain = 0x02; model.SerialNumberDomain = 0xff; model.CMD = 1; @@ -293,7 +293,7 @@ namespace BatCharging.Service APDUModel model = new APDUModel(); model.OriginDomain1 = 0xAA; - model.OriginDomain1 = 0xF5; + model.OriginDomain2 = 0xF5; model.infoDomain = 0x02; model.SerialNumberDomain = 0xff; model.CMD = 1; @@ -340,7 +340,7 @@ namespace BatCharging.Service APDUModel model = new APDUModel(); model.OriginDomain1 = 0xAA; - model.OriginDomain1 = 0xF5; + model.OriginDomain2 = 0xF5; model.infoDomain = 0x02; model.SerialNumberDomain = 0xff; model.CMD = 1; @@ -386,7 +386,7 @@ namespace BatCharging.Service APDUModel model = new APDUModel(); model.OriginDomain1 = 0xAA; - model.OriginDomain1 = 0xF5; + model.OriginDomain2 = 0xF5; model.infoDomain = 0x02; model.SerialNumberDomain = 0xff; model.CMD = 1; @@ -444,7 +444,7 @@ namespace BatCharging.Service APDUModel model = new APDUModel(); model.OriginDomain1 = 0xAA; - model.OriginDomain1 = 0xF5; + model.OriginDomain2 = 0xF5; model.infoDomain = 0x02; model.SerialNumberDomain = 0xff; model.CMD = 1; @@ -491,7 +491,7 @@ namespace BatCharging.Service APDUModel model = new APDUModel(); model.OriginDomain1 = 0xAA; - model.OriginDomain1 = 0xF5; + model.OriginDomain2 = 0xF5; model.infoDomain = 0x02; model.SerialNumberDomain = 0xff; model.CMD = 1; @@ -536,7 +536,7 @@ namespace BatCharging.Service APDUModel model = new APDUModel(); model.OriginDomain1 = 0xAA; - model.OriginDomain1 = 0xF5; + model.OriginDomain2 = 0xF5; model.infoDomain = 0x02; model.SerialNumberDomain = 0xff; model.CMD = 1; @@ -581,7 +581,7 @@ namespace BatCharging.Service APDUModel model = new APDUModel(); model.OriginDomain1 = 0xAA; - model.OriginDomain1 = 0xF5; + model.OriginDomain2 = 0xF5; model.infoDomain = 0x02; model.SerialNumberDomain = 0xff; model.CMD = 1; @@ -631,7 +631,7 @@ namespace BatCharging.Service APDUModel model = new APDUModel(); model.OriginDomain1 = 0xAA; - model.OriginDomain1 = 0xF5; + model.OriginDomain2 = 0xF5; model.infoDomain = 0x02; model.SerialNumberDomain = 0xff; model.CMD = 1; @@ -684,7 +684,7 @@ namespace BatCharging.Service APDUModel model = new APDUModel(); model.OriginDomain1 = 0xAA; - model.OriginDomain1 = 0xF5; + model.OriginDomain2 = 0xF5; model.infoDomain = 0x02; model.SerialNumberDomain = 0xff; model.CMD = 1; @@ -741,7 +741,7 @@ namespace BatCharging.Service APDUModel model = new APDUModel(); model.OriginDomain1 = 0xAA; - model.OriginDomain1 = 0xF5; + model.OriginDomain2 = 0xF5; model.infoDomain = 0x02; model.SerialNumberDomain = 0xff; model.CMD = 1; @@ -790,7 +790,7 @@ namespace BatCharging.Service APDUModel model = new APDUModel(); model.OriginDomain1 = 0xAA; - model.OriginDomain1 = 0xF5; + model.OriginDomain2 = 0xF5; model.infoDomain = 0x02; model.SerialNumberDomain = 0xff; model.CMD = 1; @@ -846,7 +846,7 @@ namespace BatCharging.Service APDUModel model = new APDUModel(); model.OriginDomain1 = 0xAA; - model.OriginDomain1 = 0xF5; + model.OriginDomain2 = 0xF5; model.infoDomain = 0x02; model.SerialNumberDomain = 0xff; model.CMD = 1; @@ -897,7 +897,7 @@ namespace BatCharging.Service APDUModel model = new APDUModel(); model.OriginDomain1 = 0xAA; - model.OriginDomain1 = 0xF5; + model.OriginDomain2 = 0xF5; model.infoDomain = 0x02; model.SerialNumberDomain = 0xff; model.CMD = 1; @@ -943,7 +943,7 @@ namespace BatCharging.Service APDUModel model = new APDUModel(); model.OriginDomain1 = 0xAA; - model.OriginDomain1 = 0xF5; + model.OriginDomain2 = 0xF5; model.infoDomain = 0x02; model.SerialNumberDomain = 0xff; model.CMD = 1; @@ -990,7 +990,7 @@ namespace BatCharging.Service APDUModel model = new APDUModel(); model.OriginDomain1 = 0xAA; - model.OriginDomain1 = 0xF5; + model.OriginDomain2 = 0xF5; model.infoDomain = 0x02; model.SerialNumberDomain = 0xff; model.CMD = 1; @@ -1039,7 +1039,7 @@ namespace BatCharging.Service APDUModel model = new APDUModel(); model.OriginDomain1 = 0xAA; - model.OriginDomain1 = 0xF5; + model.OriginDomain2 = 0xF5; model.infoDomain = 0x02; model.SerialNumberDomain = 0xff; model.CMD = 1; @@ -1083,7 +1083,7 @@ namespace BatCharging.Service APDUModel model = new APDUModel(); model.OriginDomain1 = 0xAA; - model.OriginDomain1 = 0xF5; + model.OriginDomain2 = 0xF5; model.infoDomain = 0x02; model.SerialNumberDomain = 0xff; model.CMD = 1; @@ -1127,7 +1127,7 @@ namespace BatCharging.Service APDUModel model = new APDUModel(); model.OriginDomain1 = 0xAA; - model.OriginDomain1 = 0xF5; + model.OriginDomain2 = 0xF5; model.infoDomain = 0x02; model.SerialNumberDomain = 0xff; model.CMD = 1; @@ -1171,7 +1171,7 @@ namespace BatCharging.Service APDUModel model = new APDUModel(); model.OriginDomain1 = 0xAA; - model.OriginDomain1 = 0xF5; + model.OriginDomain2 = 0xF5; model.infoDomain = 0x02; model.SerialNumberDomain = 0xff; model.CMD = 1; @@ -1215,7 +1215,7 @@ namespace BatCharging.Service APDUModel model = new APDUModel(); model.OriginDomain1 = 0xAA; - model.OriginDomain1 = 0xF5; + model.OriginDomain2 = 0xF5; model.infoDomain = 0x02; model.SerialNumberDomain = 0xff; model.CMD = 1; @@ -1260,7 +1260,7 @@ namespace BatCharging.Service APDUModel model = new APDUModel(); model.OriginDomain1 = 0xAA; - model.OriginDomain1 = 0xF5; + model.OriginDomain2 = 0xF5; model.infoDomain = 0x02; model.SerialNumberDomain = 0xff; model.CMD = 1; @@ -1304,7 +1304,7 @@ namespace BatCharging.Service APDUModel model = new APDUModel(); model.OriginDomain1 = 0xAA; - model.OriginDomain1 = 0xF5; + model.OriginDomain2 = 0xF5; model.infoDomain = 0x02; model.SerialNumberDomain = 0xff; model.CMD = 1; @@ -1353,7 +1353,7 @@ namespace BatCharging.Service APDUModel model = new APDUModel(); model.OriginDomain1 = 0xAA; - model.OriginDomain1 = 0xF5; + model.OriginDomain2 = 0xF5; model.infoDomain = 0x02; model.SerialNumberDomain = 0xff; model.CMD = 1; @@ -1396,7 +1396,7 @@ namespace BatCharging.Service APDUModel model = new APDUModel(); model.OriginDomain1 = 0xAA; - model.OriginDomain1 = 0xF5; + model.OriginDomain2 = 0xF5; model.infoDomain = 0x02; model.SerialNumberDomain = 0xff; model.CMD = 1; @@ -1508,7 +1508,7 @@ namespace BatCharging.Service APDUModel model = new APDUModel(); model.OriginDomain1 = 0xAA; - model.OriginDomain1 = 0xF5; + model.OriginDomain2 = 0xF5; model.infoDomain = 0x02; model.SerialNumberDomain = 0xff; model.CMD = 1; @@ -1632,7 +1632,7 @@ namespace BatCharging.Service APDUModel model = new APDUModel(); model.OriginDomain1 = 0xAA; - model.OriginDomain1 = 0xF5; + model.OriginDomain2 = 0xF5; model.infoDomain = 0x02; model.SerialNumberDomain = 0xff; model.CMD = 1; @@ -1678,7 +1678,7 @@ namespace BatCharging.Service APDUModel model = new APDUModel(); model.OriginDomain1 = 0xAA; - model.OriginDomain1 = 0xF5; + model.OriginDomain2 = 0xF5; model.infoDomain = 0x02; model.SerialNumberDomain = 0xff; model.CMD = 1; @@ -1738,7 +1738,7 @@ namespace BatCharging.Service APDUModel model = new APDUModel(); model.OriginDomain1 = 0xAA; - model.OriginDomain1 = 0xF5; + model.OriginDomain2 = 0xF5; model.infoDomain = 0x02; model.SerialNumberDomain = 0xff; model.CMD = 1; @@ -1788,7 +1788,7 @@ namespace BatCharging.Service APDUModel model = new APDUModel(); model.OriginDomain1 = 0xAA; - model.OriginDomain1 = 0xF5; + model.OriginDomain2 = 0xF5; model.infoDomain = 0x02; model.SerialNumberDomain = 0xff; model.CMD = 1; diff --git a/CommonClass/PubCommonClass.cs b/CommonClass/PubCommonClass.cs index 6980cc7..f7ef88f 100644 --- a/CommonClass/PubCommonClass.cs +++ b/CommonClass/PubCommonClass.cs @@ -8,7 +8,7 @@ /// /// /// - public Dictionary cmd3StartAddress = new Dictionary(); + public static Dictionary cmd3StartAddress = new Dictionary(); //public Dictionary<> diff --git a/Module.Socket.Tool/AesEncryption.cs b/Module.Socket.Tool/AesEncryption.cs new file mode 100644 index 0000000..8129655 --- /dev/null +++ b/Module.Socket.Tool/AesEncryption.cs @@ -0,0 +1,80 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Security.Cryptography; +using System.Text; +using System.Threading.Tasks; + +namespace Module.Socket.Tool +{ + /// + /// AES加密 + /// + public class AesEncryption + { + private static readonly byte[] Key = Encoding.UTF8.GetBytes("YourSecretKeyHere12345678"); // 必须是32字节(256位) + private static readonly byte[] Iv = Encoding.UTF8.GetBytes("YourInitializationVectorHere"); // 必须是16字节(128位) + + public static byte[] EncryptStringToBytes(string plainText) + { + if (plainText == null || plainText.Length <= 0) + throw new ArgumentNullException(nameof(plainText)); + if (Key == null || Key.Length != 32) + throw new ArgumentNullException(nameof(Key)); + if (Iv == null || Iv.Length != 16) + throw new ArgumentNullException(nameof(Iv)); + + byte[] encrypted; + + using (Aes aesAlg = Aes.Create()) + { + aesAlg.Key = Key; + aesAlg.IV = Iv; + + ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV); + + using (MemoryStream msEncrypt = new MemoryStream()) + { + using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) + using (StreamWriter swEncrypt = new StreamWriter(csEncrypt)) + { + swEncrypt.Write(plainText); + } + encrypted = msEncrypt.ToArray(); + } + } + + return encrypted; + } + + public static string DecryptBytesToString(byte[] cipherText) + { + if (cipherText == null || cipherText.Length <= 0) + throw new ArgumentNullException(nameof(cipherText)); + if (Key == null || Key.Length != 32) + throw new ArgumentNullException(nameof(Key)); + if (Iv == null || Iv.Length != 16) + throw new ArgumentNullException(nameof(Iv)); + + string plaintext = null; + + using (Aes aesAlg = Aes.Create()) + { + aesAlg.Key = Key; + aesAlg.IV = Iv; + + ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV); + + using (MemoryStream msDecrypt = new MemoryStream(cipherText)) + using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) + using (StreamReader srDecrypt = new StreamReader(csDecrypt)) + { + plaintext = srDecrypt.ReadToEnd(); + } + } + + return plaintext; + } + } +} diff --git a/Module.Socket.Tool/Module.Socket.Tool.csproj b/Module.Socket.Tool/Module.Socket.Tool.csproj index a48860f..22e324b 100644 --- a/Module.Socket.Tool/Module.Socket.Tool.csproj +++ b/Module.Socket.Tool/Module.Socket.Tool.csproj @@ -42,6 +42,7 @@ + diff --git a/Module.Socket.Tool/TcpClientChargerTool.cs b/Module.Socket.Tool/TcpClientChargerTool.cs index e7c0b4a..56eb4bd 100644 --- a/Module.Socket.Tool/TcpClientChargerTool.cs +++ b/Module.Socket.Tool/TcpClientChargerTool.cs @@ -392,7 +392,7 @@ namespace Module.Socket.Tool bool bResult = false; int checksum = 0; - for (int i = 8; i < data.Count() - 1; i++)//这里有校验域所以减一 + for (int i = 6; i < data.Count() - 1; i++)//这里有校验域所以减一 { checksum += data[i]; } @@ -438,7 +438,22 @@ namespace Module.Socket.Tool //校验域是否正确 if (checksumDomainReceive(newBytes)) - lstResult.Add(newBytes); + { + var info = newBytes[4]; + if ((info & (1 << 1)) != 0)//AES加密 + { + + List byteList = newBytes.ToList(); + byteList.Remove(8); + byteList.Remove(8);//移除前面两个长度 + newBytes= byteList.ToArray(); + lstResult.Add(newBytes); + } + else //不加密 + { + lstResult.Add(newBytes); + } + } } } } diff --git a/Monitor.ChargeTest/Program.cs b/Monitor.ChargeTest/Program.cs index 8032808..0784eb3 100644 --- a/Monitor.ChargeTest/Program.cs +++ b/Monitor.ChargeTest/Program.cs @@ -1,3 +1,4 @@ +using CommonClass; using System; using System.Collections.Generic; using System.Linq; @@ -14,6 +15,8 @@ namespace Monitor.ChargeTest [STAThread] static void Main() { + new PubCommonClass().SetCmd3StartAddress(); + Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new FrmChargeResult());