1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

[C#|OpenSSL] RSA is causing me headaches

Discussion in 'C#' started by WolFSharp, Mar 3, 2011.

  1. WolFSharp

    WolFSharp New Member

    Joined:
    Mar 2, 2011
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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 :nonod:
    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?
     
  2. E L N I N O

    E L N I N O New Member

    Joined:
    Feb 28, 2012
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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!
     

Share This Page