private static string ExtractRsaKeyInformation(Dictionary <string, object> information) { var modulusKeyPair = information.FirstOrDefault(i => i.Key == Constants.JsonWebKeyParameterNames.RsaKey.ModulusName); var exponentKeyPair = information.FirstOrDefault(i => i.Key == Constants.JsonWebKeyParameterNames.RsaKey.ExponentName); if (modulusKeyPair.Equals(default(KeyValuePair <string, object>)) || exponentKeyPair.Equals(default(KeyValuePair <string, object>))) { throw new InvalidOperationException(ErrorDescriptions.CannotExtractParametersFromJsonWebKey); } var rsaParameters = new RSAParameters { Modulus = modulusKeyPair.Value.ToString().Base64DecodeBytes(), Exponent = exponentKeyPair.Value.ToString().Base64DecodeBytes() }; if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { using (var rsaCryptoServiceProvider = new RSACryptoServiceProvider()) { rsaCryptoServiceProvider.ImportParameters(rsaParameters); return(rsaCryptoServiceProvider.ToXmlStringNetCore(false)); } } else { using (var rsaCryptoServiceProvider = new RSAOpenSsl()) { rsaCryptoServiceProvider.ImportParameters(rsaParameters); return(rsaCryptoServiceProvider.ToXmlStringNetCore(false)); } } }
private static string ExtractRsaKeyInformation(Dictionary <string, object> information) { var modulusKeyPair = information.FirstOrDefault(i => i.Key == Constants.JsonWebKeyParameterNames.RsaKey.ModulusName); var exponentKeyPair = information.FirstOrDefault(i => i.Key == Constants.JsonWebKeyParameterNames.RsaKey.ExponentName); if (modulusKeyPair.Equals(default(KeyValuePair <string, object>)) || exponentKeyPair.Equals(default(KeyValuePair <string, object>))) { throw new InvalidOperationException(ErrorDescriptions.CannotExtractParametersFromJsonWebKey); } var rsaParameters = new RSAParameters { Modulus = modulusKeyPair.Value.ToString().Base64DecodeBytes(), Exponent = exponentKeyPair.Value.ToString().Base64DecodeBytes() }; #if UAP // TODO : Extract RSA Key information ... return(null); #elif NET46 || NET45 using (var rsaCryptoServiceProvider = new RSACryptoServiceProvider()) { rsaCryptoServiceProvider.ImportParameters(rsaParameters); return(rsaCryptoServiceProvider.ToXmlString(false)); } #elif NETSTANDARD using (var rsaCryptoServiceProvider = new RSAOpenSsl()) { rsaCryptoServiceProvider.ImportParameters(rsaParameters); return(rsaCryptoServiceProvider.ToXmlString(false)); } #endif }
public static bool VerifyHash(string publicKey, byte[] data, string signature) { using (var rsa = new RSAOpenSsl()) { rsa.ImportParameters(GetParameters(publicKey)); return(rsa.VerifyData(data, Convert.FromBase64String(signature), HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1)); } }
private static RSA BuildRsaPublicKey(byte[] encodedData) { using (SafeRsaHandle rsaHandle = Interop.Crypto.DecodeRsaPublicKey(encodedData, encodedData.Length)) { Interop.Crypto.CheckValidOpenSslHandle(rsaHandle); RSAParameters rsaParameters = Interop.Crypto.ExportRsaParameters(rsaHandle, false); RSA rsa = new RSAOpenSsl(); rsa.ImportParameters(rsaParameters); return(rsa); } }
private static unsafe RSA BuildRsaPublicKey(byte[] encodedData) { using (SafeRsaHandle rsaHandle = Interop.libcrypto.OpenSslD2I(Interop.libcrypto.d2i_RSAPublicKey, encodedData)) { Interop.libcrypto.CheckValidOpenSslHandle(rsaHandle); RSAParameters rsaParameters = Interop.libcrypto.ExportRsaParameters(rsaHandle, false); RSA rsa = new RSAOpenSsl(); rsa.ImportParameters(rsaParameters); return(rsa); } }
public static void FromXmlStringNetCore(this RSAOpenSsl rsa, string xmlString) { RSAParameters parameters = new RSAParameters(); var xmlDoc = new XmlDocument(); xmlDoc.LoadXml(xmlString); if (xmlDoc.DocumentElement.Name.Equals("RSAKeyValue")) { foreach (XmlNode node in xmlDoc.DocumentElement.ChildNodes) { switch (node.Name) { case "Modulus": parameters.Modulus = Convert.FromBase64String(node.InnerText); break; case "Exponent": parameters.Exponent = Convert.FromBase64String(node.InnerText); break; case "P": parameters.P = Convert.FromBase64String(node.InnerText); break; case "Q": parameters.Q = Convert.FromBase64String(node.InnerText); break; case "DP": parameters.DP = Convert.FromBase64String(node.InnerText); break; case "DQ": parameters.DQ = Convert.FromBase64String(node.InnerText); break; case "InverseQ": parameters.InverseQ = Convert.FromBase64String(node.InnerText); break; case "D": parameters.D = Convert.FromBase64String(node.InnerText); break; } } } else { throw new Exception("Invalid XML RSA key."); } rsa.ImportParameters(parameters); }
/// <summary> /// Parses binary ans.1 RSA private key; returns RSAOpenSsl /// </summary> /// <param name="privateKey"></param> /// <returns></returns> public static RSAOpenSsl DecodeRsaPrivateKey(byte[] privateKey) { // --------- Set up stream to decode the asn.1 encoded RSA private key ------ var memoryStream = new MemoryStream(privateKey); var binaryReader = new BinaryReader(memoryStream); //wrap Memory Stream with BinaryReader for easy reading try { var twoBytes = binaryReader.ReadUInt16(); if (twoBytes == 0x8130) //data read as little endian order (actual data order for Sequence is 30 81) { binaryReader.ReadByte(); //advance 1 byte } else if (twoBytes == 0x8230) { binaryReader.ReadInt16(); //advance 2 bytes } else { return(null); } twoBytes = binaryReader.ReadUInt16(); if (twoBytes != 0x0102) //version number { return(null); } var bt = binaryReader.ReadByte(); if (bt != 0x00) { return(null); } //------ all private key components are Integer sequences ---- var elems = GetIntegerSize(binaryReader); var modulus = binaryReader.ReadBytes(elems); elems = GetIntegerSize(binaryReader); var e = binaryReader.ReadBytes(elems); elems = GetIntegerSize(binaryReader); var d = binaryReader.ReadBytes(elems); elems = GetIntegerSize(binaryReader); var p = binaryReader.ReadBytes(elems); elems = GetIntegerSize(binaryReader); var q = binaryReader.ReadBytes(elems); elems = GetIntegerSize(binaryReader); var dp = binaryReader.ReadBytes(elems); elems = GetIntegerSize(binaryReader); var dq = binaryReader.ReadBytes(elems); elems = GetIntegerSize(binaryReader); var iq = binaryReader.ReadBytes(elems); var rsaParameters = new RSAParameters { Modulus = modulus, Exponent = e, D = d, P = p, Q = q, DP = dp, DQ = dq, InverseQ = iq }; var rsaOpenSsl = new RSAOpenSsl(); rsaOpenSsl.ImportParameters(rsaParameters); return(rsaOpenSsl); } catch (Exception) { return(null); } finally { binaryReader.Close(); } }