// Opens an unencrypted database
SQLiteConnection cnn = newSQLiteConnection("Data Source=c:\\test.db3")
cnn.Open()
// Encrypts the database. The connection remains valid and usable afterwards.
cnn.ChangePassword("mypassword")
解密一个已加密的数据库调用l ChangePassword() 将参数设为 NULL or "" :
// Opens an encrypted database
SQLiteConnection cnn = newSQLiteConnection("Data Source=c:\\test.db3Password=mypassword")
cnn.Open()
// Removes the encryption on an encrypted database.
cnn.ChangePassword("")
要打开一个已加密的数据库或者新建一个加密数据库,在打开或者新建前调用SetPassword()函数
// Opens an encrypted database by calling SetPassword()
SQLiteConnection cnn = newSQLiteConnection("Data Source=c:\\test.db3")
cnn.SetPassword(newbyte[] { 0xFF, 0xEE, 0xDD, 0x10, 0x20, 0x30 })
cnn.Open()
// The connection is now usable
Sqlite数据库的加密
1、创建空的sqlite数据库。
//数据库名的后缀你可以直接指定,甚至没有后缀都可以
//方法一:创建一个空sqlite数据库,用IO的方式
FileStream fs = File.Create(“c:\\test.db“)
//方法二:用SQLiteConnection
SQLiteConnection.CreateFile(“c:\\test.db“)
创建的数据库是个0字节的文件。
2、创建加密的空sqlite数据库
//创建一个密码为password的空的sqlite数据库
SQLiteConnection.CreateFile(“c:\\test2.db“)
SQLiteConnection cnn = new SQLiteConnection(“Data Source=c:\\test2.db“)
SQLiteConnection cnn = new SQLiteConnection(“Data Source=D:\\test2.db“)
cnn.Open()
cnn.ChangePassword(“password“)
3、给未加密的数据库加密
SQLiteConnection cnn = new SQLiteConnection(“Data Source=c:\\test.db“)
cnn.Open()
cnn.ChangePassword(“password“)
4、打开加密sqlite数据库
//方法一
SQLiteConnection cnn = new SQLiteConnection(“Data Source=c:\\test2.db“)
cnn.SetPassword(“password“)
cnn.Open()
//方法二
SQLiteConnectionStringBuilder builder = new SQLiteConnectionStringBuilder()
builder.DataSource = @”c:\test.db“
builder.Password = @”password“
SQLiteConnection cnn = new SQLiteConnection(builder.ConnectionString)
cnn .Open()
分页
select * from messages limit 10,100
表示跳过10行,取100行的返回结果。
加密有很多中,常用的有MD5C# md5加密(上)
string a//加密前数据
string b//加密后数据
b=System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(a,"MD5")
using System
using System.Security.Cryptography
方法2
public static string GetMD5(string myString)
{
MD5 md5 = new MD5CryptoServiceProvider()
byte[] fromData = System.Text.Encoding.Unicode.GetBytes(myString)
byte[] targetData = md5.ComputeHash(fromData)
string byte2String = null
for (int i=0 i<targetData.Length i++)
{
byte2String += targetData[i].ToString("x")
}
return byte2String
}
using System.Security.Cryptography
/// <summary>
/// 给一个字符串进行MD5加密
/// </summary>
/// <param name="strText">待加密字符串</param>
/// <returns>加密后的字符串</returns>
public static string MD5Encrypt(string strText)
{
MD5 md5 = new MD5CryptoServiceProvider()
byte[] result = md5.ComputeHash(System.Text.Encoding.Default.GetBytes(strText))
return System.Text.Encoding.Default.GetString(result)
}
C# MD5加密
using System.Security.Cryptography
private void btnOK_Click(object sender, System.EventArgs e)
{
string strConn = "server=192.168.0.51database=chenghengUser id=sapassword=123"
if(texName.Text.Trim()=="")
{
this.RegisterStartupScript("sf","<script language='javascript'>alert('用户名不能为空')document.all('texName').focus()</script>")
return
}
else if(texPassword.Text.Trim()=="")
{
this.RegisterStartupScript("sfs","<script language='javascript'>alert('密码不能为空')document.all('texPassword').focus()</script>")
return
}
else
{
//将获取的密码加密与数据库中加了密的密码相比较
byte[] by = md5.ComputeHash(utf.GetBytes(texPassword.Text.Trim()))
string resultPass = System.Text.UTF8Encoding.Unicode.GetString(by)
conn.ConnectionString=strConn
SqlCommand comm = new SqlCommand()
string name = texName.Text.Trim().ToString()
comm.CommandText="select Ruser_pwd,Ruser_nm from Ruser where Accountno = @name"
comm.Parameters.Add("@name",SqlDbType.NVarChar,40)
comm.Parameters["@name"].Value=name
try
{
conn.Open()
comm.Connection=conn
SqlDataReader dr=comm.ExecuteReader()
if(dr.Read())
{
//用户存在,对密码进行检查
if(dr.GetValue(0).Equals(resultPass))
{
string user_name=dr.GetValue(1).ToString()
string user_Accountno=texName.Text.Trim()
Session["logon_name"]=user_name
Session["logon_Accountno"]=user_Accountno
//登录成功,进行页面导向
}
else
{
this.RegisterStartupScript("wp","<script language='javascript'>alert('密码错误,请检查。')</script>")
}
}
else
{
this.RegisterStartupScript("nu","<script language=javascript>alert('用户名不存在,请检查。')</script>")
}
}
catch(Exception exec)
{
this.RegisterStartupScript("wc","<script language=javascript>alert('网络连接有异,请稍后重试。')</script>")
}
finally
{
conn.Close()
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)