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.

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.

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.

Comments

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.

The

The

RSA algorithm is not a commutative operation.

v2

Comments

I'd say both keys are needed, because private key will never become into public and vice versa.

More details on Wikipedia

5ed!

More details on Wikipedia

5ed!

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 :)

Thanks for your support Maciej :)

:thumbsup:

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.

Quote: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 http://www.go4expert.com/articles/bouncy-castle-net-implementationrsa-t24827/
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:");
Console.WriteLine(BitConverter.ToString(cipherBytes));
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}");
Console.ReadLine();
}
}
}
```

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

CodeProject,
503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada
+1 416-849-8900 x 100