1 private static readonly Encoding Encoder = Encoding.UTF8;
2
3 public static String Encrypt(this String plaintext)
4 {
5 X509Certificate2 _X509Certificate2 = RSAHelper.RetrieveX509Certificate();
6 string publickey = @"<RSAKeyValue><Modulus>5m9m14XH3oqLJ8bNGw9e4rGpXpcktv9MSkHSVFVMjHbfv+SJ5v0ubqQxa5YjLN4vc49z7SVju8s0X4gZ6AzZTn06jzWOgyPRV54Q4I0DCYadWW4Ze3e+BOtwgVU1Og3qHKn8vygoj40J6U85Z/PTJu3hN1m75Zr195ju7g9v4Hk=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
7 using (RSACryptoServiceProvider RSACryptography = /*_X509Certificate2.PublicKey.Key as*/ new RSACryptoServiceProvider())
8 {
9 RSACryptography.FromXmlString(publickey);
10 Byte[] PlaintextData = RSAHelper.Encoder.GetBytes(plaintext);
11 int MaxBlockSize = RSACryptography.KeySize / 8 - 11; //加密块最大长度限制
12
13 if (PlaintextData.Length <= MaxBlockSize)
14 return Convert.ToBase64String(RSACryptography.Encrypt(PlaintextData, false));
15
16 using (MemoryStream PlaiStream = new MemoryStream(PlaintextData))
17 using (MemoryStream CrypStream = new MemoryStream())
18 {
19 Byte[] Buffer = new Byte[MaxBlockSize];
20 int BlockSize = PlaiStream.Read(Buffer, 0, MaxBlockSize);
21
22 while (BlockSize > 0)
23 {
24 Byte[] ToEncrypt = new Byte[BlockSize];
25 Array.Copy(Buffer, 0, ToEncrypt, 0, BlockSize);
26
27 Byte[] Cryptograph = RSACryptography.Encrypt(ToEncrypt, false);
28 CrypStream.Write(Cryptograph, 0, Cryptograph.Length);
29
30 BlockSize = PlaiStream.Read(Buffer, 0, MaxBlockSize);
31 }
32
33 return Convert.ToBase64String(CrypStream.ToArray(), Base64FormattingOptions.None);
34 }
35 }
36 }
37
38 public static String Decrypt(this String ciphertext)
39 {
40 X509Certificate2 _X509Certificate2 = RSAHelper.RetrieveX509Certificate();
41 string privatekey = @"<RSAKeyValue><Modulus>5m9m14XH3oqLJ8bNGw9e4rGpXpcktv9MSkHSVFVMjHbfv+SJ5v0ubqQxa5YjLN4vc49z7SVju8s0X4gZ6AzZTn06jzWOgyPRV54Q4I0DCYadWW4Ze3e+BOtwgVU1Og3qHKn8vygoj40J6U85Z/PTJu3hN1m75Zr195ju7g9v4Hk=</Modulus><Exponent>AQAB</Exponent><P>/hf2dnK7rNfl3lbqghWcpFdu778hUpIEBixCDL5WiBtpkZdpSw90aERmHJYaW2RGvGRi6zSftLh00KHsPcNUMw==</P><Q>6Cn/jOLrPapDTEp1Fkq+uz++1Do0eeX7HYqi9rY29CqShzCeI7LEYOoSwYuAJ3xA/DuCdQENPSoJ9KFbO4Wsow==</Q><DP>ga1rHIJro8e/yhxjrKYo/nqc5ICQGhrpMNlPkD9n3CjZVPOISkWF7FzUHEzDANeJfkZhcZa21z24aG3rKo5Qnw==</DP><DQ>MNGsCB8rYlMsRZ2ek2pyQwO7h/sZT8y5ilO9wu08Dwnot/7UMiOEQfDWstY3w5XQQHnvC9WFyCfP4h4QBissyw==</DQ><InverseQ>EG02S7SADhH1EVT9DD0Z62Y0uY7gIYvxX/uq+IzKSCwB8M2G7Qv9xgZQaQlLpCaeKbux3Y59hHM+KpamGL19Kg==</InverseQ><D>vmaYHEbPAgOJvaEXQl+t8DQKFT1fudEysTy31LTyXjGu6XiltXXHUuZaa2IPyHgBz0Nd7znwsW/S44iql0Fen1kzKioEL3svANui63O3o5xdDeExVM6zOf1wUUh/oldovPweChyoAdMtUzgvCbJk1sYDJf++Nr0FeNW1RB1XG30=</D></RSAKeyValue>";
42 using (RSACryptoServiceProvider RSACryptography = /*_X509Certificate2.PrivateKey as*/ new RSACryptoServiceProvider())
43 {
44 RSACryptography.FromXmlString(privatekey);
45 Byte[] CiphertextData = Convert.FromBase64String(ciphertext);
46 int MaxBlockSize = RSACryptography.KeySize / 8; //解密块最大长度限制
47
48 if (CiphertextData.Length <= MaxBlockSize)
49 return RSAHelper.Encoder.GetString(RSACryptography.Decrypt(CiphertextData, false));
50
51 using (MemoryStream CrypStream = new MemoryStream(CiphertextData))
52 using (MemoryStream PlaiStream = new MemoryStream())
53 {
54 Byte[] Buffer = new Byte[MaxBlockSize];
55 int BlockSize = CrypStream.Read(Buffer, 0, MaxBlockSize);
56
57 while (BlockSize > 0)
58 {
59 Byte[] ToDecrypt = new Byte[BlockSize];
60 Array.Copy(Buffer, 0, ToDecrypt, 0, BlockSize);
61
62 Byte[] Plaintext = RSACryptography.Decrypt(ToDecrypt, false);
63 PlaiStream.Write(Plaintext, 0, Plaintext.Length);
64
65 BlockSize = CrypStream.Read(Buffer, 0, MaxBlockSize);
66 }
67
68 return RSAHelper.Encoder.GetString(PlaiStream.ToArray());
69 }
70 }
71 }
72
73 private static X509Certificate2 RetrieveX509Certificate()
74 {
75
76 return null; //检索用于 RSA 加密的 X509Certificate2 证书
77 }
View Code