OpenSSL RSA Example
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
Related videos on Youtube
Rami W.
Updated on July 09, 2022Comments
-
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. about 13 yearsyes 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 about 13 yearsis 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. about 13 yearsin 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. about 13 yearsuntill 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. about 13 yearsit'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_CBC);"
-
Rami W. about 13 yearsand 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. about 13 yearsi 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 about 13 yearsI 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 about 13 yearsThe 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 about 13 yearsYou 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. about 13 yearsyes 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.