C# 常用的加密代码参考


1、MD5加密

 1 public static string EncryptString(string source)
 2         {
 3             string result;
 4             if (source == string.Empty || source == null)
 5             {
 6                 result = string.Empty;
 7             }
 8             else
 9             {
10                 Encoding utf = new UTF8Encoding();
11                 byte[] bytes = utf.GetBytes(source);
12                 MD5 md5 = new MD5CryptoServiceProvider();
13                 result = Convert.ToBase64String(md5.ComputeHash(bytes));
14             }
15             return result;
16         }

2、 SHA_1加密

 1 public static string EncryptString(string source)
 2         {
 3             string result;
 4             if (source == "" || source == string.Empty || source == null)
 5             {
 6                 result = source;
 7             }
 8             else
 9             {
10                 Encoding utf = new UTF8Encoding();
11                 byte[] bytes = utf.GetBytes(source);
12                 SHA1 sha = new SHA1CryptoServiceProvider();
13                 result = Convert.ToBase64String(sha.ComputeHash(bytes));
14             }
15             return result;
16         }

3、对称加密示例

 1 private static readonly string key = "sajdkfj";
 2 
 3         private static readonly byte[] iv = new byte[]
 4         {
 5             7,
 6             8,
 7             9,
 8             4,
 9             5,
10             3,
11             2,
12             1
13         };
14 
15         public static string EncryptString(string source)
16         {
17             string result;
18             if (source == string.Empty || source == null)
19             {
20                 result = source;
21             }
22             else
23             {
24                 Encoding utf = new UTF8Encoding();
25                 DESCryptoServiceProvider des = new DESCryptoServiceProvider();
26                 ICryptoTransform encrpt = des.CreateEncryptor(utf.GetBytes(SymmetricEncryption.key), SymmetricEncryption.iv);
27                 byte[] byteMessage = utf.GetBytes(source);
28                 byte[] byteEncrpt = encrpt.TransformFinalBlock(byteMessage, 0, byteMessage.Length);
29                 result = Convert.ToBase64String(byteEncrpt);
30             }
31             return result;
32         }
33 
34         public static string DecryptString(string source)
35         {
36             Encoding utf = new UTF8Encoding();
37             DESCryptoServiceProvider des = new DESCryptoServiceProvider();
38             ICryptoTransform decrpt = des.CreateDecryptor(utf.GetBytes(SymmetricEncryption.key), SymmetricEncryption.iv);
39             byte[] byteEncrypt = Convert.FromBase64String(source);
40             byte[] byteDecrypt = decrpt.TransformFinalBlock(byteEncrypt, 0, byteEncrypt.Length);
41             return utf.GetString(byteDecrypt);
42         }

4、非对称加密

  1 public const string PUBLIC_KEY = "sdfasdfjsldkjfljLKjlkjaslkdj";
  2 
  3         public void RSAKey(out string privateKeys, out string publicKey)
  4         {
  5             RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
  6             privateKeys = rsa.ToXmlString(true);
  7             publicKey = rsa.ToXmlString(false);
  8         }
  9 
 10         public string RSAEncrypt(string publicKey, string strEncryptString)
 11         {
 12             byte[] plainTextBArray = new UnicodeEncoding().GetBytes(strEncryptString);
 13             return this.RSAEncrypt(publicKey, plainTextBArray);
 14         }
 15 
 16         public string RSAEncrypt(string publicKey, byte[] encryptString)
 17         {
 18             RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
 19             rsa.FromXmlString(publicKey);
 20             int keySize = rsa.KeySize / 8;
 21             int bufferSize = keySize - 11;
 22             byte[] buffer = new byte[bufferSize];
 23             MemoryStream msInput = new MemoryStream(encryptString);
 24             MemoryStream msOutput = new MemoryStream();
 25             for (int readLen = msInput.Read(buffer, 0, bufferSize); readLen > 0; readLen = msInput.Read(buffer, 0, bufferSize))
 26             {
 27                 byte[] dataToEnc = new byte[readLen];
 28                 Array.Copy(buffer, 0, dataToEnc, 0, readLen);
 29                 byte[] encData = rsa.Encrypt(dataToEnc, false);
 30                 msOutput.Write(encData, 0, encData.Length);
 31             }
 32             msInput.Close();
 33             byte[] cypherTextBArray = msOutput.ToArray();
 34             msOutput.Close();
 35             rsa.Clear();
 36             return Convert.ToBase64String(cypherTextBArray);
 37         }
 38 
 39         public string RSADecrypt(string privateKey, string strDecryptString)
 40         {
 41             byte[] plainTextBArray = Convert.FromBase64String(strDecryptString);
 42             return this.RSADecrypt(privateKey, plainTextBArray);
 43         }
 44 
 45         public string RSADecrypt(string privateKey, byte[] decryptString)
 46         {
 47             RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
 48             rsa.FromXmlString(privateKey);
 49             int keySize = rsa.KeySize / 8;
 50             byte[] buffer = new byte[keySize];
 51             MemoryStream msInput = new MemoryStream(decryptString);
 52             MemoryStream msOutput = new MemoryStream();
 53             for (int readLen = msInput.Read(buffer, 0, keySize); readLen > 0; readLen = msInput.Read(buffer, 0, keySize))
 54             {
 55                 byte[] dataToDec = new byte[readLen];
 56                 Array.Copy(buffer, 0, dataToDec, 0, readLen);
 57                 byte[] decData = rsa.Decrypt(dataToDec, false);
 58                 msOutput.Write(decData, 0, decData.Length);
 59             }
 60             msInput.Close();
 61             byte[] dypherTextBArray = msOutput.ToArray();
 62             msOutput.Close();
 63             rsa.Clear();
 64             return new UnicodeEncoding().GetString(dypherTextBArray);
 65         }
 66 
 67         public bool GetHash(string strSource, ref byte[] HashData)
 68         {
 69             HashAlgorithm MD5 = HashAlgorithm.Create("MD5");
 70             byte[] Buffer = Encoding.GetEncoding("GB2312").GetBytes(strSource);
 71             HashData = MD5.ComputeHash(Buffer);
 72             return true;
 73         }
 74 
 75         public string GetHash(string strSource)
 76         {
 77             HashAlgorithm MD5 = HashAlgorithm.Create("MD5");
 78             byte[] Buffer = Encoding.GetEncoding("GB2312").GetBytes(strSource);
 79             byte[] HashData = MD5.ComputeHash(Buffer);
 80             return Convert.ToBase64String(HashData);
 81         }
 82 
 83         public void SignatureFormatter(string p_strKeyPrivate, byte[] HashbyteSignature, out byte[] EncryptedSignatureData)
 84         {
 85             RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
 86             RSA.FromXmlString(p_strKeyPrivate);
 87             RSAPKCS1SignatureFormatter RSAFormatter = new RSAPKCS1SignatureFormatter(RSA);
 88             RSAFormatter.SetHashAlgorithm("MD5");
 89             EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);
 90         }
 91 
 92         public void SignatureFormatter(string p_strKeyPrivate, byte[] HashbyteSignature, out string m_strEncryptedSignatureData)
 93         {
 94             RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
 95             RSA.FromXmlString(p_strKeyPrivate);
 96             RSAPKCS1SignatureFormatter RSAFormatter = new RSAPKCS1SignatureFormatter(RSA);
 97             RSAFormatter.SetHashAlgorithm("MD5");
 98             byte[] EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);
 99             m_strEncryptedSignatureData = Convert.ToBase64String(EncryptedSignatureData);
