0

I have generated random number using RNGCryptoServiceProvider, the next requirement is to generate a private, public RSA key pair which can be used for encryption/decryption purposes.

How to construct the RSAParameters object using that random number, because the RSA parameters can be imported to create RSACryptoServiceProvider object which will eventually used to encrypt the raw data.

  • Not all the random numbers are good RSA keys... They must be prime (or semi-primes if I remember correctly)... For example an algorithm to "find" a key is described here: http://crypto.stackexchange.com/a/1971 – xanatos Mar 06 '17 at 09:24
  • Why are you rolling your own? DiY security always ends in tears ... – H H Mar 06 '17 at 09:44
  • @HenkHolterman I have to tailor the requirement from client...I am just combining various methods like hashing, encryption, signing of data for the customer...do you suggest any library for these tasks – Samarth Srivastava Mar 06 '17 at 10:18
  • @SamarthSrivastava No, he suggests that if you have to create a key, you simply `new RSACryptoServiceProvider(keySizeInBits)` and leave the work to it. – xanatos Mar 06 '17 at 10:37
  • Unless that client is also doing it DiY you should be able to use standard library components. Which ones, I don't know. This is starting to look like an [XY-question](http://meta.stackexchange.com/a/66378). – H H Mar 06 '17 at 10:38
  • 2
    If you are asked to build a dam, but you are not competent to build a dam, you should not build the dam. People could die. – Ben Mar 06 '17 at 11:03
  • @Ben well, thats why I am learning to do it, I dont sit back with a sulky pout face saying 'I don't know, so I will not try doing' – Samarth Srivastava Mar 07 '17 at 08:01
  • @HenkHolterman, I was able to do it using BouncyCastle [link] (http://www.bouncycastle.org/csharp/) and I am posting the code as answer – Samarth Srivastava Mar 07 '17 at 08:01
  • You have missed the point. It is not safe for you to do it. You need to get someone else to do it and show you how. Only when you are competent should you try to do it without supervision. Asking the occasional question on SO is not enough because of the unknown unknowns. – Ben Mar 07 '17 at 10:19
  • @Ben You are right. Thanks for advice. I have posted the answer how I achieved my objective. Do you have any suggestions if anything is not right. – Samarth Srivastava Mar 07 '17 at 11:04

1 Answers1

0

This is how I converted the random number to private/public key pair using BouncyCastlelink

byte[] btRandomNumber = new byte[1000000];
        using (RNGCryptoServiceProvider r = new RNGCryptoServiceProvider())
        {
            r.GetBytes(btRandomNumber);
        }

int RsaKeySize = 1024;

        Org.BouncyCastle.Security.SecureRandom secureRandom = new Org.BouncyCastle.Security.SecureRandom(btRandomNumber);
        var keyGenerationParameters = new Org.BouncyCastle.Crypto.KeyGenerationParameters(secureRandom, RsaKeySize);
        var keyPairGenerator = new Org.BouncyCastle.Crypto.Generators.RsaKeyPairGenerator();
        keyPairGenerator.Init(keyGenerationParameters);
        Org.BouncyCastle.Crypto.AsymmetricCipherKeyPair keyPair = keyPairGenerator.GenerateKeyPair(); 

Welcome to any corrections or some other way to do this