【RSA加密】PEM格式密钥转换成XML格式(方法二)
Nuget首先需要添加引用:Portable.BouncyCastle
注意:密钥的开头和结尾的字符,如:-----BEGIN PUBLIC KEY-----,一定要单独一行,不然会报错:未将对象引用设置到对象的实例
1 // GET: Test 2 public ActionResult Index() 3 { 4 //pem格式公钥 5 string publicKey = @"-----BEGIN PUBLIC KEY----- 6 MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCN18bgncHY9Xq26vd9o7L9mSOH 7 RBsMRem7ar3QGnfoQNYfNGTAYZrF26JvdRaFUy9RKItzYd8O2wXLib5Bc9yxZyoA 8 qXuzMMY8ldBT+6fHSONYHFYKjVqrIY3YUQwuiff4LBPf/hD4XXkKqzY2o7WEQVHv 9 dhsZZFbxnbCu6Ppi5wIDAQAB 10 -----END PUBLIC KEY-----"; 11 12 //pem格式私钥 13 string privateKey = @"-----BEGIN RSA PRIVATE KEY----- 14 MIICXAIBAAKBgQCjTtLZqou0r+vLvqVejf/90IknC0PLTwXFXUY+4QljjqNcNyb2 15 FHr2aTEl4mb+yOtHYRjx7HlHnWEbvNxvek4n56B2njhgjGQ1rhg2Z840vLgnmH3b 16 7GwVsapM5XXZPQh24KBmh9nbhj6R6hLzE+7LAOjAZalUB/C+Uu7ewlgJUwIDAQAB 17 AoGABGK5ht7toYyX6IDWMBPgLBDNnh7PU0ZsAUDgAhBJEENkX7WbdkBWjQeGKl5K 18 Jc1/hAs3gEe5ehLf6L7JrLS8JIsSTEODB9Zvq3bQmyr8jbqFKNihYsSmcpuleYmi 19 U3UkOId3IFMiRrdr4q9oOsHR+PPn/v5sJFEjyu/RVDe9swECQQDxDMRkHHl6r8zz 20 yDtPVOLZxQxlixvTd5TaoM1aP8FgGsVPTuGzUQaYSEJYJ0PfnugMPGGz7gAhzK/5 21 DGTm8uI/AkEArW+4XR7oBPnza8G6krI76YLoUgqxQYFpQz5Ffdls4ssfk7Ihl6Wn 22 TQB6JUdL/0Ku5pJZGyqja/FtPoAkwYQr7QJBAJFNYeD67RtQd5oH/ACBKdGdnzBi 23 n15kef0j60VYoydcW83HvfOKhxAqlptu59IXhsdBap+8eqXlLbpRh01T4Z8CQAF+ 24 GKQUhAKm9AVVYXmptwJHciBinyvlJTbC7m4q1NkOznLC8a8QaeQrwOFZP7oupLqv 25 P70QHN/s7/e9upM/w/ECQBqf7ruwMc5+T8/BfsUQwHhvnibG7eEutAE0y1fFvZCD 26 dAhQ7l3g5VKbbvztdg+kRKObpAz4JgOXXnqufNFHvHY= 27 -----END RSA PRIVATE KEY-----"; 28 29 //xml格式公钥 30 string publicKeyXml = PemToXml(publicKey, false); 31 //xml格式私钥 32 string privateKeyXml = PemToXml(privateKey, true); 33 36 return View(); 37 } 38 39 /// <summary> 40 /// Pem格式密钥转换成Xml格式密钥 41 /// </summary> 42 /// <param name="pemKey">Pem格式密钥(公钥或私钥)</param> 43 /// <param name="isPrivateKey">是否是私钥</param> 44 /// <returns>转换后的Xml格式密钥(公钥或私钥)</returns> 45 public static string PemToXml(string pemKey, bool isPrivateKey) 46 { 47 string rsaKey = string.Empty; 48 object pemObject = null; 49 RSAParameters rsaPara = new RSAParameters(); 50 using (StringReader sReader = new StringReader(pemKey)) 51 { 52 var pemReader = new Org.BouncyCastle.OpenSsl.PemReader(sReader); 53 pemObject = pemReader.ReadObject(); 54 } 55 //私钥 56 if (isPrivateKey) 57 { 58 RsaPrivateCrtKeyParameters key = (RsaPrivateCrtKeyParameters)((AsymmetricCipherKeyPair)pemObject).Private; 59 rsaPara = new RSAParameters 60 { 61 Modulus = key.Modulus.ToByteArrayUnsigned(), 62 Exponent = key.PublicExponent.ToByteArrayUnsigned(), 63 D = key.Exponent.ToByteArrayUnsigned(), 64 P = key.P.ToByteArrayUnsigned(), 65 Q = key.Q.ToByteArrayUnsigned(), 66 DP = key.DP.ToByteArrayUnsigned(), 67 DQ = key.DQ.ToByteArrayUnsigned(), 68 InverseQ = key.QInv.ToByteArrayUnsigned(), 69 }; 70 } 71 //公钥 72 else 73 { 74 RsaKeyParameters key = (RsaKeyParameters)pemObject; 75 rsaPara = new RSAParameters 76 { 77 Modulus = key.Modulus.ToByteArrayUnsigned(), 78 Exponent = key.Exponent.ToByteArrayUnsigned(), 79 }; 80 } 81 RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); 82 rsa.ImportParameters(rsaPara); 83 using (StringWriter sw = new StringWriter()) 84 { 85 sw.Write(rsa.ToXmlString(isPrivateKey ? true : false)); 86 rsaKey = sw.ToString(); 87 } 88 return rsaKey; 89 }