本文整理汇总了C#中RSACryptoServiceProvider.ExportCspBlob方法的典型用法代码示例。如果您正苦于以下问题:C# RSACryptoServiceProvider.ExportCspBlob方法的具体用法?C# RSACryptoServiceProvider.ExportCspBlob怎么用?C# RSACryptoServiceProvider.ExportCspBlob使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类RSACryptoServiceProvider
的用法示例。
在下文中一共展示了RSACryptoServiceProvider.ExportCspBlob方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ExportImportPublicOnly
public static void ExportImportPublicOnly()
{
byte[] expectedExport = ByteUtils.HexToByteArray(
"0602000000a40000525341310004000001000100e19a01644b82962a224781d1f60c2cc373b"
+ "798df541343f63c638f45fa96e11049c8d9e88bd56483ec3c2d56e9460d2b1140191841761c1523840221b0e"
+ "b6401dc4d09c54bf75cea25d9e191572fb2ec92c3559b35b3ef3fa695171bb1fddeb469792e49f0d17c769d0"
+ "a37f6a4a6584af39878eb21f9ba9eae8be9c39eac6ae0");
byte[] exported;
using (var rsa = new RSACryptoServiceProvider())
{
rsa.ImportParameters(TestData.CspTestKey);
exported = rsa.ExportCspBlob(includePrivateParameters: false);
}
Assert.Equal(expectedExport, exported);
using (var rsa = new RSACryptoServiceProvider())
{
rsa.ImportCspBlob(exported);
byte[] exported2 = rsa.ExportCspBlob(includePrivateParameters: false);
Assert.Equal(exported, exported2);
Assert.Throws<CryptographicException>(() => rsa.ExportCspBlob(includePrivateParameters: true));
}
}
示例2: Main
public static void Main(String []args) {
int keysize = 1024;
if(args.Length == 1) {
try {
keysize = Int32.Parse(args[0]);
}
catch {
Console.WriteLine("Default key size is 1024, specify 512 or 2048 as an input parameter.");
return;
}
}
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(keysize);
// Create public key file
byte[] rsa_public = rsa.ExportCspBlob(false);
FileStream public_file = File.Open("rsa_public", FileMode.Create);
public_file.Write(rsa_public, 0, rsa_public.Length);
public_file.Close();
// Create private key file
byte[] rsa_private = rsa.ExportCspBlob(true);
FileStream private_file = File.Open("rsa_private", FileMode.Create);
private_file.Write(rsa_private, 0, rsa_private.Length);
private_file.Close();
}
示例3: ExportImportPublicPrivate
public static void ExportImportPublicPrivate()
{
// This blob contains the private key of TestData.CspTestKey. The guidelines for the TestData class
// as to key security/sanity apply to this blob as well.
byte[] expectedExport = ByteUtils.HexToByteArray(
"0702000000a40000525341320004000001000100e19a01644b82962a224781d1f60c2cc373b"
+ "798df541343f63c638f45fa96e11049c8d9e88bd56483ec3c2d56e9460d2b1140191841761c1523840221b0e"
+ "b6401dc4d09c54bf75cea25d9e191572fb2ec92c3559b35b3ef3fa695171bb1fddeb469792e49f0d17c769d0"
+ "a37f6a4a6584af39878eb21f9ba9eae8be9c39eac6ae07bb43a9f6e29e584b47303f8ac70384ba4f1a4b7d77"
+ "fb4c931c2a194584b9d6060d39ba798e20698221ac615b083bbdaf2b6f39c05c570276945728800b1aae1531"
+ "511b5878dae8820a178f8cc3cca5426ce761ef3247bce9375318a03c3d5779ed339d2f9d04d6265d0a99057c"
+ "c1af86b656541f4f6b062d8407968aaf794fee33273c0fd4735d688e0e8161f5c9f360c2fc1caed9a2b48a53"
+ "3ea4d26b9ac50a0e7e7ca94c6bd6edfd3fe448650b66fa99c57b50e3737fae9d26300fee06649472a664190e"
+ "a603126718f896bbfe0671401f31414678d173d32c486c8fbb6334fe90c77f7c2a04ee9c3e3ab85d948357f7"
+ "15e5d706031e013f0951eeb1e506c5af71cfec07bbc637d5b7c788fdad21ec5f250ef069d00a5c9bb6e2fe06"
+ "01b91f36121885011cd7186093ee25c2a5dd6b3cfea3d8b1627148ab0a47610b8d99743ac008b62f8a054c18"
+ "4b8b9f862beebc70af40408999bead5a09baec588375be03cfa636b018d7d9948f1abae4d5463c5c5d210a0b"
+ "42589a90a2bc01b1bb027f6c859de82ace0c60237d96574a1752e38b56326c7eae33cf7590da6728ff1de184"
+ "c654fccba0866732e576747107cef935d43aa5f477178aafee834a53a3d14");
byte[] exported;
using (var rsa = new RSACryptoServiceProvider())
{
rsa.ImportParameters(TestData.CspTestKey);
exported = rsa.ExportCspBlob(includePrivateParameters: true);
}
Assert.Equal(expectedExport, exported);
using (var rsa = new RSACryptoServiceProvider())
{
rsa.ImportCspBlob(exported);
byte[] exported2 = rsa.ExportCspBlob(includePrivateParameters: true);
Assert.Equal<byte>(exported, exported2);
}
}
示例4: RSAParametersToBlob_PublicOnly
public static void RSAParametersToBlob_PublicOnly()
{
byte[] blob;
using (var rsa = new RSACryptoServiceProvider())
{
rsa.ImportParameters(TestData.RSA1024Params);
blob = rsa.ExportCspBlob(false);
}
RSAParameters exported;
using (var rsa = new RSACryptoServiceProvider())
{
rsa.ImportCspBlob(blob);
Assert.True(rsa.PublicOnly);
exported = rsa.ExportParameters(false);
}
RSAParameters expected = new RSAParameters
{
Modulus = TestData.RSA1024Params.Modulus,
Exponent = TestData.RSA1024Params.Exponent,
};
ImportExport.AssertKeyEquals(ref expected, ref exported);
}
示例5: RSAParametersToBlob_PublicPrivate
public static void RSAParametersToBlob_PublicPrivate()
{
byte[] blob;
using (var rsa = new RSACryptoServiceProvider())
{
rsa.ImportParameters(TestData.RSA1024Params);
blob = rsa.ExportCspBlob(true);
}
RSAParameters exported;
using (var rsa = new RSACryptoServiceProvider())
{
rsa.ImportCspBlob(blob);
Assert.False(rsa.PublicOnly);
exported = rsa.ExportParameters(true);
}
RSAParameters expected = TestData.RSA1024Params;
ImportExport.AssertKeyEquals(ref expected, ref exported);
}
示例6: CreateKey_LegacyProvider_RoundtripBlob
public static void CreateKey_LegacyProvider_RoundtripBlob()
{
const int KeySize = 512;
CspParameters cspParameters = new CspParameters(PROV_RSA_FULL);
byte[] blob;
using (var rsa = new RSACryptoServiceProvider(KeySize, cspParameters))
{
CspKeyContainerInfo containerInfo = rsa.CspKeyContainerInfo;
Assert.Equal(PROV_RSA_FULL, containerInfo.ProviderType);
Assert.Equal(KeySize, rsa.KeySize);
blob = rsa.ExportCspBlob(true);
}
using (var rsa = new RSACryptoServiceProvider())
{
rsa.ImportCspBlob(blob);
CspKeyContainerInfo containerInfo = rsa.CspKeyContainerInfo;
// The provider information is not persisted in the blob
Assert.Equal(PROV_RSA_AES, containerInfo.ProviderType);
Assert.Equal(KeySize, rsa.KeySize);
}
}
示例7: NonExportable_Persisted
public static void NonExportable_Persisted()
{
CspParameters cspParameters = new CspParameters
{
KeyContainerName = Guid.NewGuid().ToString(),
Flags = CspProviderFlags.UseNonExportableKey,
};
using (new RsaKeyLifetime(cspParameters))
{
using (var rsa = new RSACryptoServiceProvider(cspParameters))
{
Assert.False(rsa.CspKeyContainerInfo.Exportable, "rsa.CspKeyContainerInfo.Exportable");
Assert.Throws<CryptographicException>(() => rsa.ExportCspBlob(true));
Assert.Throws<CryptographicException>(() => rsa.ExportParameters(true));
}
}
}
示例8: NonExportable_Ephemeral
public static void NonExportable_Ephemeral()
{
CspParameters cspParameters = new CspParameters
{
Flags = CspProviderFlags.UseNonExportableKey,
};
using (var rsa = new RSACryptoServiceProvider(cspParameters))
{
// Ephemeral keys don't successfully request the exportable bit.
Assert.ThrowsAny<CryptographicException>(() => rsa.CspKeyContainerInfo.Exportable);
Assert.Throws<CryptographicException>(() => rsa.ExportCspBlob(true));
Assert.Throws<CryptographicException>(() => rsa.ExportParameters(true));
}
}
示例9: NamedKey_AlternateProvider
public static void NamedKey_AlternateProvider()
{
const int KeySize = 512;
CspParameters cspParameters = new CspParameters(PROV_RSA_FULL)
{
KeyContainerName = Guid.NewGuid().ToString(),
};
using (new RsaKeyLifetime(cspParameters))
{
byte[] privateBlob;
string uniqueKeyContainerName;
using (var rsa = new RSACryptoServiceProvider(KeySize, cspParameters))
{
Assert.True(rsa.PersistKeyInCsp);
Assert.Equal(PROV_RSA_FULL, rsa.CspKeyContainerInfo.ProviderType);
privateBlob = rsa.ExportCspBlob(true);
Assert.Equal(cspParameters.KeyContainerName, rsa.CspKeyContainerInfo.KeyContainerName);
uniqueKeyContainerName = rsa.CspKeyContainerInfo.UniqueKeyContainerName;
Assert.NotNull(uniqueKeyContainerName);
Assert.NotEqual(string.Empty, uniqueKeyContainerName);
}
// Fail if the key didn't persist
cspParameters.Flags |= CspProviderFlags.UseExistingKey;
using (var rsa = new RSACryptoServiceProvider(cspParameters))
{
Assert.True(rsa.PersistKeyInCsp);
Assert.Equal(KeySize, rsa.KeySize);
// Since we're specifying the provider explicitly it should still match.
Assert.Equal(PROV_RSA_FULL, rsa.CspKeyContainerInfo.ProviderType);
Assert.Equal(uniqueKeyContainerName, rsa.CspKeyContainerInfo.UniqueKeyContainerName);
byte[] blob2 = rsa.ExportCspBlob(true);
Assert.Equal(privateBlob, blob2);
}
}
}
示例10: NamedKey_DefaultProvider
public static void NamedKey_DefaultProvider()
{
const int KeySize = 2048;
CspParameters cspParameters = new CspParameters
{
KeyContainerName = Guid.NewGuid().ToString(),
};
using (new RsaKeyLifetime(cspParameters))
{
byte[] privateBlob;
string uniqueKeyContainerName;
using (var rsa = new RSACryptoServiceProvider(KeySize, cspParameters))
{
Assert.True(rsa.PersistKeyInCsp, "rsa.PersistKeyInCsp");
Assert.Equal(cspParameters.KeyContainerName, rsa.CspKeyContainerInfo.KeyContainerName);
uniqueKeyContainerName = rsa.CspKeyContainerInfo.UniqueKeyContainerName;
Assert.NotNull(uniqueKeyContainerName);
Assert.NotEqual(string.Empty, uniqueKeyContainerName);
privateBlob = rsa.ExportCspBlob(true);
Assert.True(rsa.CspKeyContainerInfo.Exportable, "rsa.CspKeyContainerInfo.Exportable");
}
// Fail if the key didn't persist
cspParameters.Flags |= CspProviderFlags.UseExistingKey;
using (var rsa = new RSACryptoServiceProvider(cspParameters))
{
Assert.True(rsa.PersistKeyInCsp);
Assert.Equal(KeySize, rsa.KeySize);
Assert.Equal(uniqueKeyContainerName, rsa.CspKeyContainerInfo.UniqueKeyContainerName);
byte[] blob2 = rsa.ExportCspBlob(true);
Assert.Equal(privateBlob, blob2);
}
}
}
示例11: GenerateCACert
public bool GenerateCACert(string group)
{
if(!Context.Request.IsLocal) {
throw new Exception("Call must be made locally!");
}
string private_path = GetGroupPrivatePath(group);
Directory.CreateDirectory(private_path);
private_path += "private_key";
RSACryptoServiceProvider private_key = new RSACryptoServiceProvider(2048);
byte[] private_blob = private_key.ExportCspBlob(true);
using(FileStream fs = File.Open(private_path, FileMode.Create)) {
fs.Write(private_blob, 0, private_blob.Length);
}
string data_path = GetGroupDataPath(group);
Directory.CreateDirectory(data_path);
RSACryptoServiceProvider public_key = new RSACryptoServiceProvider();
public_key.ImportCspBlob(private_key.ExportCspBlob(false));
CertificateMaker cm = new CertificateMaker(string.Empty, group,
string.Empty, "admin", string.Empty, public_key, string.Empty);
Certificate cert = cm.Sign(cm, private_key);
string cacert_path = GetGroupDataPath(group) + "cacert";
byte[] cert_data = cert.X509.RawData;
using(FileStream fs = File.Open(cacert_path, FileMode.Create)) {
fs.Write(cert_data, 0, cert_data.Length);
}
return true;
}
示例12: GenerateKeyFile
static int GenerateKeyFile(string fileName)
{
// do not overwrite if the file already exists
var keyFile = GetFileName(fileName);
if (File.Exists(keyFile))
{
return 0;
}
// generate key pair and export it as a blob
var parms = new CspParameters();
parms.KeyNumber = 2;
var provider = new RSACryptoServiceProvider(KeySize, parms);
var array = provider.ExportCspBlob(!provider.PublicOnly);
File.WriteAllBytes(keyFile, array);
return 0;
}