public string Connect(string ip, int port, string usr, string pwd) { var ips = IPAddress.Parse(ip); var Endpoint = new IPEndPoint(ips, port); //tcpClient = new TcpClient(Endpoint); tcpClient.Connect(Endpoint); //tcpClient.Connect(ip, port); usrname = usr; password = pwd; stream = tcpClient.GetStream(); Writer = new StreamWriter(stream); Reader = new StreamReader(stream); AdvancedStream.SendMessage(ref Writer, $"L2KDB:Basic:OpenSession|{usr}|{pwd}"); string content = AdvancedStream.ReadToCurrentEnd(ref Reader); var Responses = content.Split(','); if (Responses[0] == "L2KDB:Basic:ConnectionAccept") { sessionID = Responses[1]; aes = new CustomedAES(); aesKey = Responses[2]; aesIV = Responses[3]; aes.Key = aesKey; aes.IV = aesIV; isConnected = true; return("[S]"); } else { return("[F]" + Responses[0]); } }
private static DatabaseDescription OpenDescription(string location, CryptographyCredential credential) { DatabaseDescription description = new DatabaseDescription(); if (credential == null) { var c = File.ReadAllText(location); var l = c.Split(':'); description.isPublic = bool.Parse(l[1]); } else if (credential.Key == "") { var c = File.ReadAllText(location); var l = c.Split(':'); description.isPublic = bool.Parse(l[1]); } else { var c = File.ReadAllText(location); CustomedAES aes = new CustomedAES(); aes.Key = credential.Key; aes.IV = credential.IV; var l = (aes.Decrypt(c)).Split(':'); description.isPublic = bool.Parse(l[1]); } return(new DatabaseDescription()); }
private static void SaveDescription(DatabaseDescription description, string location, CryptographyCredential credential) { if (credential == null) { File.WriteAllText(location, "PUBLIC:" + description.isPublic); } else if (credential.Key == "") { File.WriteAllText(location, "PUBLIC:" + description.isPublic); } else { CustomedAES aes = new CustomedAES(); aes.Key = credential.Key; aes.IV = credential.IV; File.WriteAllText(location, aes.Encrypt("PUBLIC:" + description.isPublic)); } }
static void Main(string[] args) { TcpClient client = new TcpClient(); client.Connect("127.0.0.1", 9341); var s = client.GetStream(); StreamWriter streamWriter = new StreamWriter(s); { AdvancedStream.SendMessage(ref streamWriter, "L2KDB:Basic:OpenSession|Creeper Lv|123456\r\nL2KDB:Basic:EndOfCurrentTransmission"); //streamWriter.Flush(); } String SessionID = ""; String Key = ""; String IV = ""; CustomedAES aes = new CustomedAES(); StreamReader streamReader = new StreamReader(s); { string receive; receive = AdvancedStream.ReadToCurrentEnd(ref streamReader); Console.WriteLine(receive); var blocks = receive.Split(':'); if (blocks[2].StartsWith("ConnectionAccept")) { var c = blocks[2].Split(','); SessionID = c[1]; Key = c[2]; IV = c[3]; aes.Key = Key; aes.IV = IV; Console.WriteLine($"Obtain:{SessionID}\t{Key}\t{IV}"); { AdvancedStream.SendMessage(ref streamWriter, "L2KDB:Basic:GetDatabaseVersion|" + SessionID, aes); Console.WriteLine(AdvancedStream.ReadToCurrentEnd(ref streamReader, aes)); } { AdvancedStream.SendMessage(ref streamWriter, "L2KDB:Basic:OpenDatabase,TestDataBase,,|" + SessionID, aes); Console.WriteLine(AdvancedStream.ReadToCurrentEnd(ref streamReader, aes)); } } else { } } //Console.WriteLine("Hello World!"); }
public static string ReadToCurrentEnd(ref StreamReader streamReader, CustomedAES aes, bool isHeaderRequired = true) { string content = ""; string tmp; while (true) { try { tmp = aes.Decrypt(streamReader.ReadLine()); if (tmp == "") { break; } if (tmp == "L2KDB:Basic:EndOfCurrentTransmission") { break; } if (content == "") { content += tmp; } else { content += Environment.NewLine + tmp; } if (content.StartsWith("L2KDB:")) { } else if (isHeaderRequired == true) { return("WRONG HEADER"); } } catch (Exception) { break; } } return(content); }
public static void SendMessage(ref StreamWriter streamWriter, string title, string content, CustomedAES aes) { string TitAes = aes.Encrypt(title); string res = aes.Encrypt(content); streamWriter.WriteLine(TitAes + streamWriter.NewLine + res + streamWriter.NewLine + aes.Encrypt("L2KDB:Basic:EndOfCurrentTransmission")); //Console.WriteLine(TitAes + streamWriter.NewLine+res + streamWriter.NewLine + aes.Encrypt("L2KDB:Basic:EndOfCurrentTransmission")); streamWriter.Flush(); }