public void EncipherTest() { uint[] keys = { 12, 23, 34, 45, 56, 67, 78, 89, }; byte[] expected = new byte[] { 96, 97, 98, 99 }; var encrypter = new EncryptionAES(); byte[] ciphertext = encrypter.Encrypt(expected, keys); byte[] actual = encrypter.Decrypt(ciphertext, keys); CollectionAssert.AreEqual(expected, actual); }
/// <summary> /// Send request Data to database server /// </summary> /// <param Name="req"></param> /// <returns>Task<Response> response Data from database</Response></returns> public async Task <Response> SendToServer(Request req) { Response res = null; try { if (!auth_bypass) { Debug.WriteLine("[Encrypted Request]"); byte[] serializeReq = Serialization.SerializeData(req); string encryptedSerializeReq = EncryptionAES.Encrypt(serializeReq, masterKey, iv); await sw.WriteLineAsync(encryptedSerializeReq); } else { Debug.WriteLine("[Unencrypted Request]"); await sw.WriteLineAsync(Serialization.SerializeObject(req)); } sw.Flush(); if (!auth_bypass) { string encryptedSerializeRes = sr.ReadLine(); Debug.WriteLine("[Encrypted Response]"); byte[] serializeRes = EncryptionAES.Decrypt(encryptedSerializeRes, masterKey, iv); res = (Response)Serialization.DeserializeData(serializeRes); } else { res = (Response)Serialization.DeserializeObject(sr.ReadLine()); Debug.WriteLine("[Unencrypted Response]"); } } catch (Exception e) { Console.WriteLine(e); sr.Close(); sw.Close(); client.Close(); } return(res); }
public static Response Create(string password) { Console.WriteLine(); Response res = new Response(); string passwordHashIncludeSalt = HashingSHA256.passwordHash(password); string[] s = passwordHashIncludeSalt.Split(','); string hashedPassword = s[0]; Console.WriteLine("hashed password:"******"salt:" + salt); string encryptSalt = EncryptionAES.Encrypt(salt); //byte[] saltBytes = Encoding.UTF8.GetBytes(salt); //byte[] hashPasswordByte = Encoding.UTF8.GetBytes(hashedPassword); // bool verify = hashing.verifyHash(pwd, hashedPassword); //need to do asymmetric encryption to store the keys! try { if (DatabaseConnection.conn != null) { //Console.WriteLine(DatabaseConnection.conn + "a1"); DatabaseConnection.conn.Open(); MySqlCommand comm = DatabaseConnection.conn.CreateCommand(); //Object usrObj = Data.userObj; // comm.CommandText = " INSERT INTO user(username,email, passwordHash, salt)VALUES (@username, @email, @passwordHash, @salt)"; comm.CommandText = " INSERT INTO checking(passwordHash, salt)VALUES ( @passwordHash, @salt)"; comm.Parameters.AddWithValue("@passwordHash", encryptHashedPassword); comm.Parameters.AddWithValue("@salt", encryptSalt); //comm.Parameters.Add("@salt", MySqlDbType.VarBinary).Value = saltBytes; comm.ExecuteNonQuery(); res.Success = true; Console.WriteLine(res.Success); } } catch (Exception e) { //Console.WriteLine(DatabaseConnection.conn + "b2"); Console.WriteLine(e); res.Success = false; res.Reason = e.Message; } finally { if (DatabaseConnection.conn != null) { //Console.WriteLine(DatabaseConnection.conn + "c3"); DatabaseConnection.conn.Close(); } else { Console.WriteLine("dbconn is null"); } } Console.WriteLine("RESPONSE FROM DATABASE"); Console.WriteLine(res); return(res); }
/// <summary> /// Constantly listening to client request and respond to it accordingly /// </summary> /// <param name="sr"></param> /// <param name="sw"></param> private async void ListeningToClient(StreamReader sr, StreamWriter sw) { while (true) { Request req; Response res; if (!auth_bypass) { string encryptedSerializedReq = await sr.ReadLineAsync(); Console.WriteLine("[Encrypted Request]"); byte[] serializedReq = EncryptionAES.Decrypt(encryptedSerializedReq, masterKey, iv); req = (Request)Serialization.DeserializeData(serializedReq); } else { req = (Request)Serialization.DeserializeObject(await sr.ReadLineAsync()); Console.WriteLine("[Unencrypted Request]"); } //Redirect r = new Redirect(); res = Redirect.redirection(req); /* * To get object Data from request * <Class> objData = (<Class>)req.Data; * * Process the request Data * TODO: Process (Symmetric Encryption and decryption) and retrieve/insert Data * * Create a reponse obj * Response res = new Response() * { * Data = <object>, * Flag = <Flag>, * Reason = <Include Reason if necessary> * Sucess = <true/false> * } */ //LoginAccount acc = (LoginAccount)req.Data; //Console.WriteLine("password input: " + acc.password); //Constants c = new Constants(); //res = new Response() //{ // Data = new House() // { // Address = "Blk 912, Hougang Street 91, #07-42", // Country = "Singapore", // PostalCode = "S530912" // }, // Flag = 1, // Reason = "SENDING BACK", // Success = true //}; if (!auth_bypass) { Console.WriteLine("[Encrypted Response]"); byte[] seralizedRes = Serialization.SerializeData(res); string encryptedSerializedRes = EncryptionAES.Encrypt(seralizedRes, masterKey, iv); await sw.WriteLineAsync(encryptedSerializedRes); } else { Console.WriteLine("[Unencrypted Response]"); await sw.WriteLineAsync(Serialization.SerializeObject(res)); } sw.Flush(); Console.WriteLine("[Sending Response]"); } }