本文整理汇总了C#中RSACryptoServiceProvider.ExportParameters方法的典型用法代码示例。如果您正苦于以下问题:C# RSACryptoServiceProvider.ExportParameters方法的具体用法?C# RSACryptoServiceProvider.ExportParameters怎么用?C# RSACryptoServiceProvider.ExportParameters使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类RSACryptoServiceProvider
的用法示例。
在下文中一共展示了RSACryptoServiceProvider.ExportParameters方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ExportAutoKey
public static void ExportAutoKey()
{
RSAParameters privateParams;
RSAParameters publicParams;
int keySize;
using (RSA rsa = new RSACryptoServiceProvider())
{
keySize = rsa.KeySize;
// We've not done anything with this instance yet, but it should automatically
// create the key, because we'll now asked about it.
privateParams = rsa.ExportParameters(true);
publicParams = rsa.ExportParameters(false);
// It shouldn't be changing things when it generated the key.
Assert.Equal(keySize, rsa.KeySize);
}
Assert.Null(publicParams.D);
Assert.NotNull(privateParams.D);
ValidateParameters(ref publicParams);
ValidateParameters(ref privateParams);
Assert.Equal(privateParams.Modulus, publicParams.Modulus);
Assert.Equal(privateParams.Exponent, publicParams.Exponent);
}
示例2: LargeKeyImportExport
public static void LargeKeyImportExport()
{
RSAParameters imported = TestData.RSA16384Params;
using (RSA rsa = new RSACryptoServiceProvider())
{
try
{
rsa.ImportParameters(imported);
}
catch (CryptographicException)
{
// The key is pretty big, perhaps it was refused.
return;
}
RSAParameters exported = rsa.ExportParameters(false);
Assert.Equal(exported.Modulus, imported.Modulus);
Assert.Equal(exported.Exponent, imported.Exponent);
Assert.Null(exported.D);
exported = rsa.ExportParameters(true);
AssertKeyEquals(ref imported, ref exported);
}
}
示例3: GenerateKey
private static void GenerateKey(Func<RSA, int> getSize)
{
int keySize;
using (var rsa = new RSACryptoServiceProvider())
{
keySize = getSize(rsa);
}
using (var rsa = new RSACryptoServiceProvider(keySize))
{
Assert.Equal(keySize, rsa.KeySize);
// Some providers may generate the key in the constructor, but
// all of them should have generated it before answering ExportParameters.
RSAParameters keyParameters = rsa.ExportParameters(false);
ImportExport.ValidateParameters(ref keyParameters);
// KeySize should still be what we set it to originally.
Assert.Equal(keySize, rsa.KeySize);
// KeySize describes the size of the modulus in bits
// So, 8 * the number of bytes in the modulus should be the same value.
Assert.Equal(keySize, keyParameters.Modulus.Length * 8);
}
}
示例4: PaddedExport
public static void PaddedExport()
{
// OpenSSL's numeric type for the storage of RSA key parts disregards zero-valued
// prefix bytes.
//
// The .NET 4.5 RSACryptoServiceProvider type verifies that all of the D breakdown
// values (P, DP, Q, DQ, InverseQ) are exactly half the size of D (which is itself
// the same size as Modulus).
//
// These two things, in combination, suggest that we ensure that all .NET
// implementations of RSA export their keys to the fixed array size suggested by their
// KeySize property.
RSAParameters diminishedDPParamaters = TestData.DiminishedDPParamaters;
RSAParameters exported;
using (RSA rsa = new RSACryptoServiceProvider())
{
rsa.ImportParameters(diminishedDPParamaters);
exported = rsa.ExportParameters(true);
}
// DP is the most likely to fail, the rest just otherwise ensure that Export
// isn't losing data.
AssertKeyEquals(ref diminishedDPParamaters, ref exported);
}
示例5: ToPublicRSAParameters
public static RSAParameters ToPublicRSAParameters(this string publicKeyXml)
{
using (var rsa = new RSACryptoServiceProvider())
{
rsa.FromXmlString(publicKeyXml);
return rsa.ExportParameters(includePrivateParameters: false);
}
}
示例6: Main
//Asymetric
void Main()
{
try
{
UnicodeEncoding ByteConverter = new UnicodeEncoding();
byte[] dataToEncrypt = ByteConverter.GetBytes("Data to Encrypt");
byte[] encryptedData;
byte[] decryptedData;
using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
{
encryptedData = RSAEncrypt(dataToEncrypt, RSA.ExportParameters(false));
decryptedData = RSADecrypt(encryptedData, RSA.ExportParameters(true));
Console.WriteLine("Decrypted plaintext: {0}", ByteConverter.GetString(decryptedData));
}
}
catch (ArgumentNullException)
{
Console.WriteLine("Encryption failed.");
}
}
示例7: RsaDecryptAfterExport
public static void RsaDecryptAfterExport()
{
byte[] output;
using (var rsa = new RSACryptoServiceProvider())
{
byte[] crypt = rsa.Encrypt(TestData.HelloBytes, true);
// Export the key, this should not clear/destroy the key.
RSAParameters ignored = rsa.ExportParameters(true);
output = rsa.Decrypt(crypt, true);
}
Assert.Equal(TestData.HelloBytes, output);
}
示例8: Main
public static void Main(string[] args) {
string path = args[0];
byte[] blob = null;
using(FileStream fs = File.Open(path, FileMode.Open)) {
blob = new byte[fs.Length];
fs.Read(blob, 0, blob.Length);
}
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.ImportCspBlob(blob);
RSAParameters PrivateKey = rsa.ExportParameters(true);
byte[] key = RSAKeyToASN1(PrivateKey);
using(FileStream fs = File.Open(path + ".out", FileMode.Create)) {
fs.Write(key, 0, key.Length);
}
Console.WriteLine("Your file is ready for you at " + path + ".out.");
}
示例9: Pkcs1DecodingTest
public void Pkcs1DecodingTest()
{
#pragma warning disable 0436
// Initialize the "Known Good" RSAParameters.
byte[] capi1Blob = Convert.FromBase64String(AsymmetricKeyAlgorithmProviderTests.Helper.PrivateKeyFormatsAndBlobs[Tuple.Create(PCLCrypto.AsymmetricAlgorithm.RsaOaepSha1, CryptographicPrivateKeyBlobType.Capi1PrivateKey)]);
var rsa = new RSACryptoServiceProvider();
rsa.ImportCspBlob(capi1Blob);
RSAParameters rsaCapi = rsa.ExportParameters(true);
// Now load up the tested one.
byte[] pkcs1KeyBlob = Convert.FromBase64String(AsymmetricKeyAlgorithmProviderTests.Helper.PrivateKeyFormatsAndBlobs[Tuple.Create(PCLCrypto.AsymmetricAlgorithm.RsaOaepSha1, CryptographicPrivateKeyBlobType.Pkcs1RsaPrivateKey)]);
RSAParameters homeReadPkcs1 = KeyFormatter.ToPlatformParameters(KeyFormatter.Pkcs1.Read(pkcs1KeyBlob));
#pragma warning restore 0436
Assert.Equal(WinRTCrypto.CryptographicBuffer.EncodeToHexString(rsaCapi.Modulus), WinRTCrypto.CryptographicBuffer.EncodeToHexString(homeReadPkcs1.Modulus));
Assert.Equal(WinRTCrypto.CryptographicBuffer.EncodeToHexString(rsaCapi.Exponent), WinRTCrypto.CryptographicBuffer.EncodeToHexString(homeReadPkcs1.Exponent));
Assert.Equal(WinRTCrypto.CryptographicBuffer.EncodeToHexString(rsaCapi.D), WinRTCrypto.CryptographicBuffer.EncodeToHexString(homeReadPkcs1.D));
Assert.Equal(WinRTCrypto.CryptographicBuffer.EncodeToHexString(rsaCapi.P), WinRTCrypto.CryptographicBuffer.EncodeToHexString(homeReadPkcs1.P));
Assert.Equal(WinRTCrypto.CryptographicBuffer.EncodeToHexString(rsaCapi.Q), WinRTCrypto.CryptographicBuffer.EncodeToHexString(homeReadPkcs1.Q));
Assert.Equal(WinRTCrypto.CryptographicBuffer.EncodeToHexString(rsaCapi.DP), WinRTCrypto.CryptographicBuffer.EncodeToHexString(homeReadPkcs1.DP));
Assert.Equal(WinRTCrypto.CryptographicBuffer.EncodeToHexString(rsaCapi.DQ), WinRTCrypto.CryptographicBuffer.EncodeToHexString(homeReadPkcs1.DQ));
Assert.Equal(WinRTCrypto.CryptographicBuffer.EncodeToHexString(rsaCapi.InverseQ), WinRTCrypto.CryptographicBuffer.EncodeToHexString(homeReadPkcs1.InverseQ));
}
示例10: VerifyLicenseKeyText
public static bool VerifyLicenseKeyText(this string licenseKeyText, out LicenseKey key)
{
var publicRsaProvider = new RSACryptoServiceProvider();
publicRsaProvider.FromXmlString(LicensePublicKey);
var publicKeyParams = publicRsaProvider.ExportParameters(false);
key = licenseKeyText.ToLicenseKey();
var originalData = key.GetHashKeyToSign().ToUtf8Bytes();
var signedData = Convert.FromBase64String(key.Hash);
return VerifySignedHash(originalData, signedData, publicKeyParams);
}
示例11: RSATest
private static void RSATest()
{
var publicPrivateRsa = new RSACryptoServiceProvider
(
new CspParameters()
{
KeyContainerName = "PublicPrivateKeys",
Flags = CspProviderFlags.UseMachineKeyStore
//Flags = CspProviderFlags.UseDefaultKeyContainer
}
)
{
PersistKeyInCsp = true,
};
var publicRsa = new RSACryptoServiceProvider(
new CspParameters()
{
KeyContainerName = "PublicKey",
Flags = CspProviderFlags.UseMachineKeyStore
//Flags = CspProviderFlags.UseDefaultKeyContainer
}
)
{
PersistKeyInCsp = true
};
//Export the key.
publicRsa.ImportParameters(publicPrivateRsa.ExportParameters(false));
Console.WriteLine(publicRsa.ToXmlString(false));
Console.WriteLine(publicPrivateRsa.ToXmlString(false));
//Dispose those two CSPs.
using (publicRsa)
{
publicRsa.Clear();
}
using (publicPrivateRsa)
{
publicRsa.Clear();
}
//Retrieve keys
publicPrivateRsa = new RSACryptoServiceProvider(
new CspParameters()
{
KeyContainerName = "PublicPrivateKeys",
Flags = CspProviderFlags.UseMachineKeyStore
//Flags = CspProviderFlags.UseDefaultKeyContainer
}
);
publicRsa = new RSACryptoServiceProvider(
new CspParameters()
{
KeyContainerName = "PublicKey",
Flags = CspProviderFlags.UseMachineKeyStore
//Flags = CspProviderFlags.UseDefaultKeyContainer
}
);
Console.WriteLine(publicRsa.ToXmlString(false));
Console.WriteLine(publicPrivateRsa.ToXmlString(false));
using (publicRsa)
{
publicRsa.Clear();
}
using (publicPrivateRsa)
{
publicRsa.Clear();
}
}
示例12: CreateKeys
/// <summary>
/// Generates a new set of keys
/// </summary>
/// <returns></returns>
public static RSAParameters CreateKeys()
{
CspParameters xCSP = new CspParameters();
RSACryptoServiceProvider xRSA = new RSACryptoServiceProvider(xCSP);
return xRSA.ExportParameters(true);
}
示例13: 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);
}
示例14: 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);
}
示例15: Enroll
/// <summary>
/// Enroll the authenticator with the server
/// </summary>
public bool Enroll(EnrollState state)
{
// clear error
state.Error = null;
try
{
var data = new NameValueCollection();
var cookies = state.Cookies = state.Cookies ?? new CookieContainer();
string response;
if (string.IsNullOrEmpty(state.OAuthToken) == true)
{
// get session
response = Request(COMMUNITY_BASE + "/login/home?goto=0", "GET", null, cookies);
// get the user's RSA key
data.Add("username", state.Username);
response = Request(COMMUNITY_BASE + "/login/getrsakey", "POST", data, cookies);
var rsaresponse = JObject.Parse(response);
if (rsaresponse.SelectToken("success").Value<bool>() != true)
{
throw new InvalidEnrollResponseException("Cannot get steam information for user: " + state.Username);
}
state.SteamId = rsaresponse.SelectToken("steamid").Value<string>();
// encrypt password with RSA key
RNGCryptoServiceProvider random = new RNGCryptoServiceProvider();
byte[] encryptedPassword;
using (var rsa = new RSACryptoServiceProvider())
{
var passwordBytes = Encoding.ASCII.GetBytes(state.Password);
var p = rsa.ExportParameters(false);
p.Exponent = Authenticator.StringToByteArray(rsaresponse.SelectToken("publickey_exp").Value<string>());
p.Modulus = Authenticator.StringToByteArray(rsaresponse.SelectToken("publickey_mod").Value<string>());
rsa.ImportParameters(p);
encryptedPassword = rsa.Encrypt(passwordBytes, false);
}
// login request
data = new NameValueCollection();
data.Add("password", Convert.ToBase64String(encryptedPassword));
data.Add("username", state.Username);
data.Add("twofactorcode", "");
data.Add("emailauth", (state.EmailAuthText != null ? state.EmailAuthText : string.Empty));
data.Add("loginfriendlyname", "#login_emailauth_friendlyname_mobile");
data.Add("captchagid", (state.CaptchaId != null ? state.CaptchaId : "-1"));
data.Add("captcha_text", (state.CaptchaText != null ? state.CaptchaText : "enter above characters"));
data.Add("emailsteamid", (state.EmailAuthText != null ? state.SteamId : string.Empty));
data.Add("rsatimestamp", rsaresponse.SelectToken("timestamp").Value<string>());
data.Add("remember_login", "false");
data.Add("oauth_client_id", "DE45CD61");
data.Add("oauth_scope", "read_profile write_profile read_client write_client");
data.Add("donotache", new DateTime().ToUniversalTime().Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds.ToString());
response = Request(COMMUNITY_BASE + "/mobilelogin/dologin/", "POST", data, cookies);
Dictionary<string, object> loginresponse = JsonConvert.DeserializeObject<Dictionary<string, object>>(response);
// require captcha
if (loginresponse.ContainsKey("captcha_needed") == true && (bool)loginresponse["captcha_needed"] == true)
{
state.RequiresCaptcha = true;
state.CaptchaId = (string)loginresponse["captcha_gid"];
state.CaptchaUrl = COMMUNITY_BASE + "/public/captcha.php?gid=" + state.CaptchaId;
}
else
{
state.RequiresCaptcha = false;
state.CaptchaId = null;
state.CaptchaUrl = null;
state.CaptchaText = null;
}
// require email auth
if (loginresponse.ContainsKey("emailauth_needed") == true && (bool)loginresponse["emailauth_needed"] == true)
{
if (loginresponse.ContainsKey("emaildomain") == true)
{
var emaildomain = (string)loginresponse["emaildomain"];
if (string.IsNullOrEmpty(emaildomain) == false)
{
state.EmailDomain = emaildomain;
}
}
state.RequiresEmailAuth = true;
}
else
{
state.EmailDomain = null;
state.RequiresEmailAuth = false;
}
// require email auth
if (loginresponse.ContainsKey("requires_twofactor") == true && (bool)loginresponse["requires_twofactor"] == true)
{
state.Requires2FA = true;
}
else
//.........这里部分代码省略.........