//执行代码添加
SqlCommand oCmd;
String strcon,str1,str2,str3;
try
{
strcon="Data Source=(local);";
strcon+="Initial catalog=Northwind;";
strcon+="user ID=sa;";
strcon+="password=;";
oCmd=new SqlCommand();
oCmd.Connection=new SqlConnection(strcon);
oCmd.Connection.Open();
str1=textBox1.Text;
str2=textBox2.Text;
str3=textBox3.Text;
oCmd.CommandText="insert into Customers(CustomerID,CompanyName,ContactName) values('"+str1+"','"+str2+"','"+str3+"')";
oCmd.ExecuteNonQuery
MessageBox.Show("Sql statatement 成功 ","button3_Click()");
oCmd.Connection.Close();
}
catch(Exception oExcept)
{
MessageBox.Show("error:"+oExcept.Message,"button3_Click()");
}
那吗怎么样对这段代码进行加密呢
也就是是用户看不到保存的数据库里的数据
然后又怎么样来解密呢
小妹谢谢了
谢谢
SqlCommand oCmd;
String strcon,str1,str2,str3;
try
{
strcon="Data Source=(local);";
strcon+="Initial catalog=Northwind;";
strcon+="user ID=sa;";
strcon+="password=;";
oCmd=new SqlCommand();
oCmd.Connection=new SqlConnection(strcon);
oCmd.Connection.Open();
str1=textBox1.Text;
str2=textBox2.Text;
str3=textBox3.Text;
oCmd.CommandText="insert into Customers(CustomerID,CompanyName,ContactName) values('"+str1+"','"+str2+"','"+str3+"')";
oCmd.ExecuteNonQuery
MessageBox.Show("Sql statatement 成功 ","button3_Click()");
oCmd.Connection.Close();
}
catch(Exception oExcept)
{
MessageBox.Show("error:"+oExcept.Message,"button3_Click()");
}
那吗怎么样对这段代码进行加密呢
也就是是用户看不到保存的数据库里的数据
然后又怎么样来解密呢
小妹谢谢了
谢谢
里边包含解密方式
http://blog.csdn.net/chengking/archive/2005/10/26/517171.aspx
using System.IO;
using System.Text;
using System.Security.Cryptography;/// <summary>
/// 一个简单的使用.NET非对称加密算法的例子
/// 本例的程序很简单,仅用于说明如何在.NET里面使用非对称(RSA)算法。
/// Kwanhong 2005.9
/// </summary>
class Class1
{
public static void Main(string[] args)
{
Class1 c=new Class1();
c.StartDemo();
} public void StartDemo()
{
//RSA的加解密过程:
// 有 rsa1 和 rsa2 两个RSA对象。
// 现在要 rsa2 发送一段信息给 rsa1, 则先由 rsa1 发送“公钥”给 rsa2
// rsa2 获取得公钥之后,用来加密要发送的数据内容。
// rsa1 获取加密后的内容后,用自己的私钥解密,得出原始的数据内容。 RSACryptoServiceProvider rsa1 = new RSACryptoServiceProvider();
RSACryptoServiceProvider rsa2 = new RSACryptoServiceProvider(); string publickey;
publickey=rsa1.ToXmlString(false); //导出 rsa1 的公钥 string plaintext;
plaintext="你好吗?这是用于测试的字符串。"; //原始数据
Console.WriteLine("原始数据是:\n{0}\n",plaintext); rsa2.FromXmlString(publickey); //rsa2 导入 rsa1 的公钥,用于加密信息 //rsa2开始加密
byte[] cipherbytes;
cipherbytes=rsa2.Encrypt(
Encoding.UTF8.GetBytes(plaintext),
false); /*//////////////////////////////////////////////*/
Console.WriteLine("加密后的数据是:");
for(int i=0; i< cipherbytes.Length; i++)
{
Console.Write("{0:X2} ",cipherbytes[i]);
}
Console.WriteLine("\n");
/*//////////////////////////////////////////////*/ //rsa1开始解密
byte[] plaintbytes;
plaintbytes = rsa1.Decrypt(cipherbytes,false); Console.WriteLine("解密后的数据是:");
Console.WriteLine(Encoding.UTF8.GetString(plaintbytes)); Console.ReadLine();
}}
using System.Security.Cryptography;
using System.Text;
using System.IO;/// <summary>
/// 使用对称加密的例子
/// </summary>
class Class2
{ static void Main(string[] args)
{
Class2 c=new Class2();
c.StartDemo();
} public void StartDemo()
{
//establish symmetric algorithm
SymmetricAlgorithm sa = Rijndael.Create(); //key and iv
sa.GenerateKey(); //产生随机的 (32*8) 位的密钥
//sa.GenerateIV(); //初始向量,在ECB模式里面可以不用IV
sa.Mode = CipherMode.ECB; //块处理模式
sa.Padding = PaddingMode.Zeros; //末尾数据块的填充模式
Console.WriteLine("密钥是:"); ///////////
for (int i=0; i<sa.Key.Length; i++) ///////////
{ ///////////
Console.Write("{0:X2} ",sa.Key[i]); ///////////
} ///////////
Console.WriteLine("\n"); ///////////
//establish crypto stream
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms,sa.CreateEncryptor(),CryptoStreamMode.Write); //
string plaintext; //原始文本
byte[] cipherbytes; //加密后的数据
byte[] finalbytes; //解密后的数据 plaintext="How are you? 这是一行文字。";
byte[] plainbytes = Encoding.UTF8.GetBytes(plaintext);
Console.WriteLine("原始文本是:\n{0}\n",plaintext);
//display plaint text byte array in hex format
Console.WriteLine("原始数据是:"); ///////////
for (int i=0; i<plainbytes.Length; i++) ///////////
{ ///////////
Console.Write("{0:X2} ",plainbytes[i]); ///////////
} ///////////
Console.WriteLine("\n"); /////////// //加密过程
cs.Write(plainbytes, 0, plainbytes.Length);
cs.Close();
cipherbytes = ms.ToArray();
ms.Close(); //display ciphertext byte array in hex format
Console.WriteLine("加密后的数据是:"); ///////////
for (int i=0; i<cipherbytes.Length; i++) ///////////
{ ///////////
Console.Write("{0:X2} ",cipherbytes[i]);///////////
} ///////////
Console.WriteLine("\n"); ///////////
//下面的为加密过程
ms=new MemoryStream(cipherbytes);
cs=new CryptoStream(ms,sa.CreateDecryptor(),CryptoStreamMode.Read);
finalbytes=new byte[plainbytes.Length];
cs.Read(finalbytes,0,plainbytes.Length); Console.WriteLine("解密后的数据是:"); ///////////
for (int i=0; i<finalbytes.Length; i++) ///////////
{ ///////////
Console.Write("{0:X2} ",finalbytes[i]); ///////////
} ///////////
Console.WriteLine("\n"); /////////// string finaltext=Encoding.UTF8.GetString(finalbytes); Console.WriteLine("解密后的文本是:\n{0}\n\n",finaltext );
Console.WriteLine("按任意键继续......");
Console.ReadLine();
}
}
里面有加密算法....或是自己写加密类.实现..也可以...
首先引用命名空间 System.Security.Cryptography;
//登录验证
string Username = this.textBoxUsername.Text;
string Password = this.textBoxPassword.Text;
MD5 md5 = new MD5CryptoServiceProvider();
byte[] result = md5.ComputeHash(System.Text.Encoding.Default.GetBytes(Password));//将密码转换为字节型存入数组
string str2 = "";
for(int i=0;i<result.Length;i++)
{
str2 += string.Format("{0:x}",result[i]);//将指定 String 中的格式项替换为指定数组中相应 Object 实例的值的文本等效项
}
/*************************************************************************
* Copyright(C) 2004-2005 。
************************************************************************/using System;
using System.Text;
using System.IO;
using System.Security.Cryptography;namespace CryptionDataMD5
{
/// <summary>
/// Make message digest based on MD5 for byte array and string
/// </summary>
/// <Author>Yao</Author>
/// <Date>2005/06/13</Date>
public class CryptionDataMD5
{
/// <summary>
/// Make message digest based on MD5 for byte array
/// </summary>
/// <param name="origin">source byte array</param>
/// <returns>message digest</returns>
public static string EncryptionByteData(byte[] SourceData)
{
try
{
//create a stream object
MemoryStream stream = new MemoryStream(SourceData);
//define hex table
const string HEX_TABLE = "0123456789abcdef";
//create a MD5CryptoServiceProvider object
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
//Calculate MD5 Checksum
byte[] hashData = md5.ComputeHash(stream); //convert to string
StringBuilder sb=new StringBuilder();
sb.Length =hashData.Length *2;
//make string sb
for(int i=0;i<hashData.Length ;i++)
{
sb[i*2]=HEX_TABLE[hashData[i]>>4];
sb[i*2+1]=HEX_TABLE[hashData[i] & 0xF];
}
return sb.ToString();
}
catch (Exception ex)
{
throw ex;
}
} /// <summary>
/// /// Make message digest based on MD5 for string
/// </summary>
/// <param name="origin">source string</param>
/// <returns>message digest</returns>
public static string EncryptionStringData(string SourceData)
{
string messageDigest = string.Empty;
try
{
byte[] sorData = Encoding.Default.GetBytes(SourceData);
messageDigest = EncryptionByteData(sorData);
}
catch(Exception ex)
{
throw ex;
}
return messageDigest;
}
}
}