示例#1
0
        public static void Encrypt(string filePath, string cryptedTable, string cryptedField)
        {
            string    connectionString = "data source=" + filePath + ";New=True;UseUTF16Encoding=True";
            DataTable table            = new DataTable();

            string query = string.Format("SELECT * FROM {0} {1} {2}", cryptedTable, "", "");

            using (SQLiteConnection connect = new SQLiteConnection(connectionString))
            {
                SQLiteCommand     command = new SQLiteCommand(query, connect);
                SQLiteDataAdapter adapter = new SQLiteDataAdapter(command);

                adapter.Fill(table);

                int cryptFiled = table.Columns.IndexOf(cryptedField);

                for (int i = 0; i < table.Rows.Count; i++)
                {
                    byte[] array   = (byte[])table.Rows[i][cryptFiled];
                    byte[] encrypt = DPAPI.Encrypt(DPAPI.KeyType.UserKey, array, null, null);

                    table.Rows[i][cryptFiled] = encrypt;
                }
                adapter.Update(table);
            }
        }
示例#2
0
        public static DataTable DecryptFile(Process typeProces, string filePath, string cryptedTable, params string[] cryptedField)
        {
            if (!File.Exists(filePath))
            {
                throw new Exception("file " + filePath + " not found");
            }

            string    connectionString = "data source=" + filePath + ";New=True;UseUTF16Encoding=True";
            DataTable table            = new DataTable();

            string query = string.Format("SELECT * FROM {0} ", cryptedTable);

            using (SQLiteConnection connect = new SQLiteConnection(connectionString))
            {
                SQLiteCommand     command = new SQLiteCommand(query, connect);
                SQLiteDataAdapter adapter = new SQLiteDataAdapter(command);

                adapter.Fill(table);
                string description;
                byte[] entropy = null;

                int[] cryptFiled = new int[cryptedField.Length];
                for (int i = 0; i < cryptedField.Length; i++)
                {
                    cryptFiled[i] = table.Columns.IndexOf(cryptedField[i]);
                }


                for (int i = 0; i < table.Rows.Count; i++)
                {
                    for (int j = 0; j < cryptFiled.Length; j++)
                    {
                        byte[] array   = (byte[])table.Rows[i][cryptFiled[j]];
                        byte[] decript = typeProces == Process.Decrypt ? DPAPI.Decrypt(array, entropy, out description)
                            : DPAPI.Encrypt(DPAPI.KeyType.MachineKey, array, entropy, null);
                        table.Rows[i][cryptFiled[j]] = decript;
                    }
                }
            }
            return(table);
        }
示例#3
0
        public static void DecryptAndUpdateFile(string filePath, string cryptedTable, params string[] cryptedField)
        {
            if (!File.Exists(filePath))
            {
                throw new Exception("file " + filePath + " not found");
            }

            if (IsFileLocked.Check(new FileInfo(filePath)))
            {
                throw new Exception("File is locked. Please close browser");
            }


            string    connectionString = "data source=" + filePath + ";New=True;UseUTF8Encoding=True";
            DataTable table            = new DataTable();

            string query = string.Format("SELECT * FROM {0} ", cryptedTable);

            using (SQLiteConnection connect = new SQLiteConnection(connectionString))
            {
                SQLiteCommand     command = new SQLiteCommand(query, connect);
                SQLiteDataAdapter adapter = new SQLiteDataAdapter(command);

                connect.Open();
                adapter.Fill(table);
                string description;
                byte[] entropy = null;

                int[] cryptFiled = new int[cryptedField.Length];
                for (int i = 0; i < cryptedField.Length; i++)
                {
                    cryptFiled[i] = table.Columns.IndexOf(cryptedField[i]);
                }


                for (int i = 0; i < table.Rows.Count; i++)
                {
                    for (int j = 0; j < cryptFiled.Length; j++)
                    {
                        byte[] array = (byte[])table.Rows[i][cryptFiled[j]];

                        byte[] decript = DPAPI.Decrypt(array, entropy, out description);
                        //table.Rows[i][cryptFiled[j]] = decript ?? throw new Exception("all very bad");
                        //adapter.UpdateCommand = new SQLiteCommand("UPDATE " + cryptedTable + " SET password_value = @password_value WHERE origin_url=@origin_url ", adapter.SelectCommand.Connection);
                        //var updateParametrs = adapter.UpdateCommand.Parameters;
                        //updateParametrs.Add("@password_value", DbType.Binary,decript.Length);
                        //updateParametrs["@password_value"].Value = decript;
                        //updateParametrs.Add("@origin_url", DbType.String, 25, "origin_url");
                        //adapter.Update(table);

                        SQLiteCommand cmd = new SQLiteCommand(connect);
                        cmd.CommandText = "UPDATE " + cryptedTable + " SET password_value = @password_value WHERE origin_url=@origin_url ";
                        cmd.Prepare();
                        cmd.Parameters.Add("@password_value", DbType.Binary, decript.Length);
                        cmd.Parameters["@password_value"].Value = decript;
                        cmd.Parameters.Add("@origin_url", DbType.String, 25, "origin_url");
                        cmd.Parameters["@origin_url"].Value = table.Rows[i][0];
                        cmd.ExecuteNonQuery();

                        //SQLiteCommand cmd2 = new SQLiteCommand(connect);
                        //cmd2.CommandText = "SELECT Data FROM Images WHERE Id="+;
                        //byte[] data = (byte[])cmd.ExecuteScalar();
                    }
                }
                connect.Close();
                //MessageBox.Show(parsedPass);
                //if (cryptedTable == "logins")
                //{
                //    //adapter.UpdateCommand = new SQLiteCommand("UPDATE " + cryptedTable + " SET password_value = @password_value WHERE origin_url=@origin_url ", adapter.SelectCommand.Connection);
                //    //var updateParametrs = adapter.UpdateCommand.Parameters;
                //    //updateParametrs.Add("@password_value", DbType.Binary);
                //    //updateParametrs.Add("@origin_url", DbType.String,25, "origin_url");
                //    //adapter.Update(table);
                //    int i = 0;
                //    int index = table.Columns.IndexOf("password_value");
                //    MessageBox.Show(((byte[])table.Rows[0][index]).Length.ToString());

                //    while (i<table.Rows.Count)
                //    {
                //        adapter.UpdateCommand = new SQLiteCommand("UPDATE " + cryptedTable + " SET password_value = @password_value WHERE origin_url=@origin_url ", adapter.SelectCommand.Connection);
                //        var updateParametrs = adapter.UpdateCommand.Parameters;
                //        updateParametrs.AddWithValue("@password_value", table.Rows[i][index]);
                //        updateParametrs.Add("@origin_url", DbType.String, 25, "origin_url");
                //        adapter.Update(table);

                //        i++;
                //    };


                //}
                //else if (cryptedTable == "cookies")
                //{
                //    adapter.UpdateCommand = new SQLiteCommand("UPDATE " + cryptedTable + " SET encrypted_value = @encrypted_value", adapter.SelectCommand.Connection);
                //    var updateParametrs = adapter.UpdateCommand.Parameters;
                //    updateParametrs.Add("@encrypted_value", DbType.Binary);
                //    adapter.Update(table);
                //}
            }
        }