.NET使用OpenSSL生成的pem密钥文件(增加size为2048的密钥转换)
上篇随笔 .NET使用OpenSSL生成的pem密钥文件【做电子商务的朋友可能需要】http://www.2cto.com/kf/201202/121297.html 的算法只支持1024位的密钥文件导入.NET,今天把2048位的支持加上:
using System;
using System.Text;
using System.Security.Cryptography;
using System.Web;
using System.IO;
namespace Thinhunan.Cnblogs.Com.RSAUtility
{
/// <summary>
/// Author http://thinhunan.cnblogs.com
/// </summary>
public class PemConverter
{
/// <summary>
/// 将pem格式公钥(1024 or 2048)转换为RSAParameters
/// </summary>
/// <param name="pemFileConent">pem公钥内容</param>
/// <returns>转换得到的RSAParamenters</returns>
public static RSAParameters ConvertFromPemPublicKey(string pemFileConent)
{
if (string.IsNullOrEmpty(pemFileConent))
{
throw new ArgumentNullException("pemFileConent", "This arg cann't be empty.");
}
pemFileConent = pemFileConent.Replace("-----BEGIN PUBLIC KEY-----", "").Replace("-----END PUBLIC KEY-----", "").Replace("\n", "").Replace("\r", "");
byte[] keyData = Convert.FromBase64String(pemFileConent);
bool keySize1024 = (keyData.Length == 162);
bool keySize2048 = (keyData.Length == 294);
if (!( keySize1024 || keySize2048 ))
{
throw new ArgumentException("pem file content is incorrect, Only support the key size is 1024 or 2048");
}
byte[] pemModulus = (keySize1024? new byte[128] : new byte[256]);
byte[] pemPublicExponent = new byte[3];
相关新闻>>
- 发表评论
-
- 最新评论 更多>>