RsaSecretBcpgKey.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Bcpg
  7. {
  8. /// <remarks>Base class for an RSA secret (or priate) key.</remarks>
  9. public class RsaSecretBcpgKey
  10. : BcpgObject, IBcpgKey
  11. {
  12. private readonly MPInteger d, p, q, u;
  13. private readonly BigInteger expP, expQ, crt;
  14. public RsaSecretBcpgKey(
  15. BcpgInputStream bcpgIn)
  16. {
  17. this.d = new MPInteger(bcpgIn);
  18. this.p = new MPInteger(bcpgIn);
  19. this.q = new MPInteger(bcpgIn);
  20. this.u = new MPInteger(bcpgIn);
  21. this.expP = d.Value.Remainder(p.Value.Subtract(BigInteger.One));
  22. this.expQ = d.Value.Remainder(q.Value.Subtract(BigInteger.One));
  23. this.crt = BigIntegers.ModOddInverse(p.Value, q.Value);
  24. }
  25. public RsaSecretBcpgKey(
  26. BigInteger d,
  27. BigInteger p,
  28. BigInteger q)
  29. {
  30. // PGP requires (p < q)
  31. int cmp = p.CompareTo(q);
  32. if (cmp >= 0)
  33. {
  34. if (cmp == 0)
  35. throw new ArgumentException("p and q cannot be equal");
  36. BigInteger tmp = p;
  37. p = q;
  38. q = tmp;
  39. }
  40. this.d = new MPInteger(d);
  41. this.p = new MPInteger(p);
  42. this.q = new MPInteger(q);
  43. this.u = new MPInteger(BigIntegers.ModOddInverse(q, p));
  44. this.expP = d.Remainder(p.Subtract(BigInteger.One));
  45. this.expQ = d.Remainder(q.Subtract(BigInteger.One));
  46. this.crt = BigIntegers.ModOddInverse(p, q);
  47. }
  48. public BigInteger Modulus
  49. {
  50. get { return p.Value.Multiply(q.Value); }
  51. }
  52. public BigInteger PrivateExponent
  53. {
  54. get { return d.Value; }
  55. }
  56. public BigInteger PrimeP
  57. {
  58. get { return p.Value; }
  59. }
  60. public BigInteger PrimeQ
  61. {
  62. get { return q.Value; }
  63. }
  64. public BigInteger PrimeExponentP
  65. {
  66. get { return expP; }
  67. }
  68. public BigInteger PrimeExponentQ
  69. {
  70. get { return expQ; }
  71. }
  72. public BigInteger CrtCoefficient
  73. {
  74. get { return crt; }
  75. }
  76. /// <summary>The format, as a string, always "PGP".</summary>
  77. public string Format
  78. {
  79. get { return "PGP"; }
  80. }
  81. /// <summary>Return the standard PGP encoding of the key.</summary>
  82. public override byte[] GetEncoded()
  83. {
  84. try
  85. {
  86. return base.GetEncoded();
  87. }
  88. catch (Exception)
  89. {
  90. return null;
  91. }
  92. }
  93. public override void Encode(
  94. BcpgOutputStream bcpgOut)
  95. {
  96. bcpgOut.WriteObjects(d, p, q, u);
  97. }
  98. }
  99. }
  100. #pragma warning restore
  101. #endif