static void Main(string[] args) { try { Socket.SendTimeout = 500; Socket.ReceiveTimeout = 500; Socket.Connect(server, port); var helloBytes = new byte[16]; Socket.Receive(helloBytes); Console.WriteLine("Received hello: " + Encoding.UTF8.GetString(helloBytes)); if (!CheckConnectionChecksum(helloBytes, 16)) { throw new Exception("Wrong connection Checksum"); } CryptoBlock.cc_crypt_xor(helloBytes); SHA1 sha = new SHA1Managed(); var sha1Hash = sha.ComputeHash(helloBytes); //Init receive cripto block ReceiveBlock.cc_crypt_init(sha1Hash, 20); ReceiveBlock.cc_decrypt(helloBytes, 16); //Init send cripto block SendBlock.cc_crypt_init(helloBytes, 16); SendBlock.cc_decrypt(sha1Hash, 20); SendMessage(MsgTypes.MsgNoHeader, 20, sha1Hash); //Handshake byte[] userName = new byte[20]; Array.Copy(GetBytes(username), userName, GetBytes(username).Length); SendMessage(MsgTypes.MsgNoHeader, 20, userName); //Send username in a padded array of 20 bytes byte[] pwd = new byte[password.Length]; Array.Copy(GetBytes(password), pwd, GetBytes(password).Length); SendBlock.cc_encrypt(pwd, pwd.Length); //encript psw in cripto block byte[] cCcam = { Convert.ToByte('C'), Convert.ToByte('C'), Convert.ToByte('c'), Convert.ToByte('a'), Convert.ToByte('m'), 0 }; SendMessage(MsgTypes.MsgNoHeader, 6, cCcam); //Send "CCcam" with password encripted block try { byte[] receiveBytes = new byte[20]; var recvCount = Socket.Receive(receiveBytes); if (recvCount > 0) { ReceiveBlock.cc_decrypt(receiveBytes, 20); var valid = Encoding.Default.GetString(receiveBytes).Replace("\0", "") == "CCcam"; Console.WriteLine(valid ? "Connection SUCCESS!" : "Wrong ACK received!"); if (valid && retrieveCardInfo) { SendClientInfo(); if (Socket.Connected) { ReadCardsInformation(); } } } else { Console.WriteLine("Wrong username/password"); } } catch (Exception) { Console.WriteLine("Wrong username/password"); } } catch (Exception) { Console.WriteLine("Failed to connect"); } Socket.Close(); }
static void Main(string[] args) { var server = "srv.server.com"; var port = 9999; var username = "******"; var password = "******"; try { Socket.Connect(server, port); var helloBytes = new byte[16]; Socket.Receive(helloBytes); Console.WriteLine("Received hello: " + Encoding.UTF8.GetString(helloBytes)); if (!CheckConnectionChecksum(helloBytes, 16)) { throw new Exception("Wrong connection Checksum"); } CryptoBlock.cc_crypt_xor(helloBytes); SHA1 sha = new SHA1Managed(); var sha1Hash = sha.ComputeHash(helloBytes); //Init receive cripto block ReceiveBlock.cc_crypt_init(sha1Hash, 20); ReceiveBlock.cc_decrypt(helloBytes, 16); //Init send cripto block SendBlock.cc_crypt_init(helloBytes, 16); SendBlock.cc_decrypt(sha1Hash, 20); SendMsg(20, sha1Hash); //Handshake byte[] userName = new byte[20]; Array.Copy(GetBytes(username), userName, GetBytes(username).Length); SendMsg(20, userName); //Send username in a padded array of 20 bytes byte[] pwd = new byte[password.Length]; Array.Copy(GetBytes(password), userName, GetBytes(password).Length); SendBlock.cc_encrypt(pwd, pwd.Length); //encript psw in cripto block byte[] cCcam = { Convert.ToByte('C'), Convert.ToByte('C'), Convert.ToByte('c'), Convert.ToByte('a'), Convert.ToByte('m'), 0 }; SendMsg(6, cCcam); //Send "CCcam" with password encripted block try { byte[] receiveBytes = new byte[20]; var recvCount = Socket.Receive(receiveBytes); if (recvCount > 0) { ReceiveBlock.cc_decrypt(receiveBytes, 20); Console.WriteLine(Encoding.Default.GetString(receiveBytes).Replace("\0", "") == "CCcam" ? "SUCCESS!" : "Wrong ACK received!"); } else { Console.WriteLine("Wrong username/password"); } } catch (Exception) { Console.WriteLine("Wrong username/password"); } } catch (Exception) { Console.WriteLine("Failed to connect"); } Socket.Close(); }