# Manually encrypt using RSA X509 in .NET

I don't want to utilize RSaCryptoProvider available in .NET to encrypt data.

I would like to encrypt data using the RSA keys, allowing me to send a message to Alice using his public-signature and allowing Alice decrypt it using its private one.

My problem is focused in an application which people can select the symetric algorithm to encrypt the data (among many of them), not only 3DES, AES... I mean, algorithms not provided neither by RSACryptoProvider nor DSACryptoProvider.

Questions:

1) So, I understand I have to get RSA parameters D, Q, p, n to create the "secret-key" which allow me to encrypt/decrypt data in my symmetric algorithm. Is it correct?

2) Supposing I have a number to work, based on the RSA formula (C = M^e mod n and D= C^d mod n) what exactly is M and C: a byte, a word, a double-word?

3) To make my job easier, I think to utilize a padding schema (like PKCS#1). In this case, the above question should be automatically answered? Is PKCS#1 matching the necessary lenght to operate with RSA formulas?

In fact, I would like to utilize RSA keys to provide a symetric encryption in a true manual way.

I will appreciate any help.

• You should take a look at rfc3447. Just a note: this isn't a quick implementation, it is probably best to find an open source library that suits your needs. – Jacob H May 4 '18 at 6:02
• The problem with open-source libraries like Bouncy-Castle is the completeness and size of these libraries vs. what I need - I know that it should be just small-size code. I know the theory about RSA (the primes, the Phi, etc.), but I'm getting lost in how threat the numbers vs. the message (plaintext). ANyway, thanks for your answer. – David BS May 5 '18 at 2:39
• rfc3447 goes through how to implement RSA and should answer all of your questions. If you are planning on writing an RSA implementation yourself you should read through the document. For example, to answer your second question, sections 4.1/4.2 are the function that converts an octet string (your plaintext) into an integer and back. – Jacob H May 5 '18 at 3:10
• Can you elaborate on why you are trying to do this? From the description you have given, it sounds like you are trying to shoot yourself in the foot, but without the help of a gun—rather by holding a bullet in your hand and manually hitting it with a hammer until it discharges. RSA is rather difficult to use safely, and most of the verbiage around it especially in popular science and programming articles is hopelessly confusing and wrong. Probably the simplest secure message encryption scheme using RSA is RSA-KEM with some DEM (e.g., AES-GCM). – Squeamish Ossifrage May 7 '18 at 20:23
• @DavidBS May I recommend stepping back and finding a good book on cryptography engineering, such as Cryptography Engineering by Niels Ferguson, Tadayoshi Kohno, and Bruce Schneier? The difficulty of using RSA safely is not just about generating primes and computing $\phi(p\cdot q)$, and if you're unclear about signature vs. encryption, public-key vs. symmetric-key, etc., as your question suggests, you are getting ready to shoot yourself and all your users in their respective feet. – Squeamish Ossifrage May 7 '18 at 21:51