BouncyCastle RSAPrivateKey to .NET RSAPrivateKey

前端 未结 5 604
我在风中等你
我在风中等你 2020-12-07 23:27

I\'m creating a certificate distribution system to keep track of clients and stuff.

What happens is:

  • Client send CSR to Server
  • Server checks a
5条回答
  •  借酒劲吻你
    2020-12-08 00:07

    I found it!

    Or atleast part of it :)

    As for the PrivateKey.ExportToParameters(true) Still doens't work but this has something todo with the fact that the key was 2048 bit. Because when I changed it to 1024bit it did work. So if anyone ever finds out why keep me posted.

    So here we go again.

    //BouncyCastle's Key objects
    RsaPrivateCrtKeyParameters rpckp = ((RsaPrivateCrtKeyParameters)ackp.Private);
    
    //.NET RSA Key objects
    System.Security.Cryptography.RSACryptoServiceProvider rcsp = new System.Security.Cryptography.RSACryptoServiceProvider();
    System.Security.Cryptography.RSAParameters parms = new System.Security.Cryptography.RSAParameters();
    
    //So the thing changed is offcourse the ToByteArrayUnsigned() instead of
    //ToByteArray()
    parms.Modulus   = rpckp.Modulus.ToByteArrayUnsigned();
    parms.P         = rpckp.P.ToByteArrayUnsigned();
    parms.Q         = rpckp.Q.ToByteArrayUnsigned();
    parms.DP        = rpckp.DP.ToByteArrayUnsigned();
    parms.DQ        = rpckp.DQ.ToByteArrayUnsigned();
    parms.InverseQ  = rpckp.QInv.ToByteArrayUnsigned();
    parms.D         = rpckp.Exponent.ToByteArrayUnsigned();
    parms.Exponent  = rpckp.PublicExponent.ToByteArrayUnsigned();
    
    //So now this now appears to work.
    rcsp.ImportParameters(parms);
    

    So now I can add the complete Certificate to my store :)

提交回复
热议问题