本文整理汇总了C#中System.Security.Cryptography.RSACryptoServiceProvider.Clear方法的典型用法代码示例。如果您正苦于以下问题:C# RSACryptoServiceProvider.Clear方法的具体用法?C# RSACryptoServiceProvider.Clear怎么用?C# RSACryptoServiceProvider.Clear使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.Cryptography.RSACryptoServiceProvider
的用法示例。
在下文中一共展示了RSACryptoServiceProvider.Clear方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Run
public override void Run()
{
RSACryptoServiceProvider rsa = null;
try
{
var cp = new CspParameters();
cp.KeyContainerName = _containerName;
cp.Flags = CspProviderFlags.UseMachineKeyStore;
rsa = new RSACryptoServiceProvider(cp);
rsa.FromXmlString(File.ReadAllText(_keyFile));
rsa.PersistKeyInCsp = true;
rsa.Clear();
}
catch (Exception)
{
if (rsa != null)
{
rsa.PersistKeyInCsp = false;
rsa.Clear();
}
throw;
}
}
示例2: GenKeys
private static RSACryptoServiceProvider GenKeys(int keySize)
{
var parameters = new CspParameters();
RSACryptoServiceProvider provider;
parameters.Flags = CspProviderFlags.NoPrompt | CspProviderFlags.UseMachineKeyStore | CspProviderFlags.UseExistingKey;
parameters.KeyNumber = (int)KeyNumber.Exchange;
parameters.KeyContainerName = System.Reflection.Assembly.GetEntryAssembly().FullName;
try
{
//RSAHelper: Attempting to open existing key container
provider = new RSACryptoServiceProvider(parameters);
var pa = provider.ExportParameters(false);
if (pa.Modulus.Length * 8 == keySize) return provider;
//Found existing key, but not of the correct size
provider.PersistKeyInCsp = false;
provider.Clear();
provider.Dispose();
GenerateRsaKeys(parameters, keySize, out provider);
}
catch
{
//No existing Key Container was found in the machine keystore
GenerateRsaKeys(parameters, keySize, out provider);
}
finally
{
GC.Collect();
GC.WaitForPendingFinalizers();
}
return provider;
}
示例3: RSACryptoServiceProviderTest
public RSACryptoServiceProviderTest ()
{
sha1OID = CryptoConfig.MapNameToOID ("SHA1");
disposed = new RSACryptoServiceProvider (minKeySize);
disposed.FromXmlString ("<RSAKeyValue><Modulus>vtXAf62+o50prNCTiVGTMzdhm4sMjK0QVDkKQLFGu2fJQCULt9NZBab14PiWfG1t</Modulus><Exponent>AQAB</Exponent><P>5y2AHOzIhTChIFzLsgZQAGfy3U8OPwFh</P><Q>01NUVJJv+hhIsnbFiSi24FLRrfr/qYuN</Q><DP>HKLAOdUCyazKaK3V9Yleo448wTkntJpB</DP><DQ>AH5MTxo8arAN02TVlzliG+n1lVtlp2at</DQ><InverseQ>ZpgJwTxSYpT81sQCuVUvX0AYrvSziNIw</InverseQ><D>CStiJYBmsZvincAj5qw5w3M8yGmE/9ls4yv7wenozzC4kZshpI2MuON0d2Z8f4aB</D></RSAKeyValue>");
disposed.Clear ();
}
示例4: CreateKey
/// <summary>
/// ������˽Կ
/// </summary>
/// <param name="publicKey"></param>
/// <param name="privatekey"></param>
public void CreateKey(out string publicKey, out string privateKey)
{
RSACryptoServiceProvider crypt = new RSACryptoServiceProvider();
publicKey = crypt.ToXmlString(false);
privateKey = crypt.ToXmlString(true);
crypt.Clear();
}
示例5: Main
static void Main(string[] args)
{
string KeyContainerName = "MyKeyContainer";
string clearText = "This is the data we want to encrypt!";
CspParameters cspParams = new CspParameters();
cspParams.KeyContainerName = KeyContainerName;
RSAParameters publicKey;
RSAParameters privateKey;
using(var rsa = new RSACryptoServiceProvider(cspParams))
{
rsa.PersistKeyInCsp = true;
publicKey = rsa.ExportParameters(false);
privateKey = rsa.ExportParameters(true);
rsa.Clear();
}
byte[] encrypted = EncryptUsingRSAParam(clearText, publicKey);
string decrypted = DecryptUsingRSAParam(encrypted, privateKey);
Console.WriteLine("Asymmetric RSA - Using RSA Params");
Console.WriteLine("Encrypted:{0}", Convert.ToBase64String(encrypted));
Console.WriteLine("Decrypted:{0}", decrypted);
Console.WriteLine("Asymmetric RSA - Using Persistent Key Container");
encrypted = EncryptUsingContainer(clearText, KeyContainerName);
decrypted = DecryptUsingContainer(encrypted, KeyContainerName);
Console.WriteLine("Encrypted:{0}", Convert.ToBase64String(encrypted));
Console.WriteLine("Decrypted:{0}", decrypted);
Console.ReadLine();
}
示例6: RSADeleteKeyInCSP
public static void RSADeleteKeyInCSP(string ContainerName)
{
try
{
// Create a new instance of CspParameters. Pass
// 13 to specify a DSA container or 1 to specify
// an RSA container. The default is 1.
CspParameters cspParams = new CspParameters();
// Specify the container name using the passed variable.
cspParams.KeyContainerName = ContainerName;
//Create a new instance of RSACryptoServiceProvider.
//Pass the CspParameters class to use the
//key in the container.
RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider(cspParams);
//Delete the key entry in the container.
RSAalg.PersistKeyInCsp = false;
//Call Clear to release resources and delete the key from the container.
RSAalg.Clear();
//Indicate that the key was persisted.
Console.WriteLine("The RSA key was deleted from the container, \"{0}\".", ContainerName);
}
catch (CryptographicException e)
{
Console.WriteLine(e.Message);
}
}
示例7: Decrypt
/// <summary>
/// Decrypts a string using RSA
/// </summary>
/// <param name="Input">Input string (should be small as anything over 128 bytes can not be decrypted)</param>
/// <param name="Key">Key to use for decryption</param>
/// <returns>A decrypted string</returns>
public static string Decrypt(string Input, string Key)
{
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSA.FromXmlString(Key);
byte[] InputArray = Convert.FromBase64String(Input);
byte[] EncryptedBytes = RSA.Decrypt(InputArray, true);
RSA.Clear();
return Encoding.UTF8.GetString(EncryptedBytes);
}
示例8: GetKeyInfo
/// <summary>
/// 得到私匙和公匙的信息,string[0]是私匙的信息,string[1]是公匙的信息
/// </summary>
/// <returns></returns>
public static string[] GetKeyInfo()
{
string[] key=new string[2];
RSACryptoServiceProvider crypt=new RSACryptoServiceProvider();
key[0]=crypt.ToXmlString(true);
key[1]=crypt.ToXmlString(false);
crypt.Clear();
return key;
}
示例9: GenerateRSAKey
/// <summary>
/// 生成密钥
/// </summary>
public RSAKey GenerateRSAKey()
{
RSAKey RSAKEY = new RSAKey();
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSAKEY.PrivateKey = RSA.ToXmlString(true); //生成私钥
RSAKEY.PublicKey = RSA.ToXmlString(false); //生成公钥
RSA.Clear();
return RSAKEY;
}
示例10: Delete
public static void Delete()
{
var cp = new CspParameters();
cp.KeyContainerName = KeyContainerName;
cp.Flags = CspProviderFlags.UseMachineKeyStore;
var rsa = new RSACryptoServiceProvider(cp);
rsa.PersistKeyInCsp = false; // Delete the key entry in the container.
rsa.Clear();
}
示例11: Encrypt
/// <summary>
/// Encrypts a string using RSA
/// </summary>
/// <param name="Input">Input string (should be small as anything over 128 bytes can not be decrypted)</param>
/// <param name="Key">Key to use for encryption</param>
/// <returns>An encrypted string (64bit string)</returns>
public static string Encrypt(string Input, string Key)
{
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSA.FromXmlString(Key);
ASCIIEncoding Encoding = new ASCIIEncoding();
byte[] InputArray = Encoding.GetBytes(Input);
byte[] EncryptedBytes = RSA.Encrypt(InputArray, true);
RSA.Clear();
return Convert.ToBase64String(EncryptedBytes);
}
示例12: FixtureSetUp
public void FixtureSetUp ()
{
sha1OID = CryptoConfig.MapNameToOID ("SHA1");
disposed = new RSACryptoServiceProvider (minKeySize);
disposed.FromXmlString ("<RSAKeyValue><Modulus>vtXAf62+o50prNCTiVGTMzdhm4sMjK0QVDkKQLFGu2fJQCULt9NZBab14PiWfG1t</Modulus><Exponent>AQAB</Exponent><P>5y2AHOzIhTChIFzLsgZQAGfy3U8OPwFh</P><Q>01NUVJJv+hhIsnbFiSi24FLRrfr/qYuN</Q><DP>HKLAOdUCyazKaK3V9Yleo448wTkntJpB</DP><DQ>AH5MTxo8arAN02TVlzliG+n1lVtlp2at</DQ><InverseQ>ZpgJwTxSYpT81sQCuVUvX0AYrvSziNIw</InverseQ><D>CStiJYBmsZvincAj5qw5w3M8yGmE/9ls4yv7wenozzC4kZshpI2MuON0d2Z8f4aB</D></RSAKeyValue>");
// FX 2.0 beta 1 bug - we must use the key before clearing it
// http://lab.msdn.microsoft.com/ProductFeedback/viewfeedback.aspx?feedbackid=1bc807eb-c4ca-4c2d-8499-9f0470b71a29
int ks = disposed.KeySize;
disposed.Clear ();
}
示例13: CifrarElemento
/// <summary>
///
/// </summary>
void CifrarElemento()
{
// Create an XmlDocument object.
XmlDocument xmlDoc = new XmlDocument();
// Load an XML file into the XmlDocument object.
try
{
xmlDoc.PreserveWhitespace = true;
xmlDoc.Load("ConfigurationManager.xml");
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
// Create a new CspParameters object to specify
// a key container.
CspParameters cspParams = new CspParameters();
cspParams.KeyContainerName = "XML_ENC_RSA_KEY";
// Create a new RSA key and save it in the container. This key will encrypt
// a symmetric key, which will then be encryped in the XML document.
RSACryptoServiceProvider rsaKey = new RSACryptoServiceProvider(cspParams);
try
{
// Encrypt the "creditcard" element.
Encrypt(xmlDoc, "Groups", "EncryptedElement1", rsaKey, "rsaKey");
// Save the XML document.
xmlDoc.Save("ConfigurationManager.xml");
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
// Clear the RSA key.
rsaKey.Clear();
}
}
示例14: Create
public static void Create()
{
var cp = new CspParameters();
cp.KeyContainerName = KeyContainerName;
cp.Flags = CspProviderFlags.UseMachineKeyStore;
var rsa = new RSACryptoServiceProvider(cp);
rsa.FromXmlString(XmlKey);
rsa.PersistKeyInCsp = true;
rsa.Clear();
}
示例15: EncryptUsingRSAParam
static byte[] EncryptUsingRSAParam(string value, RSAParameters rsaKeyInfo)
{
using(RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
{
rsa.ImportParameters(rsaKeyInfo);
byte[] encodedData = Encoding.Default.GetBytes(value);
byte[] encryptedData = rsa.Encrypt(encodedData, true);
rsa.Clear();
return encryptedData;
}
}