OpenSSL RSA Example

25,398

Solution 1

If you are using OpenSSL wrapper for .Net from this project.

You can take a look at test suite for this wrapper.

There is one test for RSA encryption/decryption you can found it here. Just read the TestKey method and it should be easy to use the library without any problems in case of any please let me know.

UPDATE

To generate and save keys:

OpenSSL.Crypto.RSA rsa = new OpenSSL.Crypto.RSA();
rsa.GenerateKeys(1024, 65537, null, null);

File.WriteAllText("MasterPrivateKey.pem", rsa.PrivateKeyAsPEM);
File.WriteAllText("MasterPublicKey.pem", rsa.PublicKeyAsPEM);

To create RSA class from file:

RSA rsa = RSA.FromPrivateKey(bin, OnPassword, null);

bin is instance of BIO class and should contains text you want to decrypt/encrypt. Sample code of reading file from console application mentioned in comments:

public static BIO GetInFile(string infile)
{
    BIO bio;
    if (string.IsNullOrEmpty(infile))
    {
            bio = BIO.MemoryBuffer();
            Stream cin = Console.OpenStandardInput();
            byte[] buf = new byte[1024];
            while (true)
            {
                    int len = cin.Read(buf, 0, buf.Length);
                    if (len == 0)
                            break;
                    bio.Write(buf, len);
            }
            return bio;
    }

    return BIO.File(infile, "r");
}            

OnPassword is a instance of PasswordHandler delegate with signature:

public static string OnPassword(bool verify, object arg)

this method should return password if there is any.

Solution 2

have you checked this example on microsoft website?
it's a full documentation of RSA in .Net with an example in the end! it's very easy to understand and reuse!

UPDATE1 This code is in C++ and it's using RSA .. it should be easy to understand as it's close to C#
i'll have more updates on my answer if i have something else .. i just wanted to post a starting point for you so you.
UPDATE2
This SO question has one answer that is not marked as a right answer .. however i think it might be useful, take a look at it!
UPDATE3
Check this link as a main source and this sub link which is a part of the first one for info about RSA keys

Share:
25,398

Related videos on Youtube

Rami W.
Author by

Rami W.

Updated on July 09, 2022

Comments

  • Rami W.
    Rami W. almost 2 years

    I have to use Crypto (OpenSSL) in my C# project, I could use all the symmetric ciphers and the message digests but i could'n use the RSA. Please, does any one know how to use it ? I mean how to manipulate the private/ public key automatically or manually (with big integers).

    EDIT

    byte[] msg = System.Text.Encoding.ASCII.GetBytes("text to encrypt");
    OpenSSL.Crypto.RSA rsa = new OpenSSL.Crypto.RSA();
    byte[]result = rsa.PrivateEncrypt(msg, OpenSSL.Crypto.RSA.Padding.None);
    Console.WriteLine(Convert.ToBase64String(result));
    

    I got AccessViolationException with this message : "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."

    and I thing something is missing, to configure RSA keys no?

  • Rami W.
    Rami W. about 13 years
    yes i used MS library and it's easier because of it's documentation, i must use an external library in my project, an open source one (OpenSSL)
  • Majd
    Majd about 13 years
    is there a reason for using exactly that library while you could easily us the MS one? I would recommend that you keep on trying for the purpose of learning and i'm gonna look up some examples for you (if there are any) .. however .. if there are no reason for not using the MS library .. i would suggest that u use it for now in order to save yourself some time!
  • Rami W.
    Rami W. about 13 years
    in my project i need to encrypt/decrypt files so i used MS library because it's easy but my boss told me that I have to use an external library, an open source one so i found "bouncy castle" and "OpenSSL"
  • Rami W.
    Rami W. about 13 years
    untill now i found how to encrypt/decrypt files withe symmetric cipher(AES, BlowFish..) using Crypto of OpenSSL but i could't use RSA. i tried ti find any example or documentation and no way. thank you very much for your help ;)
  • Rami W.
    Rami W. about 13 years
    it's dating from 2003 :s and the OpenSSL library contain too many new classes and methods. Look for example to create AES256 Cipher, i use this instruction OpenSSL.Crypto.CipherContext cc = new OpenSSL.Crypto.CipherContext(OpenSSL.Crypto.Cipher.AES_256_C‌​BC);"
  • Rami W.
    Rami W. about 13 years
    and to crypt i use cc.Crypt(byte[],key,iv,true); but for RSA i can't make it i don't know how to reconfigure private key and public key do you see ?
  • Rami W.
    Rami W. about 13 years
    i found the constructor OpenSSL.Crypto.RSA rsa = new OpenSSL.Crypto.RSA(); but now how to configure keys ? automatically or manually ?...i mean wich methods.
  • bartosz.lipinski
    bartosz.lipinski about 13 years
    I was always provinding keys from pem file. Take a look at consol application which is using this project openssl-net.git.sourceforge.net/git/gitweb.cgi?p=openssl-net‌​/… you can download it from git and debug it to see how its working
  • bartosz.lipinski
    bartosz.lipinski about 13 years
    The basic idea is to generate keys using this library with method rsa.GenerateKeys save them. (2 files private & public) and later read them from hdd to encrypt/decrypt text.
  • Rup
    Rup about 13 years
    You should edit this into your question instead - thanks. Yes, it looks like you'd need to set up keys first but I don't know that API.
  • Rami W.
    Rami W. about 13 years
    yes i guess so but look at the method rsa.GenerateKeys(int bits, OpenSSL.Core.BigNumber e, OpenSSL.Core.BigNumber.GeneratorHandler callback, object arg) bits = key length BigNumber = Big integer i guess P and Q but the last ones I cant understand.

Related