摘要: 问:C# 加密后为何有两种结果的字符串?比如 cftea 的 MD5 加密后:有的人的结果是:c2e1861ca90e67ce1f9a62f9c27d8bdc有的人的结果是:wuGGHKkOZ84fmmL5wn2L3A答:这是对字节的两种不同表示结果。第一种是用十六进制表示的(FormsAuthentication.HashPasswordForStoringInConfigFile 就是这种,只是是大写的),具体请参见 BitConverter。如果用 BitConverter 时没有替换掉“-”的话,加密结果中还会有“-”分隔开。第二种是用的 Base64 编码,具体请参见 Base64 阅读全文
posted @ 2009-12-25 23:29 deepwishly 阅读(253) 评论(0) 推荐(0) 编辑
摘要: 在 System.Security.Cryptography 中,我们可以看到有许多类,有些类还很相似,比如:System.Security.Cryptography.SHA1System.Security.Cryptography.SHA1ManagedSystem.Security.Cryptography.SHA1CryptoServiceProvider这三个类有什么关系呢?SHA1 是抽象类,SHA1Managed 和 SHA1CryptoServiceProvider 继承于 SHA1。SHA1Managed 和 SHA1CryptoServiceProvider 有什么区别呢?S 阅读全文
posted @ 2009-12-25 23:27 deepwishly 阅读(235) 评论(0) 推荐(0) 编辑
摘要: 要安全地存储密钥,应将密钥存放在密钥容器中,而不是明文存放在文件中。如果您不了解密钥容器,可以参照 MSDN 上的 了解计算机级别和用户级别的 RSA 密钥容器。CspParameters 的名称空间是:System.Security.Cryptography创建和读取密钥容器CspParameters cp = new CspParameters();cp.KeyContainerName = ContainerName;RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cp);创建和读取密钥容器都使用上述代码:如果密钥容 阅读全文
posted @ 2009-12-25 23:26 deepwishly 阅读(671) 评论(0) 推荐(0) 编辑
摘要: Rijndael 属对称加密,对称加密在加密和解密时都使用相同的密钥。2000 年 10 月,NIST 选择 Rijndael(发音为 "Rhine dale")作为 AES 算法,用以取代 DES。Rijndael 的名称空间是:System.Security.Cryptographybyte[] plaintextBuffer = System.Text.Encoding.UTF8.GetBytes("明文"); //加密Rijndael rijndael = Rijndael.Create();ICryptoTransform transform 阅读全文
posted @ 2009-12-25 23:25 deepwishly 阅读(314) 评论(0) 推荐(0) 编辑
摘要: TripleDES 属对称加密,对称加密在加密和解密时都使用相同的密钥,速度快。TripleDESCryptoServiceProvider 的名称空间是:System.Security.Cryptographybyte[] plaintextBuffer = System.Text.Encoding.UTF8.GetBytes("明文");//加密TripleDESCryptoServiceProvider tripleDES = new TripleDESCryptoServiceProvider();ICryptoTransform transform = tripl 阅读全文
posted @ 2009-12-25 23:24 deepwishly 阅读(734) 评论(0) 推荐(0) 编辑
摘要: RSA 属不对称加密,使用一个公钥一个私钥,公钥可以公开用以加密,私钥严格保密用于解密,RSA 适合于数据量不大的加密,比如加密对称加密的密钥。RSACryptoServiceProvider 的名称空间是:System.Security.CryptographyRSACryptoServiceProvider rsaSend = new RSACryptoServiceProvider();string plaintext = "明文"; //明文byte[] ciphertext = rsaSend.Encrypt(System.Text.Encoding.UTF8.G 阅读全文
posted @ 2009-12-25 23:23 deepwishly 阅读(318) 评论(0) 推荐(0) 编辑
摘要: RSA 实际应用中是:接收方产生公钥和私钥,发送方用其公钥加密,再把加密后的内容发送给接收方。CspParameters 的名称空间是:System.Security.CryptographyCspParameters cpSend = new CspParameters(); //Csp = Cryptography Service ProviderCspParameters cpReceive = new CspParameters();cpSend.KeyContainerName = "SendTestContainer";cpReceive.KeyContaine 阅读全文
posted @ 2009-12-25 23:23 deepwishly 阅读(284) 评论(0) 推荐(0) 编辑
摘要: 创建散列码的方法非常多,即使是同一种散列算法也可以通过许多类来实现,前面章节介绍的算一种,下面再介绍一种。以 SHA1 为例:string plaintext = "明文";byte[] srcBuffer = System.Text.Encoding.UTF8.GetBytes(plaintext);HashAlgorithm hash = HashAlgorithm.Create("SHA1"); //将参数换成“MD5”,则执行 MD5 加密。不区分大小写。byte[] destBuffer = hash.ComputeHash(srcBuffer 阅读全文
posted @ 2009-12-25 23:21 deepwishly 阅读(271) 评论(0) 推荐(0) 编辑
摘要: 在 ASP.NET 中可以非常方便地执行 MD5 或 SHA1 加密。<%@ Import Namespace="System.Web.Security" %>FormsAuthentication.HashPasswordForStoringInConfigFile只需要两步,第一步引入名称空间(该名称空间也可以省略引用),第二步执行加密函数。FormsAuthentication.HashPasswordForStoringInConfigFile 有两个参数:第一个参数是要加密的字符串;第二个参数可选值有 MD5 和 SHA1,表示使用哪种加密方法。返回加 阅读全文
posted @ 2009-12-25 23:20 deepwishly 阅读(192) 评论(0) 推荐(0) 编辑
摘要: using System;using System.Collections;using System.Configuration;using System.Data;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq;using Sy 阅读全文
posted @ 2009-12-25 23:14 deepwishly 阅读(164) 评论(0) 推荐(0) 编辑
摘要: 数据库操作一、最简单的。string connString = "Data Source=(local);Initial Catalog=db;User Id=userId;Password=password";using (SqlConnection conn = new SqlConnection(connString)){ conn.Open(); string sql = @"delete from table1"; using (SqlCommand cmd = new SqlCommand(sql, conn)) { cmd.ExecuteN 阅读全文
posted @ 2009-12-25 23:10 deepwishly 阅读(145) 评论(0) 推荐(0) 编辑
摘要: 可以把投影、聚合、筛选、排序组合起来使用。int[] arr = { 1, 2, 3, 7, 8, 9, 6, 5, 4 };var items = arr.Where(i => i > 3).OrderBy(i => i);把 > 3 的元素按升序排列,显示为:456789 阅读全文
posted @ 2009-12-25 22:58 deepwishly 阅读(118) 评论(0) 推荐(0) 编辑
摘要: OrderByint[] arr = { 1, 2, 3, 7, 8, 9, 6, 5, 4 };var items = arr.OrderBy(i => i);按 1、2、3……排序。OrderByDescendingint[] arr = { 1, 2, 3, 7, 8, 9, 6, 5, 4 };var items = arr.OrderByDescending(i => i);按 9、8、7……排序。Reverseint[] arr = { 1, 2, 3, 7, 8, 9, 6, 5, 4 };var items = arr.Reverse();倒序,本例中按 4、5、6 阅读全文
posted @ 2009-12-25 22:55 deepwishly 阅读(277) 评论(0) 推荐(0) 编辑
摘要: Whereint[] arr = { 1, 2, 3, 7, 8, 9, 6, 5, 4 };var items = arr.Where(i => i > 3 && i < 8);foreach (var m in items){ Response.Write(m.ToString() + "<br>");}Lambda 表达式 i => i > 3 && i < 8 表示只选择 > 3 且 < 8 的元素,显示为:7654 阅读全文
posted @ 2009-12-25 22:50 deepwishly 阅读(147) 评论(0) 推荐(0) 编辑
摘要: 我们一般说的 LINQ 是指 LINQ 表达式,详见 LINQ 步步学,但除了表达式以外,LINQ 查询操作更方便,凡是实现了接口 IEnumerable<T> 或 IQueryable<T> 的,都可以用 LINQ 查询操作。方法一览int[] arr = { 1, 2, 3, 7, 8, 9, 6, 5, 4 };Response.Write(arr.Count() + "<br>"); // 元素数量,这里为 9Response.Write(arr.Sum() + "<br>"); // 元素总和,这 阅读全文
posted @ 2009-12-25 22:39 deepwishly 阅读(173) 评论(0) 推荐(0) 编辑
摘要: 我们一般说的 LINQ 是指 LINQ 表达式,详见 LINQ 步步学,但除了表达式以外,LINQ 查询操作更方便,凡是实现了接口 IEnumerable<T> 或 IQueryable<T> 的,都可以用 LINQ 查询操作。Selectint[] arr = { 1, 2, 3, 7, 8, 9, 6, 5, 4 };var items = arr.Select(i => i);foreach (var m in items){ Response.Write(m.ToString() + "<br>");}显示:12378965 阅读全文
posted @ 2009-12-25 22:31 deepwishly 阅读(201) 评论(0) 推荐(0) 编辑
摘要: XML 是个好东西,但查询 XML 真的不方便,自从有了 LINQ to XML 后,一切改变了。假设有 XML 文件如下:<?xml version="1.0" encoding="utf-8" ?><company> <name engName="cftea">千一网络</name> <employees> <employee> <name age="29">张龙</name> </employee> & 阅读全文
posted @ 2009-12-25 22:25 deepwishly 阅读(139) 评论(0) 推荐(0) 编辑
摘要: 如果您正在使用 SQL Server 数据库,那么使用 LINQ to SQL 将为您带来极大的方便。首先,在 App_Code 中新建一个“LINQ to SQL 类”项,我们命名为 DataClasses.dbml,并打开它。其次,在数据库资源管理器中连接数据库,并展开“表”,然后将某个/些表(我们这里是 TranTable)拖到 DataClasses.dbml 的界面中,也可以只拖需要的字段,保存 DataClasses.dbml。最后,使用 LINQ 吧,代码如下:DataClassesDataContext dc = new DataClassesDataContext();var 阅读全文
posted @ 2009-12-25 22:14 deepwishly 阅读(146) 评论(0) 推荐(0) 编辑
摘要: 前面谈了弱类型 DataSet,可以发现它不能受 IntelliSense 支持,我们仍然需要记住字段名称,尤其是利用 SQL 语句填充 DataSet 时。那么能不能让 LINQ to DataSet 更高级点呢?可以,用强类型 DataSet。首先,在 App_Code 中新建一个“数据集”项,我们命名为 DataSet1.xsd,并打开它。其次,在数据库资源管理器中连接数据库,并展开“表”,然后将某个/些表(我们这里是 TranTable)拖到 DataSet1.xsd 的界面中,也可以只拖需要的字段,保存 DataSet1.xsd。最后,使用 LINQ 吧,代码如下:DataSet1 阅读全文
posted @ 2009-12-25 22:08 deepwishly 阅读(224) 评论(0) 推荐(0) 编辑
摘要: 代码DataSet ds = new DataSet();ds.Tables.Add();ds.Tables[0].Columns.Add("Id", System.Type.GetType("System.Int32"));ds.Tables[0].Columns.Add("Text");for (int i = 0; i < 10; i++){ DataRow dr = ds.Tables[0].Rows.Add(); dr["Id"] = i; dr["Text"] = " 阅读全文
posted @ 2009-12-25 22:06 deepwishly 阅读(164) 评论(0) 推荐(0) 编辑