Skip to content

Commit

Permalink
Switch to RSACng for .NET 4.6+
Browse files Browse the repository at this point in the history
  • Loading branch information
smithrobs committed Mar 23, 2017
1 parent 6f6a3ed commit 70d409a
Showing 1 changed file with 7 additions and 29 deletions.
36 changes: 7 additions & 29 deletions Nexmo.Api/PemParse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,19 +237,6 @@ public static RSA DecodeRSAPrivateKey(byte[] privkey, bool isPkcs8)
elems = GetIntegerSize(binr);
IQ = binr.ReadBytes(elems);

//Console.WriteLine("showing components ..");
//if (verbose)
//{
// showBytes("\nModulus", MODULUS);
// showBytes("\nExponent", E);
// showBytes("\nD", D);
// showBytes("\nP", P);
// showBytes("\nQ", Q);
// showBytes("\nDP", DP);
// showBytes("\nDQ", DQ);
// showBytes("\nIQ", IQ);
//}

// ------- create RSACryptoServiceProvider instance and initialize with public key -----
#if NETSTANDARD1_6
RSA RSA;
Expand All @@ -262,21 +249,13 @@ public static RSA DecodeRSAPrivateKey(byte[] privkey, bool isPkcs8)
{
RSA = new RSACng();
}
var RSAparams = new RSAParameters
{
Modulus = MODULUS,
Exponent = E,
D = D,
P = P,
Q = Q,
DP = DP,
DQ = DQ,
InverseQ = IQ
};
RSA.ImportParameters(RSAparams);
return RSA;
#else
#elif NET452
// TODO: throwing "Bad Data" exception even though RSACng is fine
var RSA = new RSACryptoServiceProvider();
#else
// 4.6+ introduced CNG
var RSA = new RSACng();
#endif
var RSAparams = new RSAParameters
{
Modulus = MODULUS,
Expand All @@ -290,11 +269,10 @@ public static RSA DecodeRSAPrivateKey(byte[] privkey, bool isPkcs8)
};
RSA.ImportParameters(RSAparams);
return RSA;
#endif
}
catch (Exception ex)
{
Configuration.Instance.AuthenticationLogger.LogError($"DecodeRSAPrivateKey fail: {ex.Message}");
Configuration.Instance.AuthenticationLogger.LogError($"DecodeRSAPrivateKey fail: {ex.Message}, {ex.InnerException?.Message}");
return null;
}
}
Expand Down

0 comments on commit 70d409a

Please sign in to comment.