本文整理汇总了C#中System.Security.Cryptography.RSACryptoServiceProvider.PublicDecryption方法的典型用法代码示例。如果您正苦于以下问题:C# RSACryptoServiceProvider.PublicDecryption方法的具体用法?C# RSACryptoServiceProvider.PublicDecryption怎么用?C# RSACryptoServiceProvider.PublicDecryption使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.Cryptography.RSACryptoServiceProvider
的用法示例。
在下文中一共展示了RSACryptoServiceProvider.PublicDecryption方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: PrivateEncryption_Load_Key_Test
public void PrivateEncryption_Load_Key_Test()
{
Random rnd = new Random();
byte[] data = new byte[196];
rnd.NextBytes(data);
byte[] encData;
using (RSACryptoServiceProvider privateRsa = new RSACryptoServiceProvider())
{
string privateKey =
"<RSAKeyValue><Modulus>vKzm/rFi2jPG6fumvhDWeZ49ZZhUh5EEXWW5fs1Y5iFW2poGA9I2sdeBBVgE16DshQY+VdW+e4uaXZJncuy+MdHoe9HWGx6iAVB3PSFUGNhaMQnX+bv+GWORBoFMBpx2ZGUqOSbazIxBpTxyg0DnXdpeFbVrWONq/A7RsmLWHZKk1cdoJ15YPI85IPFO3YwsfBDCYMHaaVcy7Ac9UvJGasopzfQq7dPM8d0xj93VUPq8La4psejJ/N56IYjs/+rvoFvACbt8U+a9UMWjNVbnBm4+MjcxASbxLkd3izRmZ5Jtv8YRoHOKkxlgG8vVVcNPNONoMNdE+YmxeZh7pH1w0Q==</Modulus><Exponent>AQAB</Exponent><P>3Wj62DgD6UFsoOxlgr7FpDM7Ev3/K4IfdHUkznGpVXWALawFMNMLmMIvp1NwFJ9U05+6IA3xnoq0Ue7mrJZ4HPqb7qwrJ9hG5gpXZzyMVUvcblJxchzet6N5fb3yMDX3A6zwfrsnaYucHVWmdf/MhWtkM/jQQN+Z1kAxkB+NTz0=</P><Q>2ia9ZGUKmw8Js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>";
privateRsa.FromXmlString(privateKey);
encData = privateRsa.PrivareEncryption(data);
}
byte[] decipher;
using (RSACryptoServiceProvider publicRsa = new RSACryptoServiceProvider())
{
string publicKey =
"<RSAKeyValue><Modulus>vKzm/rFi2jPG6fumvhDWeZ49ZZhUh5EEXWW5fs1Y5iFW2poGA9I2sdeBBVgE16DshQY+VdW+e4uaXZJncuy+MdHoe9HWGx6iAVB3PSFUGNhaMQnX+bv+GWORBoFMBpx2ZGUqOSbazIxBpTxyg0DnXdpeFbVrWONq/A7RsmLWHZKk1cdoJ15YPI85IPFO3YwsfBDCYMHaaVcy7Ac9UvJGasopzfQq7dPM8d0xj93VUPq8La4psejJ/N56IYjs/+rvoFvACbt8U+a9UMWjNVbnBm4+MjcxASbxLkd3izRmZ5Jtv8YRoHOKkxlgG8vVVcNPNONoMNdE+YmxeZh7pH1w0Q==</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
publicRsa.FromXmlString(publicKey);
decipher = publicRsa.PublicDecryption(encData);
}
Assert.IsTrue(decipher.SequenceEqual(data));
}
示例2: PrivareEncryptionTest
public void PrivareEncryptionTest()
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(1024);
Random rnd = new Random();
byte[] data;
for (int i = 0; i < 200; i++)
{
data = new byte[rnd.Next(16, 123)];
rnd.NextBytes(data); // Random content
// Private encrypt this data
byte[] cipher = rsa.PrivareEncryption(data);
// And decrypt it back
byte[] decipher = rsa.PublicDecryption(cipher);
Assert.AreEqual(data.Length, decipher.Length, "Round: {0}, data: {1}, decipher: {2}",
i, ShowBytesValue(data), ShowBytesValue(decipher));
Assert.IsTrue(data.SequenceEqual(decipher), "Round: {0} data: {1} decipher: {2}",
i, ShowBytesValue(data), ShowBytesValue(decipher));
}
}
示例3: RSACryptoServiceProvider_Usage
public void RSACryptoServiceProvider_Usage()
{
string secret = "My secret message";
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(512); // Key bits length
/*
* Skip the loading part for the RSACryptoServiceProvider will generate
* random Private / Public keys pair, that you can save later with
* rsa.ToXmlString(true);
*
string key = "private or public key as xml string";
rsa.FromXmlString(key);
*/
// Convert the string to byte array
byte[] secretData = Encoding.UTF8.GetBytes(secret);
// Encrypt it using the private key:
byte[] encrypted = rsa.PrivareEncryption(secretData);
// Decrypt it using the public key
byte[] decrypted = rsa.PublicDecryption(encrypted);
string decString = Encoding.UTF8.GetString(decrypted); // And back to string
Assert.AreEqual("My secret message", decString);
}