本文整理汇总了C#中System.Security.Cryptography.RSACryptoServiceProvider.ExportCspBlob方法的典型用法代码示例。如果您正苦于以下问题:C# RSACryptoServiceProvider.ExportCspBlob方法的具体用法?C# RSACryptoServiceProvider.ExportCspBlob怎么用?C# RSACryptoServiceProvider.ExportCspBlob使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.Cryptography.RSACryptoServiceProvider
的用法示例。
在下文中一共展示了RSACryptoServiceProvider.ExportCspBlob方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RSA_Encrypt
//RSA加密,随机生成公私钥对并作为出参返回
public static string RSA_Encrypt(string str_Plain_Text, out string str_Public_Key, out string str_Private_Key)
{
str_Public_Key = "";
str_Private_Key = "";
UnicodeEncoding ByteConverter = new UnicodeEncoding();
byte[] DataToEncrypt = ByteConverter.GetBytes(str_Plain_Text);
try
{
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
str_Public_Key = Convert.ToBase64String(RSA.ExportCspBlob(false));
str_Private_Key = Convert.ToBase64String(RSA.ExportCspBlob(true));
//OAEP padding is only available on Microsoft Windows XP or later.
byte[] bytes_Cypher_Text = RSA.Encrypt(DataToEncrypt, false);
str_Public_Key = Convert.ToBase64String(RSA.ExportCspBlob(false));
str_Private_Key = Convert.ToBase64String(RSA.ExportCspBlob(true));
string str_Cypher_Text = Convert.ToBase64String(bytes_Cypher_Text);
return str_Cypher_Text;
}
catch (CryptographicException e)
{
Console.WriteLine(e.Message);
return null;
}
}
示例2: GenerateKeys
/// <summary>
/// Generate keys into specified files.
/// </summary>
/// <param name="publicKeyFileName">Name of the file that will contain public key</param>
/// <param name="privateKeyFileName">Name of the file that will contain private key</param>
public void GenerateKeys(out byte[] publicKey, out byte[] privateKey)
{
// Variables
CspParameters cspParams = null;
RSACryptoServiceProvider rsaProvider = null;
try
{
// Create a new key pair on target CSP
cspParams = new CspParameters()
{
ProviderType = 1, // PROV_RSA_FULL
Flags = CspProviderFlags.UseArchivableKey, // can be exported
KeyNumber = (int)KeyNumber.Exchange // can be safely stored and exchanged
};
rsaProvider = new RSACryptoServiceProvider(cspParams);
rsaProvider.PersistKeyInCsp = false;
// Export public key only
publicKey = rsaProvider.ExportCspBlob(false);
privateKey = rsaProvider.ExportCspBlob(true);
}
catch (Exception ex)
{
Debug.Fail(string.Format("Exception occured while generating keys: {0}", ex.Message));
publicKey = null;
privateKey = null;
}
finally
{
if (rsaProvider != null) rsaProvider.PersistKeyInCsp = false;
}
}
示例3: UpdateSigningKey
public UpdateSigningKey() {
RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
PrivateKey = provider.ExportCspBlob(true);
PublicKey = provider.ExportCspBlob(false);
Name = "New Key";
}
示例4: GenerateKeyPairFiles
public static void GenerateKeyPairFiles(FileInfo privateKeyFile, FileInfo publicKeyFile)
{
using (var rsa = new RSACryptoServiceProvider())
{
rsa.ExportCspBlob(true).CompressToFile(privateKeyFile);
rsa.ExportCspBlob(false).CompressToFile(publicKeyFile);
}
}
示例5: GenerateKeys
public void GenerateKeys()
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
string privateKey = Convert.ToBase64String(rsa.ExportCspBlob(true));
Console.WriteLine("\nPrivateKey:\n{0}", privateKey);
string publicKey = Convert.ToBase64String(rsa.ExportCspBlob(false));
Console.WriteLine("\nPublicKey:\n{0}", publicKey);
}
示例6: GenerateKeyPair
//Generates keys to be used for testing purposes
public static Settings GenerateKeyPair()
{
var cspParams = new CspParameters { ProviderType = 1 };
var rsaProvider = new RSACryptoServiceProvider(1024, cspParams);
var publicKey = Convert.ToBase64String(rsaProvider.ExportCspBlob(false));
var privateKey = Convert.ToBase64String(rsaProvider.ExportCspBlob(true));
return new Settings { PublicKey = publicKey, PrivateKey = privateKey };
}
示例7: ImportFromXml
public override void ImportFromXml(IKeyStore keyStore, string xml, int keySize = 1024)
{
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(keySize))
{
rsa.PersistKeyInCsp = false;
rsa.FromXmlString(xml);
keyStore.PublicKey.Key = rsa.ExportCspBlob(false);
if(!rsa.PublicOnly)
keyStore.PrivateKey.Key = rsa.ExportCspBlob(true);
}
}
示例8: CreateKeyPair
public string CreateKeyPair()
{
CspParameters cspParams = new CspParameters { ProviderType = 1 };
RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider(1024, cspParams);
string publicKey = Convert.ToBase64String(rsaProvider.ExportCspBlob(false));
string privateKey = Convert.ToBase64String(rsaProvider.ExportCspBlob(true));
return privateKey + ":" + publicKey;
}
示例9: CreateKeys
public Tuple<string, string> CreateKeys()
{
CspParameters cspParams = new CspParameters {ProviderType = 1};
RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider(1024, cspParams);
string publicKey = Convert.ToBase64String(rsaProvider.ExportCspBlob(false));
string privateKey = Convert.ToBase64String(rsaProvider.ExportCspBlob(true));
return new Tuple<string, string>(privateKey, publicKey);
}
示例10: CreateNewKeyPair
public override void CreateNewKeyPair(IKeyStore keyStore, int keySize = 1024)
{
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(keySize))
{
rsa.PersistKeyInCsp = false;
keyStore.PublicKey.Key = rsa.ExportCspBlob(false);
keyStore.PrivateKey.Key = rsa.ExportCspBlob(true);
keyStore.KeySize = keySize;
}
}
示例11: ExportPrivateKey
public byte[] ExportPrivateKey()
{
using (var rsa = new RSACryptoServiceProvider(RSA_KEY_SIZE))
{
return rsa.ExportCspBlob(true);
}
}
示例12: Certificate
/// <summary>
/// Create a new certificate.
/// </summary>
/// <param name="language">Language preferred by the certificate holder.</param>
/// <param name="passphrase">Passphrase to encrypt the key with or null for no encryption.</param>
public Certificate(Language language, string passphrase)
{
CreationDate = DateTime.Now;
Id = Guid.NewGuid();
RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider();
rsaProvider.KeySize = 4096;
PublicKey = rsaProvider.ExportCspBlob(false);
PrivateKeyStatus = PrivateKeyStatus.Unencrypted;
if (passphrase == null)
{
this.privateKeyData = rsaProvider.ExportCspBlob(true);
}
else
{
EncryptPrivateKey(passphrase, rsaProvider);
Unlock(passphrase);
}
this.signatures = new List<Signature>();
this.attributes = new List<CertificateAttribute>();
AddAttribute(new Int32CertificateAttribute(CertificateAttributeName.Language, (int)language));
}
示例13: Create
public static ICspProxyFactory Create(int keySize)
{
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(keySize))
{
return new DefaultFactory(rsa.ExportCspBlob(true), rsa.KeySize);
}
}
示例14: SignN3Rsa
public string SignN3Rsa(string data)
{
var cspParams = new CspParameters {KeyContainerName = "XML_DSIG_RSA_KEY"};
var key = new RSACryptoServiceProvider(cspParams);
var cspBlob = key.ExportCspBlob(false);
var base64Blob = Convert.ToBase64String(cspBlob);
var rsaFormatter = new RSAPKCS1SignatureFormatter(key);
rsaFormatter.SetHashAlgorithm("MD5");
var hash = Md5Helper.GetMd5Hash(data);
var base64Hash = Convert.ToBase64String(hash);
var sign = rsaFormatter.CreateSignature(hash);
var base64Sign = Convert.ToBase64String(sign);
var signData = new SignData
{
data = data,
public_key = base64Blob,
hash = base64Hash,
sign = base64Sign
};
return new SerializationHelper<SignData>().Serialize(signData);
}
示例15: GenerateStrongNameKeyPair
/// <summary>
/// Generates a 1024 bit the strong-name key pair that can be written to an SNK file.
/// </summary>
/// <returns>A strong-name key pair array.</returns>
public static byte[] GenerateStrongNameKeyPair()
{
using (var provider = new RSACryptoServiceProvider(1024, new CspParameters() { KeyNumber = 2 }))
{
return provider.ExportCspBlob(!provider.PublicOnly);
}
}