diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2013-12-07 17:32:45 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2013-12-07 17:32:45 +0700 |
commit | e8f924196be2b825e1e14f1120bbb26b35db2f90 (patch) | |
tree | d237e52ccea59d758949fc4945245737c41dbae3 /crypto/src/security | |
parent | Use Negate() to simplify (diff) | |
download | BouncyCastle.NET-ed25519-e8f924196be2b825e1e14f1120bbb26b35db2f90.tar.xz |
Add methods for converting from BC RSAPrivateKeyStructure
Diffstat (limited to 'crypto/src/security')
-rw-r--r-- | crypto/src/security/DotNetUtilities.cs | 47 |
1 files changed, 34 insertions, 13 deletions
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; + } } } |