OpenSSL RSA 示例

发布于 2024-10-19 12:06:50 字数 513 浏览 1 评论 0原文

我必须在我的 C# 项目中使用 Crypto (OpenSSL),我可以使用所有对称密码和消息摘要,但我不能使用 RSA。请问有人知道怎么用吗? 我的意思是如何自动或手动(使用大整数)操作私钥/公钥。

编辑

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 并显示以下消息:“尝试读取或写入受保护的内存。这通常表明其他内存已损坏。”

我觉得缺少一些东西,无法配置 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).

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北音执念 2024-10-26 12:06:51

如果您使用此项目中的 .Net OpenSSL 包装器。

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

您可以找到一个 RSA 加密/解密测试 此处。只需阅读 TestKey 方法,就可以轻松使用该库,不会出现任何问题,如有任何问题请告诉我。

更新

生成并保存密钥:

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 类:

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

bin 是 BIO 类的实例,应包含您要解密/加密的文本。注释中提到的从控制台应用程序读取文件的示例代码:

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 委托的实例:

public static string OnPassword(bool verify, object arg)

此方法应返回密码(如果有)。

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.

小巷里的女流氓 2024-10-26 12:06:51

您在 Microsoft 网站上检查过这个示例吗?
这是 .Net 中 RSA 的完整文档,最后还有一个示例!它非常容易理解和重用!

更新1
这个代码是用C++编写的,它使用RSA..应该很容易理解,因为它接近C#
如果我还有其他事情,我会对我的答案进行更多更新..我只是想为您发布一个起点,以便您。
更新2
问题有一个未标记为正确答案的答案..不过我认为它可能有用,请看一下!
更新3
检查这个链接作为主要来源和这个子链接 这是第一个链接的一部分,提供有关 RSA 密钥的信息

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

不弃不离 2024-10-26 12:06:51
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 并显示以下消息:“尝试读取或写入受保护的内存。这通常表明其他内存已损坏。”

我觉得缺少一些东西,无法配置 RSA 密钥?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文