.NET使用OpenSSL生成的pem密钥文件
2016-10-18 18:21
691 查看
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
相关文章推荐
- .NET使用OpenSSL生成的pem密钥文件
- .NET使用OpenSSL生成的pem密钥文件
- .NET使用OpenSSL生成的pem密钥文件(增加size为2048的密钥转换)【做电子商务的朋友可能需要】
- .NET使用OpenSSL生成的pem密钥文件[1024位]
- .NET使用OpenSSL生成的pem密钥文件(增加size为2048的密钥转换)
- .NET使用OpenSSL生成的pem密钥文件[1024位]
- .NET使用OpenSSL生成的pem密钥文件【做电子商务的朋友可能需要】
- .NET 使用OpenSSL生成的pem文件做为RSA Key时的读取问题
- 全面解决.Net与Java互通时的RSA加解密问题,使用PEM格式的密钥文件
- Mac OSX 使用OpenSSL生成RSA公匙、私匙(pem)与DER文件
- ActiveMQ SSL应用之二 使用keytool工具生成密钥和证书文件
- 学习使用OpenSSL生成并使用RSA密钥对
- java读取openssl生成的private key文件生成密钥的问题
- 使用openssl手工生成cod文件的SHA1 hash码
- 使用OpenSSL生成CSR文件,并申请全球通用SSL证书
- [转]使用.net 程序生成 Excel 和Access 文件
- 使用openssl生成PEM格式私钥和公钥及ECDSA签名
- Linux 下使用openSSL 生成RSA密钥对
- 使用keytool生成ssl密钥文件keystore和truststore
- java读取OPENSSL生成的DSA的pem文件