随笔分类 -  c#加密

摘要:using System.Security.Cryptography;using System.IO;using System.text;/// <summary> /// 加密 /// </summary> /// <param name="str"></param> /// <param name="key"></param> /// <returns></returns> public static string Encode(string str, s 阅读全文
posted @ 2012-03-13 10:51 deepwishly 阅读(412) 评论(0) 推荐(0)
摘要:问:C# 加密后为何有两种结果的字符串?比如 cftea 的 MD5 加密后:有的人的结果是:c2e1861ca90e67ce1f9a62f9c27d8bdc有的人的结果是:wuGGHKkOZ84fmmL5wn2L3A答:这是对字节的两种不同表示结果。第一种是用十六进制表示的(FormsAuthentication.HashPasswordForStoringInConfigFile 就是这种,只是是大写的),具体请参见 BitConverter。如果用 BitConverter 时没有替换掉“-”的话,加密结果中还会有“-”分隔开。第二种是用的 Base64 编码,具体请参见 Base64 阅读全文
posted @ 2009-12-25 23:29 deepwishly 阅读(266) 评论(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 阅读(242) 评论(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 阅读(725) 评论(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 阅读(325) 评论(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 阅读(323) 评论(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 阅读(289) 评论(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 阅读(285) 评论(0) 推荐(0)