.NET使用OpenSSL生成的pem密钥文件
来源:互联网 发布:plm软件占有率 编辑:程序博客网 时间:2024/06/05 18:00
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
0 0
- .NET使用OpenSSL生成的pem密钥文件
- .NET使用OpenSSL生成的pem密钥文件
- .NET使用OpenSSL生成的pem密钥文件(增加size为2048的密钥转换)
- 使用OpenSSL生成证书,Pem文件生成
- java读取OPENSSL生成的DSA的pem文件
- java读取OPENSSL生成的DSA的pem文件
- openssl pem文件的读取
- openssl pem密钥文件rsa加密解密例子
- openssl生成pem
- openssl生成pem
- openssl生成pem数字证书
- openssl生成pem
- openssl生成pem
- OpenSSL中读取PEM文件的问题
- Mac OSX 使用OpenSSL生成RSA公匙、私匙(pem)与DER文件
- 生成mdm的pem文件
- 推送PEM 文件的生成
- openssl pem 生成公钥和私钥及文件
- Decorrelating Semantic Visual Attributes by Resisting the Urge to Share 论文笔记
- 如何讲man帮助手册改成中文的
- JS实现的文本框内容发生改变立马触发事件简介
- 211. Add and Search Word - Data structure design
- android中对u盘以及tf卡插入的广播监听
- .NET使用OpenSSL生成的pem密钥文件
- Spoken Attributes: Mixing Binary and Relative Attributes to Say the Right Thing 论文笔记
- 携程移动端架构演进与优化之路
- Unity3d 跨平台原理
- ExtJS textField 表单验证 常用正则表达式
- Oracle建立表空间和用户
- IntelliJ IDEA 使用心得与常用快捷键
- MSE和PSNR
- 逗号表达式