c# rsa example

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);
    }
  }
}

Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
c# rsa sign load rsa in c# rsa c sharp rsa implementation in c# c# rsa sign data RSA Algorithm With C# simple rsa encryption example C# RSA.Create linux c# rsa algorithm c# create rsa rsa encryption example in c# C# PRIVATE RSA SAMPLE c# rsa encryption example rsa key c# c# RSA explain rsa parameters example c# c# rsa .D c# rsa sample rsa encryption decryption c# microsoft System.Security.Cryptography.RSACryptoServiceProvider rsacryptoserviceprovider encrypt decrypt c# example c# rsa example with my key c# rsa encrypt decrypt string EXAMPLE c# rsa encrypt decrypt string c# rsa diffrent encryption c# rsa encrypt unique encryption RSACryptoServiceProvider c# RSACryptoServiceProvider RSACryptoServiceProvider # c# string rsa encryption rsa encryption algorithm c# rsacryptoserviceprovider c# example how to use rsa encryption c# .net core rsa 2048 encryption rsa cryptography sign c# c# rsa generate parameters to encrypt and decrypt rsa encryption with public key c# example Decrypt String RSA(System.Uri.UnescapeDataString(Token), 512, str PrivateKey); rsa cryptio hashsing data c# criptography with rsa kwy c# rsa crypto service provider rsacryptoserviceprovider c# RSA class c# c# rsa rsa microsft mvc rsa encryption c# example rsa encryption c# rsa in c# c# rsa encrypt decrypt example c# encrypt string with private key algorithm rsA256 c# encrypt string with private key algorithm rsa 256 c# microsoft rsa crypto provider rsa c# rsa algorithm c# c# RSA encrypt text based on private key c# how to use rsa encryption example RSA Encrypt in c# c# rsa encryption RSA Example C# rsa algorithm in c# RSA function in c# c# rsa example
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source