Click here to Skip to main content
14,449,862 members
Rate this:
Please Sign up or sign in to vote.
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.
Posted
Updated 11-Feb-20 22:38pm
Comments
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.
Rate this:
Please Sign up or sign in to vote.

Solution 1

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.
   
v2
Comments
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
5ed!
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
   
:thumbsup:
Rate this:
Please Sign up or sign in to vote.

Solution 2

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.
   
Rate this:
Please Sign up or sign in to vote.

Solution 3

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.
   
Rate this:
Please Sign up or sign in to vote.

Solution 4

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