public static byte[] Decode(byte[] arr, AesDecryptor aesDecryptor, ref int recvIdx) { //byte flag = arr[0]; //bool ziped = ((flag & 0x80) == 0x80); //bool aesed = ((flag & 0x40) == 0x40); //bool crced = ((flag & 0x20) == 0x20); //int idx = flag & 0x1F; //if (recvIdx == idx || true) { //recvIdx++; //if (recvIdx > 0x1F) //{ // recvIdx = 0; //} //var bcrc = new byte[4]; //Buffer.BlockCopy(arr, 1, bcrc, 0, 4); //CheckReverse(bcrc); //int crc32 = BitConverter.ToInt32(bcrc, 0); byte[] data; //var data = new byte[arr.Length - 1 - 4]; //Buffer.BlockCopy(arr, 1 + 4, data, 0, data.Length); //int ncrc32 = 0; //if (crced) //{ // ncrc32 = Crc.Crc32(data); //} //if (ncrc32 == crc32 || true) { //if (aesed || true) { data = arr; data = aesDecryptor.Decrypt(data); } //if (ziped && false) //{ // data = ZLib.UnZip(data); //} if (data != null) { return(data); } else { throw new Exception("Recv Decode data null"); } } //else //{ // throw new Exception("Recv error crc32 " + crc32 + " ncrc32" + ncrc32); //} } //else //{ // throw new Exception("Recv error idx " + idx + " lidx" + recvIdx); //} }
internal static byte[] Decode(byte[] arr, AesDecryptor aesDecryptor, ref int recvIdx) { byte flag = arr[0]; bool ziped = ((flag & 0x80) == 0x80); bool aesed = ((flag & 0x40) == 0x40); bool crced = ((flag & 0x20) == 0x20); int idx = flag & 0x1F; if (recvIdx == idx) { recvIdx++; if (recvIdx > 0x1F) { recvIdx = 0; } Byte[] bcrc = new Byte[4]; Buffer.BlockCopy(arr, 1, bcrc, 0, 4); int crc32 = BitConverter.ToInt32(bcrc, 0); Byte[] data = new Byte[arr.Length - 1 - 4]; Buffer.BlockCopy(arr, 1 + 4, data, 0, data.Length); int ncrc32 = 0; if (crced) { ncrc32 = Crc.Crc32(data); } if (ncrc32 == crc32) { if (aesed) { data = aesDecryptor.Decrypt(data); } if (ziped) { data = ZLib.UnZip(data); } if (data != null) { return(data); } else { TcpLogger.LogError("Recv Decode data null"); } } else { TcpLogger.LogError("Recv error crc32 " + crc32 + " ncrc32" + ncrc32); } } else { TcpLogger.LogError("Recv error idx " + idx + " lidx" + recvIdx); } return(null); }
void HandleDataReceived(IAsyncResult ar) { var args = ar.AsyncState as NetClient.SocketArgs; try { int bytesRead = args.m_Socket.EndReceive(ar); if (bytesRead > 0) { m_BufferReceivedSize += bytesRead; if (m_BufferReceivedSize == m_Buffer.Length) { //解Aes Key IV var decrypt = new AesDecryptor(m_KeyIV, m_KeyIV); var bytes = decrypt.Decrypt(m_Buffer); if (!AesKeyIV.Check(bytes)) { LogHelper.Error("Aes Key IV len error {0}", bytes.Length); m_HandleClose?.Invoke(args); } else { m_HandleConnected?.Invoke(args, bytes); } } else { args.m_Socket.BeginReceive(m_Buffer, m_BufferReceivedSize, m_Buffer.Length - m_BufferReceivedSize, SocketFlags.None, new AsyncCallback(HandleHeadReceived), args); } } else { LogHelper.Error(SocketError.NoData.ToString()); m_HandleClose?.Invoke(args); } } catch (Exception e) { LogHelper.Exception(e); m_HandleClose?.Invoke(args); } }
private string Decrypt(string encData) { if (String.IsNullOrEmpty(encData)) { return(String.Empty); } if (EncryptionVersion == 1) { var key = EncryptionMode == EncryptionMode.Pattern ? EncryptionV1.EncryptionV1.GeneratePasswordHash(pattern, userId) : userId; var iv = new SecureString().AppendChars("LCGoogleApps"); var algorithm = EncryptionV1.EncryptionV1.CreateCryptoAlgorithm(key, iv); return(EncryptionV1.EncryptionV1.Decrypt(Base64.Decode(encData), algorithm)); } else if (EncryptionVersion == 2) { var encBytes = Base64.Decode(encData); switch (EncryptionMode) { case EncryptionMode.Basic: return(Encoding.UTF8.GetString(SymmetricEncryption.Decrypt(encBytes, userId))); case EncryptionMode.Pattern: return(Encoding.UTF8.GetString(SymmetricEncryption.Decrypt(encBytes, pattern.Concat(userId)))); case EncryptionMode.Password: return(Encoding.UTF8.GetString(SymmetricEncryption.Decrypt(encBytes, password))); case EncryptionMode.PatternAndPassword: return(Encoding.UTF8.GetString(SymmetricEncryption.Decrypt(encBytes, password.Concat(pattern)))); default: throw new NotImplementedException("Encryption mode not supported: " + EncryptionMode); } } else if (EncryptionVersion == 3) { var encBytes = Base64.Decode(encData); var aesDecryptor = new AesDecryptor(cryptoConfig); switch (EncryptionMode) { case EncryptionMode.Basic: return(aesDecryptor.Decrypt(encBytes, userId)); case EncryptionMode.Pattern: return(aesDecryptor.Decrypt(encBytes, pattern.Concat(userId))); case EncryptionMode.Password: return(aesDecryptor.Decrypt(encBytes, password)); case EncryptionMode.PatternAndPassword: return(aesDecryptor.Decrypt(encBytes, password.Concat(pattern))); default: throw new NotImplementedException("Encryption mode not supported: " + EncryptionMode); } } else { throw new NotImplementedException("Encryption version not supported: " + EncryptionMode); } }