Click here to Skip to main content
15,314,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a string for example str = "Hello World" which I am suppose to encrypt using private key of RSA algorithm and decrypt the same using public key in .Net.

What I have tried:

I have tried various techniques available in .net.There are several ways of encrypting a string using public key and decrypting the same using private key of rsa. But vice versa doesn't seem to be working. Please help.
Updated 11-Feb-20 21:38pm
johannesnestler 11-Feb-20 10:12am
If you wan't to add "security" to whatever codebase you have now, you really have to understand the implications, it's not an easy topic, but from your question it seems you lack very basic understanding of Private/Public Key encryption. So try first to understand how it works, then implement correct. If you have any Questions feel free to ask.

No, this is the other way around.
The public key is used to encrypt messages and verify signatures.
The private key, on the other hand, is used to decrypt and sign messages.
RSA algorithm is not a commutative operation.
Maciej Los 10-Feb-20 7:08am
I'd say both keys are needed, because private key will never become into public and vice versa.
More details on Wikipedia
phil.o 10-Feb-20 9:43am
Of course, they form a pair of values fitting one another. OP's question is whether they could be used in the reverse order to produce an equivalent result, which is not the case.
Thanks for your support Maciej :)
Maciej Los 10-Feb-20 9:50am
If something can be decrypted via a public key then there is no encryption. What you are doing is having a keypad on a door and putting a sticky note next to the pad that says what the combination is.
Is there any way to encrypt a string using private key in RSA algorithm and decrypt the same using public key in C# .NET?

RSA works the other way around, encryption is with public key, decryption with private key. This is how the algorithm works.
The answer do not depend on a given language.

Try using the NuGet package BouncyCastle.NetCore. It is more flexible than the RSACryptoServiceProvider and its associated classes. Here is a NetCore Console example. The size of the message that can be encrypted is limited to the size of the keys.

//Derived from
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Generators;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Security;
using System;
using System.Text;

namespace EncryptString
    class Program
        public static void Main()
            RsaKeyPairGenerator rsaKeyPairGenerator = new RsaKeyPairGenerator();
            int keySize = 2048;//in bits
            rsaKeyPairGenerator.Init(new KeyGenerationParameters(new SecureRandom(), keySize));
            var keyPair = rsaKeyPairGenerator.GenerateKeyPair();
            var publicKey = keyPair.Public;
            var privateKey = keyPair.Private;
            var rsa = new RsaEngine();
            rsa.Init(true, privateKey);//use private key for encryption-not recommended
            string message = "Hello World";
            Console.WriteLine($"Original text: {message}");
            var plainTextBytes = Encoding.UTF8.GetBytes(message);
            byte[] cipherBytes = rsa.ProcessBlock(plainTextBytes, 0, message.Length);
            Console.WriteLine("Encrypted text as a byte array:");
            rsa.Init(false, publicKey);//use public key for decryption
            byte[] decryptedData = rsa.ProcessBlock(cipherBytes, 0, cipherBytes.Length);
            string decipheredText = Encoding.UTF8.GetString(decryptedData);
            Console.WriteLine($"Decrypted text: {decipheredText}");

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900