encrypt with public key and decrypt with private key c#

private static string _privateKey;
private static string _publicKey;
private static UnicodeEncoding _encoder = new UnicodeEncoding();

private static void RSA()
{
  var rsa = new RSACryptoServiceProvider();
  _privateKey = rsa.ToXmlString(true);
  _publicKey = rsa.ToXmlString(false);

  var text = "Test1";
  Console.WriteLine("RSA // Text to encrypt: " + text);
  var enc = Encrypt(text);
  Console.WriteLine("RSA // Encrypted Text: " + enc);
  var dec = Decrypt(enc);
  Console.WriteLine("RSA // Decrypted Text: " + dec);
}

public static string Decrypt(string data)
{
  var rsa = new RSACryptoServiceProvider();
  var dataArray = data.Split(new char[] { ',' });
  byte[] dataByte = new byte[dataArray.Length];
  for (int i = 0; i < dataArray.Length; i++)
  {
    dataByte[i] = Convert.ToByte(dataArray[i]);
  }

  rsa.FromXmlString(_privateKey);
  var decryptedByte = rsa.Decrypt(dataByte, false);
  return _encoder.GetString(decryptedByte);
}

public static string Encrypt(string data)
{
  var rsa = new RSACryptoServiceProvider();
  rsa.FromXmlString(_publicKey);
  var dataToEncrypt = _encoder.GetBytes(data);
  var encryptedByteArray = rsa.Encrypt(dataToEncrypt, false).ToArray();
  var length = encryptedByteArray.Count();
  var item = 0;
  var sb = new StringBuilder();
  foreach (var x in encryptedByteArray)
  {
    item++;
    sb.Append(x);

    if (item < length)
      sb.Append(",");
  }

  return sb.ToString();
}

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
how to encrypt a string with a public key and decrypt with private key decrypt the encrypted data using private key + C# encrypt with public key and decrypt with private key openssl c# decrypte information using private key C# encrypted private key decrypt how to decrypt using a public key encrypted RSA and decrypt c# decryption with public key if i encrypt using private key can i decrypt with public key how to decrypt a public key java encrypt/decrypt using public private key how does a private key decrypt a public key decrypt with private key c# encrypt using rsa public key cryptojs encrypt with public key decrypt with private key can i decrypt a message that encrypted using public key using public key can i decrypt using public key can we encrypt and decrypt with a public key rsa encrypt with public key c# c# encrypt string with private key decrypt message with public key can public key be used to decrypt public key to decrypt private to encrypt private public key encryption decryption decryption using public key encrypt data using public private key c# decrypt text using public key Anyone can encrypt with public key. Only one person can decrypt with the private key decrypt public key c# decrypt public key cryptojs encrypt with public key decrypt private key rsa encrypt finding the private key decrypt with public key does public key encrypt or decrypt c# encrypt(string with private key) encrypt with private key and decrypt with public key on other pc c# encrypt with private key how encryption and decryption work in public key decrypt using public key in public key encryption messages are decrypted using the c# rsa encrypt with public key string decrypt with public key in public key encryption which keys are used decryption encrypt using private key and decrypt using public key in js rsa does public key encrypt or decrypt how does public key decryption work decrypt give public key c# rsa decrypt with public key c# rsa encrypt with public key can the public key be used to decrypt the message public key encryption decrypt with public key public key encryption decrypt with encrypt data with public key and decrypt with private key c# encrypt private key decrypt public key c# decrypt private key public key can we encrypt using private key and decrypt with password public key encryption and decryption ssh-keygen public private encrypt decrypt decrypt using public and private rsa keys Encrypt with public key and decrypt with private key C#
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