100         }
101 
102         public void SignatureFormatter(string p_strKeyPrivate, string m_strHashbyteSignature, out byte[] EncryptedSignatureData)
103         {
104             byte[] HashbyteSignature = Convert.FromBase64String(this.GetHash(m_strHashbyteSignature));
105             RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
106             RSA.FromXmlString(p_strKeyPrivate);
107             RSAPKCS1SignatureFormatter RSAFormatter = new RSAPKCS1SignatureFormatter(RSA);
108             RSAFormatter.SetHashAlgorithm("MD5");
109             EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);
110         }
111 
112         public void SignatureFormatter(string p_strKeyPrivate, string m_strHashbyteSignature, out string m_strEncryptedSignatureData)
113         {
114             byte[] HashbyteSignature = Convert.FromBase64String(this.GetHash(m_strHashbyteSignature));
115             RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
116             RSA.FromXmlString(p_strKeyPrivate);
117             RSAPKCS1SignatureFormatter RSAFormatter = new RSAPKCS1SignatureFormatter(RSA);
118             RSAFormatter.SetHashAlgorithm("MD5");
119             byte[] EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);
120             m_strEncryptedSignatureData = Convert.ToBase64String(EncryptedSignatureData);
121         }
122 
123         public bool SignatureDeformatter(string p_strKeyPublic, byte[] HashbyteDeformatter, byte[] DeformatterData)
124         {
125             RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
126             RSA.FromXmlString(p_strKeyPublic);
127             RSAPKCS1SignatureDeformatter RSADeformatter = new RSAPKCS1SignatureDeformatter(RSA);
128             RSADeformatter.SetHashAlgorithm("MD5");
129             return RSADeformatter.VerifySignature(HashbyteDeformatter, DeformatterData);
130         }
131 
132         public bool SignatureDeformatter(string p_strKeyPublic, string p_strHashbyteDeformatter, byte[] DeformatterData)
133         {
134             byte[] HashbyteDeformatter = Convert.FromBase64String(this.GetHash(p_strHashbyteDeformatter));
135             RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
136             RSA.FromXmlString(p_strKeyPublic);
137             RSAPKCS1SignatureDeformatter RSADeformatter = new RSAPKCS1SignatureDeformatter(RSA);
138             RSADeformatter.SetHashAlgorithm("MD5");
139             return RSADeformatter.VerifySignature(HashbyteDeformatter, DeformatterData);
140         }
141 
142         public bool SignatureDeformatter(string p_strKeyPublic, byte[] HashbyteDeformatter, string p_strDeformatterData)
143         {
144             RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
145             RSA.FromXmlString(p_strKeyPublic);
146             RSAPKCS1SignatureDeformatter RSADeformatter = new RSAPKCS1SignatureDeformatter(RSA);
147             RSADeformatter.SetHashAlgorithm("MD5");
148             byte[] DeformatterData = Convert.FromBase64String(p_strDeformatterData);
149             return RSADeformatter.VerifySignature(HashbyteDeformatter, DeformatterData);
150         }
151 
152         public bool SignatureDeformatter(string p_strKeyPublic, string p_strHashbyteDeformatter, string p_strDeformatterData)
153         {
154             byte[] HashbyteDeformatter = Convert.FromBase64String(this.GetHash(p_strHashbyteDeformatter));
155             RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
156             RSA.FromXmlString(p_strKeyPublic);
157             RSAPKCS1SignatureDeformatter RSADeformatter = new RSAPKCS1SignatureDeformatter(RSA);
158             RSADeformatter.SetHashAlgorithm("MD5");
159             byte[] DeformatterData = Convert.FromBase64String(p_strDeformatterData);
160             return RSADeformatter.VerifySignature(HashbyteDeformatter, DeformatterData);
161         }