From e8f924196be2b825e1e14f1120bbb26b35db2f90 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Sat, 7 Dec 2013 17:32:45 +0700 Subject: Add methods for converting from BC RSAPrivateKeyStructure --- crypto/src/security/DotNetUtilities.cs | 47 ++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 13 deletions(-) (limited to 'crypto/src') diff --git a/crypto/src/security/DotNetUtilities.cs b/crypto/src/security/DotNetUtilities.cs index 036c0d519..d50e17d39 100644 --- a/crypto/src/security/DotNetUtilities.cs +++ b/crypto/src/security/DotNetUtilities.cs @@ -4,6 +4,7 @@ using System; using System.Security.Cryptography; using SystemX509 = System.Security.Cryptography.X509Certificates; +using Org.BouncyCastle.Asn1.Pkcs; using Org.BouncyCastle.Asn1.X509; using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Crypto.Parameters; @@ -161,22 +162,21 @@ namespace Org.BouncyCastle.Security public static RSA ToRSA(RsaKeyParameters rsaKey) { - RSAParameters rp = ToRSAParameters(rsaKey); - RSACryptoServiceProvider rsaCsp = new RSACryptoServiceProvider(); - // TODO This call appears to not work for private keys (when no CRT info) - rsaCsp.ImportParameters(rp); - return rsaCsp; - } + // TODO This appears to not work for private keys (when no CRT info) + return CreateRSAProvider(ToRSAParameters(rsaKey)); + } public static RSA ToRSA(RsaPrivateCrtKeyParameters privKey) { - RSAParameters rp = ToRSAParameters(privKey); - RSACryptoServiceProvider rsaCsp = new RSACryptoServiceProvider(); - rsaCsp.ImportParameters(rp); - return rsaCsp; - } + return CreateRSAProvider(ToRSAParameters(privKey)); + } - public static RSAParameters ToRSAParameters(RsaKeyParameters rsaKey) + public static RSA ToRSA(RsaPrivateKeyStructure privKey) + { + return CreateRSAProvider(ToRSAParameters(privKey)); + } + + public static RSAParameters ToRSAParameters(RsaKeyParameters rsaKey) { RSAParameters rp = new RSAParameters(); rp.Modulus = rsaKey.Modulus.ToByteArrayUnsigned(); @@ -201,7 +201,21 @@ namespace Org.BouncyCastle.Security return rp; } - // TODO Move functionality to more general class + public static RSAParameters ToRSAParameters(RsaPrivateKeyStructure privKey) + { + RSAParameters rp = new RSAParameters(); + rp.Modulus = privKey.Modulus.ToByteArrayUnsigned(); + rp.Exponent = privKey.PublicExponent.ToByteArrayUnsigned(); + rp.P = privKey.Prime1.ToByteArrayUnsigned(); + rp.Q = privKey.Prime2.ToByteArrayUnsigned(); + rp.D = ConvertRSAParametersField(privKey.PrivateExponent, rp.Modulus.Length); + rp.DP = ConvertRSAParametersField(privKey.Exponent1, rp.P.Length); + rp.DQ = ConvertRSAParametersField(privKey.Exponent2, rp.Q.Length); + rp.InverseQ = ConvertRSAParametersField(privKey.Coefficient, rp.Q.Length); + return rp; + } + + // TODO Move functionality to more general class private static byte[] ConvertRSAParametersField(BigInteger n, int size) { byte[] bs = n.ToByteArrayUnsigned(); @@ -216,6 +230,13 @@ namespace Org.BouncyCastle.Security Array.Copy(bs, 0, padded, size - bs.Length, bs.Length); return padded; } + + private static RSA CreateRSAProvider(RSAParameters rp) + { + RSACryptoServiceProvider rsaCsp = new RSACryptoServiceProvider(); + rsaCsp.ImportParameters(rp); + return rsaCsp; + } } } -- cgit 1.4.1