[英]How to Encrypt/Secure MS Access 2007 database file in C#?
我有一個程序,可以使用簡單的數據庫密碼訪問/添加/刪除MS Access 2007文件中的條目。
但是我的目標是修改我的文件,以使其更加安全。 我想對我的文件進行加密,如果可能的話,可以使用用戶選擇的加密方式,並且只有在用戶提供正確的用戶名和密碼時,才可以訪問該文件。
我該怎么做? 如何加密文件? 我該如何做才能使用戶進行身份驗證?
請具體說明,並推薦使用假人示例:)
編輯:如果我將使用AES加密文件中的每個條目,那會更安全嗎? 我應該這樣做並讓數據庫文件沒有密碼嗎?
這是我現在訪問文件的方式:
// May be public so we can display
// content of file from different forms.
public void DisplayFileContent(string filePath)
{
// Creating an object allowing me connecting to the database.
OleDbConnection objOleDbConnection = new OleDbConnection();
// Creating command object.
objOleDbConnection.ConnectionString =
"Provider=Microsoft.ACE.OLEDB.12.0;" +
"Data Source=" + filePath + ";" +
"Persist Security Info=False;" +
"Jet OLEDB:Database Password=" + storedAuth.Password + ";";
OleDbCommand objOleDbCommand = new OleDbCommand();
objOleDbCommand.Connection = objOleDbConnection;
objOleDbCommand.CommandText = "Select * FROM PersonalData";
// Create a data reader.
OleDbDataReader readPersonalData;
try
{
// Open database connection.
objOleDbConnection.Open();
// Associate data reader with the command.
readPersonalData = objOleDbCommand.ExecuteReader();
// Counting all entries.
int countEntries = 0;
// Clearing the textbox before proceeding.
txtDisplay.Text = string.Empty;
if (readPersonalData.HasRows)
{
while (readPersonalData.Read())
{
// Count all entries read from the reader.
countEntries++;
txtDisplay.Text += "=== Entry ID: " + readPersonalData.GetValue(0) + " ===" + Environment.NewLine;
txtDisplay.Text += "Type: " + readPersonalData.GetValue(1) + Environment.NewLine;
if (!readPersonalData.IsDBNull(2)) txtDisplay.Text += "URL: " + readPersonalData.GetValue(2) + Environment.NewLine;
if (!readPersonalData.IsDBNull(3)) txtDisplay.Text += "Software Name: " + readPersonalData.GetValue(3) + Environment.NewLine;
if (!readPersonalData.IsDBNull(4)) txtDisplay.Text += "Serial Code: " + readPersonalData.GetValue(4) + Environment.NewLine;
if (!readPersonalData.IsDBNull(5)) txtDisplay.Text += "User Name: " + readPersonalData.GetValue(5) + Environment.NewLine;
if (!readPersonalData.IsDBNull(6)) txtDisplay.Text += "Password: " + readPersonalData.GetValue(6) + Environment.NewLine;
txtDisplay.Text += Environment.NewLine;
}
}
else
{
txtDisplay.Text = "There is nothing to display! You must add something so I can display something here.";
}
// Displaying number of entries in the status bar.
tsslStatus.Text = "A total of " + countEntries + " entries.";
// Selecting 0 character to make sure text
// isn't completly selected.
txtDisplay.SelectionStart = 0;
}
我的EncryptDecrypt.cs文件:
using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;
namespace Password_Manager
{
class EncryptDecrypt
{
string input, userName, password;
RijndaelManaged Crypto = new RijndaelManaged();
public EncryptDecrypt()
{
}
public EncryptDecrypt(string input, string userName, string password)
{
this.input = input;
this.userName = userName;
this.password = password;
}
public string Encrypt(string PlainText, string pass, string usrName)
{
string HashAlgorithm = "SHA1";
int PasswordIterations = 2;
string InitialVector = "OFRna73m*aze01xY";
int KeySize = 256;
this.input = PlainText;
if (string.IsNullOrEmpty(PlainText))
return "";
byte[] InitialVectorBytes = Encoding.ASCII.GetBytes(InitialVector);
byte[] SaltValueBytes = Encoding.ASCII.GetBytes(usrName);
byte[] PlainTextBytes = Encoding.UTF8.GetBytes(PlainText);
PasswordDeriveBytes DerivedPassword = new PasswordDeriveBytes(pass, SaltValueBytes, HashAlgorithm, PasswordIterations);
byte[] KeyBytes = DerivedPassword.GetBytes(KeySize / 8);
RijndaelManaged SymmetricKey = new RijndaelManaged();
SymmetricKey.Mode = CipherMode.CBC;
byte[] CipherTextBytes = null;
using (ICryptoTransform Encryptor = SymmetricKey.CreateEncryptor(KeyBytes, InitialVectorBytes))
{
using (MemoryStream MemStream = new MemoryStream())
{
using (CryptoStream CryptoStream = new CryptoStream(MemStream, Encryptor, CryptoStreamMode.Write))
{
CryptoStream.Write(PlainTextBytes, 0, PlainTextBytes.Length);
CryptoStream.FlushFinalBlock();
CipherTextBytes = MemStream.ToArray();
MemStream.Close();
CryptoStream.Close();
}
}
}
SymmetricKey.Clear();
return Convert.ToBase64String(CipherTextBytes);
}
public string Decrypt(string CipherText, string pass, string usrName)
{
string HashAlgorithm = "SHA1";
int PasswordIterations = 2;
string InitialVector = "OFRna73m*aze01xY";
int KeySize = 256;
if (string.IsNullOrEmpty(CipherText))
return "";
byte[] InitialVectorBytes = Encoding.ASCII.GetBytes(InitialVector);
byte[] SaltValueBytes = Encoding.ASCII.GetBytes(usrName);
byte[] CipherTextBytes = Convert.FromBase64String(CipherText);
PasswordDeriveBytes DerivedPassword = new PasswordDeriveBytes(pass, SaltValueBytes, HashAlgorithm, PasswordIterations);
byte[] KeyBytes = DerivedPassword.GetBytes(KeySize / 8);
RijndaelManaged SymmetricKey = new RijndaelManaged();
SymmetricKey.Mode = CipherMode.CBC;
byte[] PlainTextBytes = new byte[CipherTextBytes.Length];
int ByteCount = 0;
using (ICryptoTransform Decryptor = SymmetricKey.CreateDecryptor(KeyBytes, InitialVectorBytes))
{
using (MemoryStream MemStream = new MemoryStream(CipherTextBytes))
{
using (CryptoStream CryptoStream = new CryptoStream(MemStream, Decryptor, CryptoStreamMode.Read))
{
ByteCount = CryptoStream.Read(PlainTextBytes, 0, PlainTextBytes.Length);
MemStream.Close();
CryptoStream.Close();
}
}
}
SymmetricKey.Clear();
return Encoding.UTF8.GetString(PlainTextBytes, 0, ByteCount);
}
}
}
絕對不要加密數據並將加密后的數據保存在數據庫中,您將最終遇到查詢,例如select field1, field2 from table where field1 = '$WDFV%$:@@{#%SAsdasdh#!fjdkj'
字段必須是文本。 它大大削弱了使用RDBMS的目的。
訪問DB可以用密碼保護和加密的看到這這些在說明2010年,但也有一個鏈接到2007年。
但是,如果您將其用作winforms gui的后端,並且您不希望用戶在其中鍵入密碼,則必須將密碼存儲在某個地方,並且確定的用戶如果擁有密碼,則可以提取該密碼。知識。
如果保護敏感數據免受未授權用戶的侵擾是優先考慮的事情,那么訪問是一個壞選擇,如果現在在項目中更改時為時已晚,則以后不要使用它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.