OpenSSL的RSA例 [英] OpenSSL RSA Example

查看:341
本文介绍了OpenSSL的RSA例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用加密(OpenSSL的)在我的C#项目,我可以使用所有的对称密码算法和消息摘要,但我could'n使用RSA。请,没有任何一个知道如何使用它? 我的意思是如何自动或手动(大整数)操纵私有/公共密钥。

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).

修改

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));

我AccessViolationException这个消息:尝试读取或写入受保护的内存这通常表明其他内存已损坏。

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

和我的事情缺少的东西,要配置RSA密钥没有?

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

推荐答案

如果您是从的这个项目

您可以看看测试套件此包装。

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

有一个测试RSA加密/解密,你可以发现它<一个href="http://openssl-net.git.sourceforge.net/git/gitweb.cgi?p=openssl-net/openssl-net;a=blob;f=test/TestRSA.cs;hb=HEAD"相对=nofollow>此处。刚看完密押方法,它应该很容易使用图书馆没有任何问题的情况下,任何请让我知道。

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.

更新

要生成和保存键:

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);

要由文件创建RSA类:

To create RSA class from file:

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

斌是生物类的实例,并应包含要解密/加密文本。样品$ C $从注释中提到的控制台应用程序读取文件c:

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是的PasswordHandler委托有特色的一个实例:

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.

这篇关于OpenSSL的RSA例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