【原创】国网远程加密机认证软件开发2(认证步骤及代码)

本文介绍了作者在开发国网远程加密机认证软件时的经历,详细讲述了认证过程,包括动态库函数调用、串口操作、组帧、连接加密机、认证等步骤。虽然没有涉及socket监听,但提供了发送和接收数据的函数代码。认证工作在端午节前完成,作者计划后续增加抄读和远控功能,并分享部分代码供参考。
摘要由CSDN通过智能技术生成

这两天出了点问题,由于不会操作加密机,顺道学习了下加密机的硬件操作,不过已经实现了讲过网络加密机对新国网正式ESAM进行认证。

先把图片放出来吧

整个认证过程我没有对端口进行监听,也就略过了socket这部分内容,也许后续会完善进去,话不多说了,直接贴代码吧。有点乱,容我有时间整里下。

1.动态库函数调用

 [DllImport("SJJ1009forformalchip.dll")]//连接设备
        public static extern int ConnectDevice(string putIp, string putPortk, string putCTime);
 [DllImport("SJJ1009forformalchip.dll")]//关闭设备
        public static extern int CloseDevice();
 [DllImport("SJJ1009forformalchip.dll")]//身份认证
        public static extern int Meter_Formal_IdentityAuthentication(int Flag, string PutDiv, System.Text.StringBuilder OutRand, System.Text.StringBuilder OutEndata);

2.串口操作自行补脑,这里就不多解释了

  1 namespace RemoteReceiveComm
  2 {
  3     using System;
  4     using System.Runtime.InteropServices;
  5     using System.Text;
  6 
  7     internal class MyCom
  8     {
  9         private byte bParity;
 10         private int bSize;
 11         private byte bStopBits;
 12         private const uint GENERIC_READ = 0x80000000;
 13         private const uint GENERIC_WRITE = 0x40000000;
 14         private int hComm;
 15         private const int INVALID_HANDLE_VALUE = -1;
 16         private int iPort;
 17         private int iRate;
 18         private int iTimeout;
 19         private const int OPEN_EXISTING = 3;
 20         public bool Opened;
 21 
 22         public MyCom()
 23         {
 24             this.iTimeout = 0xfa0;
 25             this.hComm = -1;
 26             this.Opened = false;
 27             this.iPort = 0;
 28             this.iRate = 0x2580;
 29             this.bSize = 8;
 30             this.bParity = 0;
 31             this.bStopBits = 0;
 32         }
 33 
 34         public MyCom(int com, int rate, int size, byte parity, byte stopbits)
 35         {
 36             this.iTimeout = 0xfa0;
 37             this.hComm = -1;
 38             this.Opened = false;
 39             this.iPort = com;
 40             this.iRate = rate;
 41             this.bSize = size;
 42             this.bParity = parity;
 43             this.bStopBits = stopbits;
 44         }
 45 
 46         [DllImport("kernel32.dll")]
 47         private static extern bool BuildCommDCB(string lpDef, ref DCB lpDCB);
 48         public void Close()
 49         {
 50             if (this.hComm != -1)
 51             {
 52                 CloseHandle(this.hComm);
 53                 this.Opened = false;
 54             }
 55         }
 56 
 57         [DllImport("kernel32.dll")]
 58         private static extern bool CloseHandle(int hObject);
 59         [DllImport("kernel32.dll")]
 60         private static extern int CreateFile(string lpFileName, uint dwDesiredAccess, int dwShareMode, int lpSecurityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, int hTemplateFile);
 61         [DllImport("kernel32.dll")]
 62         private static extern bool GetCommState(int hFile, ref DCB lpDCB);
 63         [DllImport("kernel32.dll")]
 64         private static extern bool GetCommTimeouts(int hFile, ref COMMTIMEOUTS lpCommTimeouts);
 65         public void Open()
 66         {
 67             DCB structure = new DCB();
 68             COMMTIMEOUTS lpCommTimeouts = new COMMTIMEOUTS();
 69             this.hComm = CreateFile("COM" + this.iPort, 0xc0000000, 0, 0, 3, 0, 0);
 70             if (this.hComm == -1)
 71             {
 72                 this.Opened = false;
 73                 throw new ApplicationException("打开所选串口失败!!");
 74             }
 75             GetCommTimeouts(this.hComm, ref lpCommTimeouts);
 76             lpCommTimeouts.ReadTotalTimeoutConstant = this.iTimeout;
 77             lpCommTimeouts.ReadTotalTimeoutMultiplier = 0;
 78             lpCommTimeouts.WriteTotalTimeoutMultiplier = 0;
 79             lpCommTimeouts.WriteTotalTimeoutConstant = 0;
 80             SetCommTimeouts(this.hComm, ref lpCommTimeouts);
 81             structure.DCBlength = Marshal.SizeOf(structure);
 82             GetCommState(this.hComm, ref structure);
 83             structure.BaudRate = this.iRate;
 84             structure.Parity = this.bParity;
 85             structure.ByteSize = (byte) this.bSize;
 86             structure.StopBits = this.bStopBits;
 87             SetCommState(this.hComm, ref structure);
 88             this.Opened = true;
 89         }
 90 
 91         public byte[] Read(int NumBytes)
 92         {
 93             byte[] lpBuffer = new byte[NumBytes];
 94             if (this.hComm == -1)
 95             {
 96                 throw new ApplicationException("串口没有打开");
 97             }
 98             OVERLAPPED lpOverlapped = new OVERLAPPED();
 99             int lpNumberOfBytesRead = 0;
100             ReadFile(this.hComm, lpBuffer, NumBytes, ref lpNumberOfBytesRead, ref lpOverlapped);
101             byte[] destinationArray = new byte[lpNumberOfBytesRead];
102             Array.Copy(lpBuffer, destinationArray, lpNumberOfBytesRead);
103             return destinationArray;
104         }
105 
106         [DllImport("kernel32.dll")]
107         private static extern bool ReadFile(int hFile, byte[] lpBuffer, int nNumberOfBytesToRead, ref int lpNumberOfBytesRead, ref OVERLAPPED lpOverlapped);
108         public string ReadS(int NumBytes)
109         {
110             byte[] lpBuffer = new byte[NumBytes];
111             if (this.hComm == -1)
112             {
113                 throw new ApplicationException("串口没有打开");
114             }
115             OVERLAPPED lpOverlapped = new OVERLAPPED();
116             int lpNumberOfBytesRead = 0;
117             ReadFile(this.hComm, lpBuffer, NumBytes, ref lpNumberOfBytesRead, ref lpOverlapped);
118             byte[] destinationArray = new byte[lpNumberOfBytesRead];
119             Array.Copy(lpBuffer, destinationArray, lpNumberOfBytesRead);
120             return Encoding.Default.GetString(destinationArray);
121         }
122 
123         [DllImport("kernel32.dll")]
124         private static extern bool SetCommState(int hFile, ref DCB lpDCB);
125         [DllImport("kernel32.dll")]
126         
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值