为公钥 RSA 加密 C# .net 创建 .pem 文件 [英] Create .pem file for public key RSA encryption C# .net

查看:238
本文介绍了为公钥 RSA 加密 C# .net 创建 .pem 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为这个方法生成的公钥创建.pem文件

I want to create .pem file for the public key generated by this method

public static Tuple<string, string> CreateKeyPair()
{
    CspParameters cspParams = 
        new CspParameters { 
            ProviderType = 1 /* PROV_RSA_FULL */ 
        };

    RSACryptoServiceProvider rsaProvider = 
        new RSACryptoServiceProvider(1024, cspParams);

    string publicKey = Convert.ToBase64String(rsaProvider.ExportCspBlob(false));
    string privateKey = Convert.ToBase64String(rsaProvider.ExportCspBlob(true));

    return new Tuple<string, string>(privateKey, publicKey);
}

因为我是为移动应用程序生成这个密钥而他们无法读取它,所以他们请求 .pem 文件而不是公钥作为字符串

Because I was generating this key for mobile application and they can not read it, they requested .pem file instead of public key as string

请多多指教,

推荐答案

首先,所谓的 .pem 文件实际上并不是一个固定的规范或格式.几种不同类型的不同文件格式通常被描述为PEM"文件.当 SSLeay(现在是 OpenSSL)项目需要生成包含关键信息的 base64 编码输出文件时,他们从旧的隐私增强邮件 RFC 中借用了格式概念 1421-1424 并将扩展名 .pem 添加到这些文件的末尾.但是这样的文件可能包含公钥、私钥、证书请求、证书、证书列表等.每个都是不同的.因此,如果您只被告知生成一个 .pem 文件,您将不得不猜测真正需要什么.

First off, a so-called .pem file is not really a fixed specification or format. Several different kinds of distinct file formats are generally described as "PEM" files. When the SSLeay (now OpenSSL) project needed to produce a base64 encoded output file containing key information they borrowed formatting concepts from the old Privacy-Enhanced Mail RFCs 1421-1424 and they added the extension .pem to the end of these files. But such a file may contain public keys, private keys, certificate requests, certificates, certificate lists, and so on. Each is different. So if all you're told is to produce a .pem file you're going to have to guess what's really needed.

编写此类文件的最简单方法是使用 Bouncycastle C# 库.包 Org.BouncyCastle.OpenSsl 包含许多实用程序,包括可以帮助您的 PemWriter 类.

The easiest way to write such files is to use the Bouncycastle C# library. The package Org.BouncyCastle.OpenSsl contains a number of utilities including a PemWriter class that should help you.

这篇关于为公钥 RSA 加密 C# .net 创建 .pem 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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