/// <summary> /// Create an RSA parameter based on the xml format public key /// </summary> /// <param name="publicKey"></param> /// <returns></returns> protected sealed override RSAParameters CreateRsapFromPublicKey(string publicKey) { publicKey = RsaPemFormatHelper.PublicKeyFormat(publicKey); PemReader pr = new PemReader(new StringReader(publicKey)); var obj = pr.ReadObject(); if (!(obj is RsaKeyParameters rsaKey)) { throw new Exception("Public key format is incorrect"); } var rsap = new RSAParameters(); rsap.Modulus = rsaKey.Modulus.ToByteArrayUnsigned(); rsap.Exponent = rsaKey.Exponent.ToByteArrayUnsigned(); return(rsap); }
/// <summary> /// Public Key Convert pem->xml /// </summary> /// <param name="publicKey"></param> /// <returns></returns> public static string PublicKeyPemToXml(string publicKey) { publicKey = RsaPemFormatHelper.PublicKeyFormat(publicKey); PemReader pr = new PemReader(new StringReader(publicKey)); var obj = pr.ReadObject(); if (!(obj is RsaKeyParameters rsaKey)) { throw new Exception("Public key format is incorrect"); } XElement publicElement = new XElement("RSAKeyValue"); //Modulus XElement pubmodulus = new XElement("Modulus", Convert.ToBase64String(rsaKey.Modulus.ToByteArrayUnsigned())); //Exponent XElement pubexponent = new XElement("Exponent", Convert.ToBase64String(rsaKey.Exponent.ToByteArrayUnsigned())); publicElement.Add(pubmodulus); publicElement.Add(pubexponent); return(publicElement.ToString()); }