[C#|OpenSSL] RSA is causing me headaches

WolFSharp's Avatar, Join Date: Mar 2011
Newbie Member
Hi, iam using an external library (OpenSSL.Crypto) to encrypt/decrypt files in my c# project.
I have to use RSA, but there is a lack of documentation, I tried to find an example but no way
i tried by myself to explore the library and i wrote this code:
Code:
byte[] msg = System.Text.Encoding.ASCII.GetBytes("text to encrypt");
            OpenSSL.Crypto.Envelope env = new OpenSSL.Crypto.Envelope();
            OpenSSL.Crypto.RSA rsa = new OpenSSL.Crypto.RSA();
            Console.Write("{0}",env.PublicKeyAsPEM);
            Console.Write("{0}", env.PrivateKeyAsPEM);
Question
Have any one used RSA before with this library?
E L N I N O's Avatar, Join Date: Feb 2012
Newbie Member
This is how you create a RSA key pair and use it to encrypt/decrypt a byte[]:

// Generate key pair
var rsa = new RSA();
rsa.GenerateKeys(2048, 0x10021, null, null);

// Encrypt file
byte[] payLoad = File.ReadAllBytes("c:\folder\myfile.ext");
byte[] encryptedResult = rsa.PublicEncrypt(payload, RSA.Padding.PKCS1);

// Decrypt file
byte[] decryptedResult = rsa.PrivateDecrypt(encryptedResult, RSA.Padding.PKCS1);

Hope this helps!