Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        /// <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);
        }
Ejemplo n.º 3
0
        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);
        }
Ejemplo n.º 4
0
        /// <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]");
            }
        }
Ejemplo n.º 5
0
        public static Response Login(string password)
        {
            Response res         = new Response();
            Response resRetrieve = new Response();
            int      checkID     = 1;

            try
            {
                Console.WriteLine("HI IM IN LOGIN");
                if (DatabaseConnection.conn != null)
                {
                    DatabaseConnection.conn.Open();
                    MySqlCommand comm = DatabaseConnection.conn.CreateCommand();

                    comm.CommandText = "SELECT passwordHash, salt from checking WHERE checkID = @checkID";
                    comm.Parameters.AddWithValue("@checkID", checkID);

                    using (var reader = comm.ExecuteReader())
                    {
                        Console.WriteLine("Breakpoint X");


                        while (reader.Read())
                        {
                            Console.WriteLine("Breakpoint Y");

                            string encryptedCorrectPasswordHash = reader.GetString("passwordHash");
                            string correctPasswordHash          = EncryptionAES.Decrypt(encryptedCorrectPasswordHash);
                            string encryptedSalt = reader.GetString("salt");
                            string salt          = EncryptionAES.Decrypt(encryptedSalt);

                            Console.WriteLine("Correct password hash: " + correctPasswordHash);
                            Console.WriteLine("Salt: " + salt);
                            bool verifyHash = HashingSHA256.verifypasswordHash(password, salt, correctPasswordHash);
                            Console.WriteLine("Verifying hash: " + verifyHash);
                            if (verifyHash)
                            {
                                Console.Write("verified");


                                res.Success = true;

                                Console.WriteLine("Breakpoint Z1 == Login success!");
                            }
                            else
                            {
                                res.Success = false;
                                res.Reason  = "Unmatch password";
                                Console.WriteLine("Breakpoint Z2: unmatch password");
                            }
                        }
                    }

                    Console.WriteLine("Breakpoint A");

                    DatabaseConnection.conn.Close();

                    Console.WriteLine("Breakpoint B");
                }
            }

            catch (Exception e)
            {
                res.Success = false;
                res.Reason  = e.Message;
                Console.WriteLine(e.Message);
                Console.WriteLine("Breakpoint C");
            }
            finally
            {
                Console.WriteLine("Breakpoint D");
                if (DatabaseConnection.conn != null)
                {
                    Console.WriteLine("Breakpoint E: connection not null currently closing");
                    DatabaseConnection.conn.Close();
                }

                Console.WriteLine("Breakpoint F: connection closed");
            }
            Console.WriteLine(res.Success);

            return(res);
        }