
1 1 private static readonly Encoding Encoder = Encoding.UTF8; 2 2 3 3 public static String Encrypt(this String plaintext) 4 4 { 5 5 X509Certificate2 _X509Certificate2 = RSAHelper.RetrieveX509Certificate(); 6 6 string publickey = @"<RSAKeyValue><Modulus>5m9m14XH3oqLJ8bNGw9e4rGpXpcktv9MSkHSVFVMjHbfv+SJ5v0ubqQxa5YjLN4vc49z7SVju8s0X4gZ6AzZTn06jzWOgyPRV54Q4I0DCYadWW4Ze3e+BOtwgVU1Og3qHKn8vygoj40J6U85Z/PTJu3hN1m75Zr195ju7g9v4Hk=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>"; 7 7 using (RSACryptoServiceProvider RSACryptography = /*_X509Certificate2.PublicKey.Key as*/ new RSACryptoServiceProvider()) 8 8 { 9 9 RSACryptography.FromXmlString(publickey); 1010 Byte[] PlaintextData = RSAHelper.Encoder.GetBytes(plaintext); 1111 int MaxBlockSize = RSACryptography.KeySize / 8 - 11; //加密块最大长度限制 1212 1313 if (PlaintextData.Length <= MaxBlockSize) 1414 return Convert.ToBase64String(RSACryptography.Encrypt(PlaintextData, false)); 1515 1616 using (MemoryStream PlaiStream = new MemoryStream(PlaintextData)) 1717 using (MemoryStream CrypStream = new MemoryStream()) 1818 { 1919 Byte[] Buffer = new Byte[MaxBlockSize]; 2020 int BlockSize = PlaiStream.Read(Buffer, 0, MaxBlockSize); 2121 2222 while (BlockSize > 0) 2323 { 2424 Byte[] ToEncrypt = new Byte[BlockSize]; 2525 Array.Copy(Buffer, 0, ToEncrypt, 0, BlockSize); 2626 2727 Byte[] Cryptograph = RSACryptography.Encrypt(ToEncrypt, false); 2828 CrypStream.Write(Cryptograph, 0, Cryptograph.Length); 2929 3030 BlockSize = PlaiStream.Read(Buffer, 0, MaxBlockSize); 3131 } 3232 3333 return Convert.ToBase64String(CrypStream.ToArray(), Base64FormattingOptions.None); 3434 } 3535 } 3636 } 3737 3838 public static String Decrypt(this String ciphertext) 3939 { 4040 X509Certificate2 _X509Certificate2 = RSAHelper.RetrieveX509Certificate(); 4141 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>"; 4242 using (RSACryptoServiceProvider RSACryptography = /*_X509Certificate2.PrivateKey as*/ new RSACryptoServiceProvider()) 4343 { 4444 RSACryptography.FromXmlString(privatekey); 4545 Byte[] CiphertextData = Convert.FromBase64String(ciphertext); 4646 int MaxBlockSize = RSACryptography.KeySize / 8; //解密块最大长度限制 4747 4848 if (CiphertextData.Length <= MaxBlockSize) 4949 return RSAHelper.Encoder.GetString(RSACryptography.Decrypt(CiphertextData, false)); 5050 5151 using (MemoryStream CrypStream = new MemoryStream(CiphertextData)) 5252 using (MemoryStream PlaiStream = new MemoryStream()) 5353 { 5454 Byte[] Buffer = new Byte[MaxBlockSize]; 5555 int BlockSize = CrypStream.Read(Buffer, 0, MaxBlockSize); 5656 5757 while (BlockSize > 0) 5858 { 5959 Byte[] ToDecrypt = new Byte[BlockSize]; 6060 Array.Copy(Buffer, 0, ToDecrypt, 0, BlockSize); 6161 6262 Byte[] Plaintext = RSACryptography.Decrypt(ToDecrypt, false); 6363 PlaiStream.Write(Plaintext, 0, Plaintext.Length); 6464 6565 BlockSize = CrypStream.Read(Buffer, 0, MaxBlockSize); 6666 } 6767 6868 return RSAHelper.Encoder.GetString(PlaiStream.ToArray()); 6969 } 7070 } 7171 } 7272 7373 private static X509Certificate2 RetrieveX509Certificate() 7474 { 7575 7676 return null; //检索用于 RSA 加密的 X509Certificate2 证书 7777 }
View Code