C#语言中的Encrypt和Decrypt

使用RSACryptoServiceProvider加密和解密一个字符串。

源代码:

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace Extensions
{
public static class Extensions
{
/// <summary>
/// Encryptes a string using the supplied key. Encoding is done using RSA encryption.
/// </summary>
/// <param name="stringToEncrypt">String that must be encrypted.</param>
/// <param name="key">Encryptionkey.</param>
/// <returns>A string representing a byte array separated by a minus sign.</returns>
/// <exception cref="ArgumentException">Occurs when stringToEncrypt or key is null or empty.</exception>
public static string Encrypt(this string stringToEncrypt, string key)
{
if (string.IsNullOrEmpty(stringToEncrypt))
{
throw new ArgumentException("An empty string value cannot be encrypted.");
}

if (string.IsNullOrEmpty(key))
{
throw new ArgumentException("Cannot encrypt using an empty key. Please supply an encryption key.");
}

CspParameters cspp = new CspParameters();
cspp.KeyContainerName = key;

RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cspp);
rsa.PersistKeyInCsp = true;

byte[] bytes = rsa.Encrypt(System.Text.UTF8Encoding.UTF8.GetBytes(stringToEncrypt), true);

return BitConverter.ToString(bytes);
}

/// <summary>
/// Decryptes a string using the supplied key. Decoding is done using RSA encryption.
/// </summary>
/// <param name="stringToDecrypt">String that must be decrypted.</param>
/// <param name="key">Decryptionkey.</param>
/// <returns>The decrypted string or null if decryption failed.</returns>
/// <exception cref="ArgumentException">Occurs when stringToDecrypt or key is null or empty.</exception>
public static string Decrypt(this string stringToDecrypt, string key)
{
string result = null;

if (string.IsNullOrEmpty(stringToDecrypt))
{
throw new ArgumentException("An empty string value cannot be encrypted.");
}

if (string.IsNullOrEmpty(key))
{
throw new ArgumentException("Cannot decrypt using an empty key. Please supply a decryption key.");
}

try
{
CspParameters cspp = new CspParameters();
cspp.KeyContainerName = key;

RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cspp);
rsa.PersistKeyInCsp = true;

string[] decryptArray = stringToDecrypt.Split(new string[] { "-" }, StringSplitOptions.None);
byte[] decryptByteArray = Array.ConvertAll<string, byte>(decryptArray, (s => Convert.ToByte(byte.Parse(s, System.Globalization.NumberStyles.HexNumber))));


byte[] bytes = rsa.Decrypt(decryptByteArray, true);

result = System.Text.UTF8Encoding.UTF8.GetString(bytes);

}
finally
{
// no need for further processing
}

return result;
}


}
}

示例:

1
2
3
string secret = "My Secret";
string encoded = secret.Encrypt("mykey");
string decoded = encoded.Decrypt("mykey");
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×