Пример #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);
        }