.NET使用OpenSSL生成的pem密钥文件

    using System;  
    using System.Security.Cryptography;  
      
    /// <summary>  
    /// 作者:http://thinhunan.cnblogs.com  
    /// 名称:RsaUtility.cs  
    /// 时间:2014-11-04 16:48:42  
    /// 描述:将pem格式的1024位或2048位的公钥和私钥转换为RSAParameters  
    /// </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]);  
            var pemPublicExponent = new byte[3];  
            Array.Copy(keyData, (keySize1024 ? 29 : 33), pemModulus, 0, (keySize1024 ? 128 : 256));  
            Array.Copy(keyData, (keySize1024 ? 159 : 291), pemPublicExponent, 0, 3);  
            var para = new RSAParameters { Modulus = pemModulus, Exponent = pemPublicExponent };  
            return para;  
        }  
      
        /// <summary>  
        /// 将pem格式私钥(1024 or 2048)转换为RSAParameters  
        /// </summary>  
        /// <param name="pemFileConent">pem私钥内容</param>  
        /// <returns>转换得到的RSAParamenters</returns>  
        public static RSAParameters ConvertFromPemPrivateKey(string pemFileConent)  
        {  
            if (string.IsNullOrEmpty(pemFileConent))  
            {  
                throw new ArgumentNullException("pemFileConent", "This arg cann't be empty.");  
            }  
            //pemFileConent = pemFileConent.Replace("-----BEGIN RSA PRIVATE KEY-----", "").Replace("-----END RSA PRIVATE KEY-----", "").Replace("\n", "").Replace("\r", "");  
            pemFileConent = pemFileConent.Replace("-----BEGIN PRIVATE KEY-----", "").Replace("-----END PRIVATE KEY-----", "").Replace("\n", "").Replace("\r", "");  
            byte[] keyData = Convert.FromBase64String(pemFileConent);  
            //byte[] keyData = System.Text.Encoding.Default.GetBytes(pemFileConent);  
            bool keySize1024 = (keyData.Length == 609 || keyData.Length == 610);  
            bool keySize2048 = (keyData.Length == 1190 || keyData.Length == 1192);  
      
            if (!(keySize1024 || keySize2048))  
            {  
                throw new ArgumentException("pem file content is incorrect, Only support the key size is 1024 or 2048");  
            }  
      
            int index = (keySize1024 ? 11 : 12);  
            byte[] pemModulus = (keySize1024 ? new byte[128] : new byte[256]);  
            Array.Copy(keyData, index, pemModulus, 0, pemModulus.Length);  
      
            index += pemModulus.Length;  
            index += 2;  
            var pemPublicExponent = new byte[3];  
            Array.Copy(keyData, index, pemPublicExponent, 0, 3);  
      
            index += 3;  
            index += 4;  
            if (keyData[index] == 0)  
            {  
                index++;  
            }  
            byte[] pemPrivateExponent = (keySize1024 ? new byte[128] : new byte[256]);  
            Array.Copy(keyData, index, pemPrivateExponent, 0, pemPrivateExponent.Length);  
      
            index += pemPrivateExponent.Length;  
            index += (keySize1024 ? ((int)keyData[index + 1] == 64 ? 2 : 3) : ((int)keyData[index + 2] == 128 ? 3 : 4));  
            byte[] pemPrime1 = (keySize1024 ? new byte[64] : new byte[128]);  
            Array.Copy(keyData, index, pemPrime1, 0, pemPrime1.Length);  
      
            index += pemPrime1.Length;  
            index += (keySize1024 ? ((int)keyData[index + 1] == 64 ? 2 : 3) : ((int)keyData[index + 2] == 128 ? 3 : 4));  
            byte[] pemPrime2 = (keySize1024 ? new byte[64] : new byte[128]);  
            Array.Copy(keyData, index, pemPrime2, 0, pemPrime2.Length);  
      
            index += pemPrime2.Length;  
            index += (keySize1024 ? ((int)keyData[index + 1] == 64 ? 2 : 3) : ((int)keyData[index + 2] == 128 ? 3 : 4));  
            byte[] pemExponent1 = (keySize1024 ? new byte[64] : new byte[128]);  
            Array.Copy(keyData, index, pemExponent1, 0, pemExponent1.Length);  
      
            index += pemExponent1.Length;  
            index += (keySize1024 ? ((int)keyData[index + 1] == 64 ? 2 : 3) : ((int)keyData[index + 2] == 128 ? 3 : 4));  
            byte[] pemExponent2 = (keySize1024 ? new byte[64] : new byte[128]);  
            Array.Copy(keyData, index, pemExponent2, 0, pemExponent2.Length);  
      
            index += pemExponent2.Length;  
            index += (keySize1024 ? ((int)keyData[index + 1] == 64 ? 2 : 3) : ((int)keyData[index + 2] == 128 ? 3 : 4));  
            byte[] pemCoefficient = (keySize1024 ? new byte[64] : new byte[128]);  
            Array.Copy(keyData, index, pemCoefficient, 0, pemCoefficient.Length);  
      
            var para = new RSAParameters  
                {  
                    Modulus = pemModulus,  
                    Exponent = pemPublicExponent,  
                    D = pemPrivateExponent,  
                    P = pemPrime1,  
                    Q = pemPrime2,  
                    DP = pemExponent1,  
                    DQ = pemExponent2,  
                    InverseQ = pemCoefficient  
                };  
            return para;  
        }  
      
        /// <summary>  
        /// 将pem格式公钥转换为RSAParameters  
        /// </summary>  
        /// <param name="pemFileConent">pem公钥内容</param>  
        /// <returns>转换得到的RSAParamenters</returns>  
        public static RSAParameters ConvertFromPemNormalPublicKey(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);  
            if (keyData.Length < 162)  
            {  
                throw new ArgumentException("pem file content is incorrect.");  
            }  
            byte[] pemModulus = new byte[128];  
            byte[] pemPublicExponent = new byte[3];  
            Array.Copy(keyData, 29, pemModulus, 0, 128);  
            Array.Copy(keyData, 159, pemPublicExponent, 0, 3);  
            RSAParameters para = new RSAParameters();  
            para.Modulus = pemModulus;  
            para.Exponent = pemPublicExponent;  
            return para;  
        }  
      
        /// <summary>  
        /// 将pem格式私钥转换为RSAParameters  
        /// </summary>  
        /// <param name="pemFileConent">pem私钥内容</param>  
        /// <returns>转换得到的RSAParamenters</returns>  
        public static RSAParameters ConvertFromPemNormalPrivateKey(string pemFileConent)  
        {  
            if (string.IsNullOrEmpty(pemFileConent))  
            {  
                throw new ArgumentNullException("pemFileConent", "This arg cann't be empty.");  
            }  
            //pemFileConent = pemFileConent.Replace("-----BEGIN RSA PRIVATE KEY-----", "").Replace("-----END RSA PRIVATE KEY-----", "").Replace("\n", "").Replace("\r", "");  
            pemFileConent = pemFileConent.Replace("-----BEGIN PRIVATE KEY-----", "").Replace("-----END PRIVATE KEY-----", "").Replace("\n", "").Replace("\r", "");  
            byte[] keyData = Convert.FromBase64String(pemFileConent);  
            if (keyData.Length < 609)  
            {  
                throw new ArgumentException("pem file content is incorrect.");  
            }  
      
            int index = 11;  
            byte[] pemModulus = new byte[128];  
            Array.Copy(keyData, index, pemModulus, 0, 128);  
      
            index += 128;  
            index += 2;//141  
            byte[] pemPublicExponent = new byte[3];  
            Array.Copy(keyData, index, pemPublicExponent, 0, 3);  
      
            index += 3;  
            index += 4;//148  
            byte[] pemPrivateExponent = new byte[128];  
            Array.Copy(keyData, index, pemPrivateExponent, 0, 128);  
      
            index += 128;  
            index += ((int)keyData[index + 1] == 64 ? 2 : 3);//279  
            byte[] pemPrime1 = new byte[64];  
            Array.Copy(keyData, index, pemPrime1, 0, 64);  
      
            index += 64;  
            index += ((int)keyData[index + 1] == 64 ? 2 : 3);//346  
            byte[] pemPrime2 = new byte[64];  
            Array.Copy(keyData, index, pemPrime2, 0, 64);  
      
            index += 64;  
            index += ((int)keyData[index + 1] == 64 ? 2 : 3);//412/413  
            byte[] pemExponent1 = new byte[64];  
            Array.Copy(keyData, index, pemExponent1, 0, 64);  
      
            index += 64;  
            index += ((int)keyData[index + 1] == 64 ? 2 : 3);//479/480  
            byte[] pemExponent2 = new byte[64];  
            Array.Copy(keyData, index, pemExponent2, 0, 64);  
      
            index += 64;  
            index += ((int)keyData[index + 1] == 64 ? 2 : 3);//545/546  
            byte[] pemCoefficient = new byte[64];  
            Array.Copy(keyData, index, pemCoefficient, 0, 64);  
      
            RSAParameters para = new RSAParameters();  
            para.Modulus = pemModulus;  
            para.Exponent = pemPublicExponent;  
            para.D = pemPrivateExponent;  
            para.P = pemPrime1;  
            para.Q = pemPrime2;  
            para.DP = pemExponent1;  
            para.DQ = pemExponent2;  
            para.InverseQ = pemCoefficient;  
            return para;  
        }  
    }  
转自:ttp://www.2cto.com/kf/201202/121307.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值