C#.NET中对称和非对称加密、解密方法汇总--亲测可用
在安全性要求比较高的系统中都会涉及到数据的加密、解密。.NET为我们封装了常用的加密算法,例如:MD5,DES,RSA等。有可逆加密,也有非可逆加密;有对称加密,也有非对称加密。
加密、解密一般会用在软件的注册码,系统密码,通讯中。
今天我就来分享,汇总一下C#.NET加密、解密的实现方法。
一、不可逆加密
不可逆加密一般不会涉及到解密。也就是是加密之后的密文不能还原成原来的明文。这种算法一般用于生成自信摘要,确保数据的完整性及防篡改。
使用FormsAuthentication类加密
using System.Web.Security;
namespace EncyptDemo{
public class WebEncryptor{
/// <summary>
/// SHA1加密字符串
/// </summary>
/// <param name="source">源字符串</param>
/// <returns>加密后的字符串</returns>
public string SHA1(string source){
return FormsAuthentication.HashPasswordForStoringInConfigFile(source, "SHA1");
}
/// <summary>
/// MD5加密字符串
/// </summary>
/// <param name="source">源字符串</param>
/// <returns>加密后的字符串</returns>
public string MD5(string source){
return FormsAuthentication.HashPasswordForStoringInConfigFile(source, "MD5");
}
}
}
使用MD5CryptoServiceProvider类生成MD5字符串
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
|
二、可逆加密、解密
可逆加密,这种算法加密之后的密码文可以解密成原来的明文。比如通讯的时候,数据的发送方和接收方约定好加密和解密的key,发送放把原始数据加密之后开始发送,接收放收到数据之后开始解密,把密文原来成明文。
可逆加密又分为对称加密和非对称加密。所谓对称加密就是加密和解密的算法一样,也就是用来加密的key和解密的key是完全一样的。而非对称加密加密的key和解密的key是不一样的,加密是用公钥,解密是用私钥。
2.1、对称加密
对称加密算法是应用较早的加密算法,技术成熟。在对称加密算法中,数据发信方将明文(原始数据)和加密密钥一起经过特殊加密算法处理后,使其变成复杂的加密密文发送出去。收信方收到密文后,若想解读原文,则需要使用加密用过的密钥及相同算法的逆算法对密文进行解密,才能使其恢复成可读明文。
在对称加密算法中,使用的密钥只有一个,发收信双方都使用这个密钥对数据进行加密和解密,这就要求解密方事先必须知道加密密钥。对称加密算法的特点是算法公开、计算量小、加密速度快、加密效率高。不足之处是,交易双方都使用同样钥匙,安全性得不到保证。此外,每对用户每次使用对称加密算法时,都需要使用其他人不知道的惟一钥匙,这会使得发收信双方所拥有的钥匙数量成几何级数增长,密钥管理成为用户的负担。对称加密算法在分布式网络系统上使用较为困难,主要是因为密钥管理困难,使用成本较高。在计算机专网系统中广泛使用的对称加密算法有DES、IDEA和AES。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
|
在对称加密算法中比较著名和常用的就是大名鼎鼎的DES加密算法,它安全性比较高的一种算法,目前只有一种方法可以破解该算法,那就是穷举法。下面我们来看看.NET中要使用DES加密怎么实现?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
|
除此之外我们还可以用DES加密文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
|
2.2、非对称加密
不对称加密算法使用两把完全不同但又是完全匹配的一对钥匙—公钥和私钥。在使用不对称加密算法加密文件时,只有使用匹配的一对公钥和私钥,才能完成对明文的加密和解密过程。加密明文时采用公钥加密,解密密文时使用私钥才能完成,而且发信方(加密者)知道收信方的公钥,只有收信方(解密者)才是唯一知道自己私钥的人。不对称加密算法的基本原理是,如果发信方想发送只有收信方才能解读的加密信息,发信方必须首先知道收信方的公钥,然后利用收信方的公钥来加密原文;收信方收到加密密文后,使用自己的私钥才能解密密文。显然,采用不对称加密算法,收发信双方在通信之前,收信方必须将自己早已随机生成的公钥送给发信方,而自己保留私钥。由于不对称算法拥有两个密钥,因而特别适用于分布式系统中的数据加密。
广泛应用的不对称加密算法有RSA算法和美国国家标准局提出的DSA。以不对称加密算法为基础的加密技术应用非常广泛。尤其是在Linux系统下会经常用到这种非对称加密。反正你只需要记住一点有公钥和私钥对的就是非对称加密。
下面我们来看看C#中的怎么实现RSA非对称加密。
首先,我们要生成一个公钥和私钥对。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
运行上面的控制台程序会在程序目录下生成配对的公钥和私钥文件。
接下来写一个RSA加密解密类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
|
上面的PublicKey和PrivateKey非别是文件PublicKey.xml和PrivateKey.xml里面的内容。测试这个RSAHelper的方法:
1 2 3 4 5 6 7 8 9 10 |
|
可以看到上面通过EncryptData加密的数据能够通过DecryptData解密成原来的明文(Hello),而这个加密和解密用是不同的密钥。
转载自:蓝狐软件工作室 » C#.NET中对称和非对称加密、解密方法汇总--亲测可用