public static void RSA_UnitTest() { for (int i = 0; i < 1000000; i++) { String[] keys = RSAHandler.GenerateKeyPair(1024); String publickey = keys[0]; String privatekey = keys[1]; String data = GenerateRandomString(78); string cypher = RSAHandler.Encrypt(publickey, data); string text = RSAHandler.Decrypt(privatekey, cypher); if (data != text) { Console.WriteLine("\n\nFailed!"); Console.WriteLine("Data: " + data); Console.WriteLine("Text: " + text); for (int a = 0; a < data.Length; a++) { if (data[a] != text[a]) { int x = data[a]; int y = text[a]; int z = 0; } } File.WriteAllText("err_dump.txt", data); return; } else { Console.WriteLine(i + " passed! " + data.Substring(0, (data.Length < 10) ? data.Length : 10)); } } Console.WriteLine("Test passed!"); }
private static void ConnectCallback(IAsyncResult ar) { try { // Retrieve the socket from the state object. Socket client = (Socket)ar.AsyncState; // Complete the connection. client.EndConnect(ar); Console.WriteLine("Socket connected to {0}", client.RemoteEndPoint.ToString()); //HS_Request r = new HS_Request("mtear", "NAMESET"); //Send(client, r.Json); string symkey = GenerateSymKey(); skey = symkey; LoginRequest lr = new LoginRequest("", "", symkey); string e = RSAHandler.Encrypt(public_key, lr.Json); Console.WriteLine("Sending data"); Send(client, e); // Signal that the connection has been made. connectDone.Set(); } catch (Exception e) { Console.WriteLine(e.ToString()); } }
void LoginConnectCallback(IAsyncResult ar) { try { Socket listener = (Socket)ar.AsyncState; listener.EndConnect(ar); sym_key = GenerateSymKey(); LoginRequest lr = new LoginRequest(username, password, sym_key); Debug.Log("Json: " + lr.Json); string e = RSAHandler.Encrypt(public_key, lr.Json); HS_SocketDataWorker sdw = new HS_SocketDataWorker(listener); sdw.SetCallback(new HS_SocketDataWorker.HS_PlayerCommandCallback(LoginMessageReceived)); sdw.Send(e); Debug.Log("Sending data: " + e); } catch (Exception e) { Debug.Log(e.ToString()); } }