RsaKey.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Renci.SshNet.Common;
  6. using Renci.SshNet.Security.Cryptography;
  7. namespace Renci.SshNet.Security
  8. {
  9. /// <summary>
  10. /// Contains RSA private and public key
  11. /// </summary>
  12. public class RsaKey : Key, IDisposable
  13. {
  14. /// <summary>
  15. /// Gets the modulus.
  16. /// </summary>
  17. public BigInteger Modulus
  18. {
  19. get
  20. {
  21. return this._privateKey[0];
  22. }
  23. }
  24. /// <summary>
  25. /// Gets the exponent.
  26. /// </summary>
  27. public BigInteger Exponent
  28. {
  29. get
  30. {
  31. return this._privateKey[1];
  32. }
  33. }
  34. /// <summary>
  35. /// Gets the D.
  36. /// </summary>
  37. public BigInteger D
  38. {
  39. get
  40. {
  41. if (this._privateKey.Length > 2)
  42. return this._privateKey[2];
  43. else
  44. return BigInteger.Zero;
  45. }
  46. }
  47. /// <summary>
  48. /// Gets the P.
  49. /// </summary>
  50. public BigInteger P
  51. {
  52. get
  53. {
  54. if (this._privateKey.Length > 3)
  55. return this._privateKey[3];
  56. else
  57. return BigInteger.Zero;
  58. }
  59. }
  60. /// <summary>
  61. /// Gets the Q.
  62. /// </summary>
  63. public BigInteger Q
  64. {
  65. get
  66. {
  67. if (this._privateKey.Length > 4)
  68. return this._privateKey[4];
  69. else
  70. return BigInteger.Zero;
  71. }
  72. }
  73. /// <summary>
  74. /// Gets the DP.
  75. /// </summary>
  76. public BigInteger DP
  77. {
  78. get
  79. {
  80. if (this._privateKey.Length > 5)
  81. return this._privateKey[5];
  82. else
  83. return BigInteger.Zero;
  84. }
  85. }
  86. /// <summary>
  87. /// Gets the DQ.
  88. /// </summary>
  89. public BigInteger DQ
  90. {
  91. get
  92. {
  93. if (this._privateKey.Length > 6)
  94. return this._privateKey[6];
  95. else
  96. return BigInteger.Zero;
  97. }
  98. }
  99. /// <summary>
  100. /// Gets the inverse Q.
  101. /// </summary>
  102. public BigInteger InverseQ
  103. {
  104. get
  105. {
  106. if (this._privateKey.Length > 7)
  107. return this._privateKey[7];
  108. else
  109. return BigInteger.Zero;
  110. }
  111. }
  112. private RsaDigitalSignature _digitalSignature;
  113. /// <summary>
  114. /// Gets the digital signature.
  115. /// </summary>
  116. protected override DigitalSignature DigitalSignature
  117. {
  118. get
  119. {
  120. if (this._digitalSignature == null)
  121. {
  122. this._digitalSignature = new RsaDigitalSignature(this);
  123. }
  124. return this._digitalSignature;
  125. }
  126. }
  127. /// <summary>
  128. /// Gets or sets the public.
  129. /// </summary>
  130. /// <value>
  131. /// The public.
  132. /// </value>
  133. public override BigInteger[] Public
  134. {
  135. get
  136. {
  137. return new BigInteger[] { this.Exponent, this.Modulus };
  138. }
  139. set
  140. {
  141. if (value.Length != 2)
  142. throw new InvalidOperationException("Invalid private key.");
  143. this._privateKey = new BigInteger[] { value[1], value[0] };
  144. }
  145. }
  146. /// <summary>
  147. /// Initializes a new instance of the <see cref="RsaKey"/> class.
  148. /// </summary>
  149. public RsaKey()
  150. {
  151. }
  152. /// <summary>
  153. /// Initializes a new instance of the <see cref="RsaKey"/> class.
  154. /// </summary>
  155. /// <param name="data">DER encoded private key data.</param>
  156. public RsaKey(byte[] data)
  157. : base(data)
  158. {
  159. if (this._privateKey.Length != 8)
  160. throw new InvalidOperationException("Invalid private key.");
  161. }
  162. #region IDisposable Members
  163. private bool _isDisposed = false;
  164. /// <summary>
  165. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged ResourceMessages.
  166. /// </summary>
  167. public void Dispose()
  168. {
  169. Dispose(true);
  170. GC.SuppressFinalize(this);
  171. }
  172. /// <summary>
  173. /// Releases unmanaged and - optionally - managed resources
  174. /// </summary>
  175. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged ResourceMessages.</param>
  176. protected virtual void Dispose(bool disposing)
  177. {
  178. // Check to see if Dispose has already been called.
  179. if (!this._isDisposed)
  180. {
  181. // If disposing equals true, dispose all managed
  182. // and unmanaged ResourceMessages.
  183. if (disposing)
  184. {
  185. // Dispose managed ResourceMessages.
  186. if (this._digitalSignature != null)
  187. {
  188. this._digitalSignature.Dispose();
  189. this._digitalSignature = null;
  190. }
  191. }
  192. // Note disposing has been done.
  193. this._isDisposed = true;
  194. }
  195. }
  196. /// <summary>
  197. /// Releases unmanaged resources and performs other cleanup operations before the
  198. /// <see cref="SshCommand"/> is reclaimed by garbage collection.
  199. /// </summary>
  200. ~RsaKey()
  201. {
  202. // Do not re-create Dispose clean-up code here.
  203. // Calling Dispose(false) is optimal in terms of
  204. // readability and maintainability.
  205. Dispose(false);
  206. }
  207. #endregion
  208. }
  209. }