如何在 C# 中将 xml 格式的密钥对更改为 PEM 格式?
我尝试将创建的 xml 格式的密钥对更改为 PEM 格式。 这是我的代码。
CspParameters cspParams = new CspParameters();
cspParams.ProviderType = 1;
cspParams.Flags = CspProviderFlags.UseArchivableKey;
cspParams.KeyNumber = (int)KeyNumber.Exchange;
rsaProvider = new RSACryptoServiceProvider(1024,cspParams);
RSAParameters rsa_params = rsaProvider.ExportParameters(true);
byte[] rsa_export = rsaProvider.ExportCspBlob(true);
//here I've try to get private key data
string data__ = Convert.ToBase64String(rsa_export);
FileStream fs = new FileStream(privateKeyFileName, FileMode.CreateNew);
string type = "RSA PRIVATE KEY";
string header = String.Format("-----BEGIN {0}-----\n", type);
string footer = String.Format("\n-----END {0}-----", type);
data__ = header+data__+footer;
byte[] d_ = Encoding.Default.GetBytes(data__);
//write to file
fs.Write(d_, 0, d_.Length);
fs.Close();
运行它后,我得到了一个私钥,但是当我使用该密钥进行测试并使用命令创建证书时:
openssl req -new -x509 -key privatekey.pvk -config
"C:\AppServ\php5\extras\openssl\openssl.cnf" -out myCert.cer -days 365
发生错误
unable to load Private Key
3004:error:0906D064:PEM routines:PEM_read_bio:bad base64 decode:.\crypto\pem\pem_lib.c:756:
I've try to change created key pair in xml format to PEM format.
Here is my code.
CspParameters cspParams = new CspParameters();
cspParams.ProviderType = 1;
cspParams.Flags = CspProviderFlags.UseArchivableKey;
cspParams.KeyNumber = (int)KeyNumber.Exchange;
rsaProvider = new RSACryptoServiceProvider(1024,cspParams);
RSAParameters rsa_params = rsaProvider.ExportParameters(true);
byte[] rsa_export = rsaProvider.ExportCspBlob(true);
//here I've try to get private key data
string data__ = Convert.ToBase64String(rsa_export);
FileStream fs = new FileStream(privateKeyFileName, FileMode.CreateNew);
string type = "RSA PRIVATE KEY";
string header = String.Format("-----BEGIN {0}-----\n", type);
string footer = String.Format("\n-----END {0}-----", type);
data__ = header+data__+footer;
byte[] d_ = Encoding.Default.GetBytes(data__);
//write to file
fs.Write(d_, 0, d_.Length);
fs.Close();
After I've run it I've got a private key but when I test by using the key to create a certificate using command:
openssl req -new -x509 -key privatekey.pvk -config
"C:\AppServ\php5\extras\openssl\openssl.cnf" -out myCert.cer -days 365
an error occured
unable to load Private Key
3004:error:0906D064:PEM routines:PEM_read_bio:bad base64 decode:.\crypto\pem\pem_lib.c:756:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看一下这篇博客文章: http://pstaev.blogspot.com.es/2010/08/convert-rsa-public-key-from-xml-to-pem.html
Take a look at this blog post: http://pstaev.blogspot.com.es/2010/08/convert-rsa-public-key-from-xml-to-pem.html
请记住,openssl 对 PEM 证书格式很挑剔。
-----开始证书-----
在单独的行上(即必须以换行符结束)。
-----结束证书-----
并且也以换行符终止。
因此,就您的情况而言,您似乎没有在 64 个字符处换行“乱码”,并且您的 END 标记缺少换行符。
对于那些不编写自己的密钥对的人,您可以采取以下几个步骤在 Linux 上标准化您的证书文件:
# dos2unix cert.pem
如果您使用的是 Windows,请尝试下载 Cygwin,然后你应该能够得到这些转换工具。
Remember that openssl is picky about PEM certificate formatting.
-----BEGIN CERTIFICATE-----
on a separate line (i.e. it must be terminated with a newline).
-----END CERTIFICATE-----
and also be terminated with a newline.
So in your case, it appears you are not line wrapping the "gibberish" at 64 characters, and your END tag is missing the newline.
For others out there not writing their own key pairs, here are a few steps you can take to normalize your certificate files on Linux:
# dos2unix cert.pem
# fold -w 64 cert.pem
If you're on Windows, try downloading Cygwin, and you should be able to get these conversion tools.