public override bool PortDataRecv(CMessage sSend, CRecvBuff cRecv) { try { base.PortDataRecv(sSend, cRecv); string sRecv = new ASCIIEncoding().GetString(cRecv.bRecvBuff, 0, cRecv.iRecvLen); int iMark = sRecv.IndexOf(Address); if (iMark >= 0) { int iFF = sRecv.IndexOf('\r', iMark); string sValue = sRecv.Substring(iMark + 6, iFF - iMark - 6); lock (this) { cRecv.DelBuff(iFF + 3); } CommStateE = ECommSatate.Normal; present_MsgFailRep = 0; present_DevFailNum = 0; present_DevFailCyc = 0; return(GetVarValue(sSend, sValue)); } return(false); } catch (Exception ex) { return(false); } }
public static string Decrypt(string Data, byte[] Key, byte[] IV) { try { MemoryStream msDecrypt = new MemoryStream(Convert.FromBase64String(Data)); // Create a CryptoStream using the MemoryStream // and the passed key and initialization vector (IV). CryptoStream csDecrypt = new CryptoStream(msDecrypt, new TripleDESCryptoServiceProvider().CreateDecryptor(Key, IV), CryptoStreamMode.Read); // Create buffer to hold the decrypted data. byte[] fromEncrypt = new byte[Data.Length]; // Read the decrypted data out of the crypto stream // and place it into the temporary buffer. csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length); //Convert the buffer into a string and return it. string ReturnValue = new ASCIIEncoding().GetString(fromEncrypt); if (ReturnValue.Contains("\0\0\0")) { ReturnValue = ReturnValue.Remove(ReturnValue.IndexOf("\0\0\0")); } return(ReturnValue); } catch (CryptographicException e) { Console.WriteLine("A Cryptographic error occurred: {0}", e.Message); return(null); } }
public static string ToAsciiString(this byte[] array) { lock (ToStringExtensions.lockByteToString) { if (array == null) { return(string.Empty); } string str = new ASCIIEncoding().GetString(array, 0, array.Length); int length = str.IndexOf(char.MinValue); if (length >= 0) { str = str.Substring(0, length); } return(str.TrimEnd(' ')); } }
public static CategorizeInfo SortPacket(PcapDotNet.Packets.Packet raw) { // Get a packet from raw data. // Be a little lazy and assume that only Ethernet DLL protcol is used. EthernetDatagram packet = raw.Ethernet; CategorizeInfo info = new CategorizeInfo { NetworkLayer = new Dictionary <string, long>(), TransportLayer = new Dictionary <string, long>(), ApplicationLayer = new Dictionary <string, long>() }; #region Network Layer switch (packet.EtherType) { case EthernetType.Arp: info.NetworkLayer.Increment("ARP"); break; case EthernetType.IpV4: info.NetworkLayer.Increment("IPv4"); break; case EthernetType.IpV6: info.NetworkLayer.Increment("IPv6"); break; default: info.NetworkLayer.Increment("Others"); break; } #endregion #region Transport Layer IpV4Datagram ipv4pk = null; if (packet.EtherType == EthernetType.IpV4) { ipv4pk = packet.IpV4; switch (ipv4pk.Protocol) { case IpV4Protocol.Tcp: info.TransportLayer.Increment("TCP"); break; case IpV4Protocol.Udp: info.TransportLayer.Increment("UDP"); break; default: info.TransportLayer.Increment("Others"); break; } } #endregion #region Application Layer if (ipv4pk != null) { if (ipv4pk.Protocol == IpV4Protocol.Tcp) { TcpDatagram tcpPk = ipv4pk.Tcp; string s = new ASCIIEncoding().GetString(tcpPk.Payload.ToArray()); if (s.IndexOf("HTTP") > 0) { // Found HTTP head info.ApplicationLayer.Increment("HTTP Header"); } else { info.ApplicationLayer.Increment("Others"); } } } #endregion return(info); }
public long GetUID(ref byte[] UID) { long _result = 0; bool cardInserted = false; IntPtr _CardContext = IntPtr.Zero; IntPtr ActiveProtocol = IntPtr.Zero; // Establish Reader context: if (this._ReaderContext == IntPtr.Zero) { _result = SCardEstablishContext(2, IntPtr.Zero, IntPtr.Zero, out this._ReaderContext); #region Get List of Available Readers uint pcchReaders = 0; int nullindex = -1; char nullchar = (char)0; // First call with 3rd parameter set to null gets readers buffer length. _result = SCardListReaders(this._ReaderContext, null, null, ref pcchReaders); byte[] mszReaders = new byte[pcchReaders]; // Fill readers buffer with second call. _result = SCardListReaders(this._ReaderContext, null, mszReaders, ref pcchReaders); // Populate List with readers. string currbuff = new ASCIIEncoding().GetString(mszReaders); int len = (int)pcchReaders; if (len > 0) { while (currbuff[0] != nullchar) { nullindex = currbuff.IndexOf(nullchar); // Get null end character. string reader = currbuff.Substring(0, nullindex); this._AvailableReaders.Add(reader); len = len - (reader.Length + 1); currbuff = currbuff.Substring(nullindex + 1, len); } } #endregion // Select the first reader: this._ReaderName = this._AvailableReaders[0]; } try { //Check if there is a Card in the reader: //dwShareMode: SCARD_SHARE_SHARED = 0x00000002 - This application will allow others to share the reader //dwPreferredProtocols: SCARD_PROTOCOL_T0 - Use the T=0 protocol (value = 0x00000001) if (this._ReaderContext != IntPtr.Zero) { _result = SCardConnect(this._ReaderContext, this._ReaderName, 0x00000002, 0x00000001, ref _CardContext, ref ActiveProtocol); if (_result == 0) { cardInserted = true; SCARD_IO_REQUEST request = new SCARD_IO_REQUEST() { dwProtocol = (uint)ActiveProtocol, cbPciLength = (uint)System.Runtime.InteropServices.Marshal.SizeOf(typeof(SCARD_IO_REQUEST)) }; byte[] sendBytes = new byte[] { 0xFF, 0xCA, 0x00, 0x00, 0x00 }; //<- get UID command for iClass cards byte[] ret_Bytes = new byte[33]; uint sendLen = (uint)sendBytes.Length; uint ret_Len = (uint)ret_Bytes.Length; _result = SCardTransmit(_CardContext, ref request, ref sendBytes[0], sendLen, ref request, ret_Bytes, ref ret_Len); if (_result == 0) { UID = ret_Bytes.Take(4).ToArray(); //only take the first 8, the last 2 bytes are not part of the UID of the card string dataOut = byteToHexa(UID, UID.Length, true).Trim(); //Devolver la respuesta en Hexadecimal } } } } finally { SCardDisconnect(_CardContext, 0); SCardReleaseContext(this._ReaderContext); } return(_result); }