C# 利用SQLite對.DB和.logdb加密和解密和SQLite建立資料庫

來源:互聯網
上載者:User

標籤:db檔案   catch   util   display   sts   blog   []   control   pos   

1.最近研究了下利用SQLite為db檔案簡單的加密和解密

 1  private static SQLiteConnection GetConnection() 2         { 3             SQLiteConnection conn; 4             string password = dataProvider.ConfigSearch(ConfigCode.SYS_File_ENCRYPTION_PASSWORD, true)[0].Value; 5             string dbFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), ConfigCode.CLIENT_DATA_PATH); 6             string dbPath = Path.GetDirectoryName(dbFile); 7             if (!Directory.Exists(dbPath)) 8             { 9                 Directory.CreateDirectory(dbPath);10             }11             if (!File.Exists(dbFile))12             {13                 //資料庫不存在,則建立14                 SQLiteConnection.CreateFile(dbFile);15                 CommonUtils.SetAccessControlList(dbFile);16                 conn = new SQLiteConnection(string.Format("Data Source={0};Pooling=true;FailIfMissing=false", dbFile));17                 conn.Open();18                 conn.ChangePassword(password);19                 if (string.IsNullOrEmpty(install_db_sql))20                 {21                     install_db_sql = Path.Combine(Path.GetDirectoryName(typeof(SQLiteProvider).Assembly.Location), "setup.sql");22                 }23 24                 foreach (string line in File.ReadAllText(install_db_sql).Split(new char[] { ‘;‘ }))25                 {26                     if (String.IsNullOrEmpty(line))27                     {28                         continue;29                     }30                     SQLiteCommand cmd = conn.CreateCommand();31                     cmd.CommandText = line;32                     cmd.CommandType = CommandType.Text;33                     cmd.ExecuteNonQuery();34                     cmd.Dispose();35                 }36             }37             else38             {39                 try40                 {41                     conn = new SQLiteConnection(string.Format("Data Source={0};Pooling=true;FailIfMissing=false", dbFile));42                     conn.SetPassword(password);43                     conn.Open();44                     conn.ChangePassword(password);45                 }46                 catch (Exception)47                 {48                     conn = new SQLiteConnection(string.Format("Data Source={0};Pooling=true;FailIfMissing=false", dbFile));49                     conn.Open();50                 }51                 52             }53             return conn;54         }
View Code

 這裡為了兼顧以前沒有加密的資料庫檔案,特意做了處理,如果資料庫不存在,直接建立,加密就不存在這個問題,主要是針對已經存在的資料庫有個try...catch的異常處理,個人已驗證,可以直接拿去用。

 

C# 利用SQLite對.DB和.logdb加密和解密和SQLite建立資料庫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.