c# rsa example
C#
using System;
using System.Security.Cryptography;
namespace RsaCryptoExample
{
static class Program
{
static void Main()
{
//lets take a new CSP with a new 2048 bit rsa key pair
var csp = new RSACryptoServiceProvider(2048);
//how to get the private key
var privKey = csp.ExportParameters(true);
//and the public key ...
var pubKey = csp.ExportParameters(false);
//converting the public key into a string representation
string pubKeyString;
{
//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();
}
//converting it back
{
//get a stream from the string
var sr = new System.IO.StringReader(pubKeyString);
//we need a deserializer
var xs = new System.Xml.Serialization.XmlSerializer(typeof(RSAParameters));
//get the object back from the stream
pubKey = (RSAParameters)xs.Deserialize(sr);
}
//conversion for the private key is no black magic either ... omitted
//we have a public key ... let's get a new csp and load that key
csp = new RSACryptoServiceProvider();
csp.ImportParameters(pubKey);
//we need some data to encrypt
var plainTextData = "foobar";
//for encryption, always handle bytes...
var bytesPlainTextData = System.Text.Encoding.Unicode.GetBytes(plainTextData);
//apply pkcs#1.5 padding and encrypt our data
var bytesCypherText = csp.Encrypt(bytesPlainTextData, false);
//we might want a string representation of our cypher text... base64 will do
var cypherText = Convert.ToBase64String(bytesCypherText);
/*
* some transmission / storage / retrieval
*
* and we want to decrypt our cypherText
*/
//first, get our bytes back from the base64 string ...
bytesCypherText = Convert.FromBase64String(cypherText);
//we want to decrypt, therefore we need a csp and load our private key
csp = new RSACryptoServiceProvider();
csp.ImportParameters(privKey);
//decrypt and strip pkcs#1.5 padding
bytesPlainTextData = csp.Decrypt(bytesCypherText, false);
//get our original plainText back...
plainTextData = System.Text.Encoding.Unicode.GetString(bytesPlainTextData);
}
}
}
Also in C#:
- Title
- winforms c# add data to datagridview with a button
- Category
- C#
- Title
- how to find the area of a circle
- Category
- C#
- Title
- get last element of array c#
- Category
- C#
- Title
- c# relaxed boolean cast
- Category
- C#
- Title
- c# making a folder
- Category
- C#
- Title
- unity load next scene
- Category
- C#
- Title
- cshtml foreach
- Category
- C#
- Title
- create asp.net which send email and sms using own api
- Category
- C#
- Title
- unity try get component
- Category
- C#
- Title
- radians to degree c#
- Category
- C#
- Title
- calculate impact damage + unity
- Category
- C#
- Title
- list.addrange in c#
- Category
- C#
- Title
- how to exit a program in c#
- Category
- C#
- Title
- unity3d quaternion add 90 degrees
- Category
- C#
- Title
- C# type cast float to string
- Category
- C#
- Title
- c# string methods
- Category
- C#
- Title
- serilog loglevel order
- Category
- C#
- Title
- double tryparse dot comma
- Category
- C#
- Title
- Instantiate c#
- Category
- C#
- Title
- C# for
- Category
- C#
- Title
- c# mixed multidimensional array
- Category
- C#
- Title
- unity image
- Category
- C#
- Title
- moq raise event
- Category
- C#
- Title
- epplus excel vb.net
- Category
- C#
- Title
- c# arraylist contains
- Category
- C#
- Title
- socket in c#
- Category
- C#
- Title
- how to use more than one condition in ef join
- Category
- C#
- Title
- the request was aborted could not create ssl/tls secure channel. c# restsharp
- Category
- C#
- Title
- strtorime online
- Category
- C#
- Title
- List C# add from List
- Category
- C#
- Title
- c# null conditional
- Category
- C#
- Title
- C# check many strings quickly
- Category
- C#
- Title
- Random number unity
- Category
- C#
- Title
- the name scripts does not exist in the current context mvc 5
- Category
- C#
- Title
- C# Console multi language
- Category
- C#
- Title
- selection sort c#
- Category
- C#
- Title
- how to call a method from a class in c#
- Category
- C#
- Title
- get gameobject by name
- Category
- C#
- Title
- rotate player unity
- Category
- C#
- Title
- get attribute value of xml element c#
- Category
- C#
- Title
- unity array of child objects
- Category
- C#
- Title
- unity matchinfo
- Category
- C#
- Title
- split using string c#
- Category
- C#
- Title
- setting the parent of a transform which resides in a prefab
- Category
- C#
- Title
- uinput dialog uwp c#
- Category
- C#
- Title
- c# iorderedenumerable to dictionary
- Category
- C#
- Title
- c# how to convert string to int
- Category
- C#
- Title
- scaffold-dbcontext sql server
- Category
- C#
- Title
- unity c# is not equal to
- Category
- C#
- Title
- dontdestroyonload unity
- Category
- C#
- Title
- Time delay C# unity
- Category
- C#
- Title
- change sprite of gameobject unity
- Category
- C#
- Title
- newtonsoft create dynamic object
- Category
- C#
- Title
- c# to vb.net
- Category
- C#
- Title
- c# ^ operator
- Category
- C#
- Title
- application isPlaying unity
- Category
- C#
- Title
- membership get user id
- Category
- C#
- Title
- first person camera controller unity
- Category
- C#
- Title
- page refresh on button click in c#
- Category
- C#
- Title
- check if enabled unity
- Category
- C#
- Title
- c# property get set
- Category
- C#
- Title
- windows forms iterate through all controls
- Category
- C#
- Title
- class selector to property in asp net core dropdown
- Category
- C#
- Title
- Failed to retrieve Firebase Instance Id
- Category
- C#
- Title
- system.io.directorynotfoundexception c#
- Category
- C#
- Title
- dto and automapper
- Category
- C#
- Title
- linq query select top 1 c#
- Category
- C#
- Title
- how to cast list to observablecollection c#
- Category
- C#
- Title
- how to make game unity c#
- Category
- C#
- Title
- c# implement a superclass in subclass
- Category
- C#
- Title
- get waht is differnt between two arrays c#
- Category
- C#
- Title
- add row and columns to grid wpf in code
- Category
- C#
- Title
- unity hide mouse
- Category
- C#
- Title
- csharp attributes as generics constraints
- Category
- C#
- Title
- c# httpclient azure function authorization
- Category
- C#
- Title
- combobox change datasource c#
- Category
- C#
- Title
- unity round vector 3 to nearest integer
- Category
- C#
- Title
- c# convert int to string
- Category
- C#
- Title
- c# fold list
- Category
- C#
- Title
- c# escape characters
- Category
- C#
- Title
- c# convert dictionary to anonymous object
- Category
- C#
- Title
- visual studio C# hintergrund bild ändern
- Category
- C#
- Title
- set textbox colour to transparent c#
- Category
- C#
- Title
- unity transfoprm position y change
- Category
- C#
- Title
- C# extend array
- Category
- C#
- Title
- c# how to open file explorer
- Category
- C#
- Title
- how to draw a dot in c#
- Category
- C#
- Title
- vector3.lerp
- Category
- C#
- Title
- json tiers dot in name c#
- Category
- C#
- Title
- import regex c#
- Category
- C#
- Title
- no overload for 'useItemOnSceneLoad' matches delegate 'UnityAction<Scene, LoadSceneMode>'
- Category
- C#
- Title
- convert string to double c#
- Category
- C#
- Title
- c# edit element in list
- Category
- C#
- Title
- c# float to string
- Category
- C#
- Title
- countdownevent
- Category
- C#
- Title
- bind repeater to dictionary
- Category
- C#
- Title
- in unity i want to destroy a gameobject when it hits the edge of the screen
- Category
- C#
- Title
- C# .NET Core linq Distinct
- Category
- C#
- Title
- how to remove file changes in git
- Category
- C#
- Title
- c# windows application get current path
- Category
- C#