RsaKey.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. /// <summary>
  113. /// Gets the length of the key.
  114. /// </summary>
  115. /// <value>
  116. /// The length of the key.
  117. /// </value>
  118. public override int KeyLength
  119. {
  120. get
  121. {
  122. return this.Modulus.BitLength;
  123. }
  124. }
  125. private RsaDigitalSignature _digitalSignature;
  126. /// <summary>
  127. /// Gets the digital signature.
  128. /// </summary>
  129. protected override DigitalSignature DigitalSignature
  130. {
  131. get
  132. {
  133. if (this._digitalSignature == null)
  134. {
  135. this._digitalSignature = new RsaDigitalSignature(this);
  136. }
  137. return this._digitalSignature;
  138. }
  139. }
  140. /// <summary>
  141. /// Gets or sets the public.
  142. /// </summary>
  143. /// <value>
  144. /// The public.
  145. /// </value>
  146. public override BigInteger[] Public
  147. {
  148. get
  149. {
  150. return new BigInteger[] { this.Exponent, this.Modulus };
  151. }
  152. set
  153. {
  154. if (value.Length != 2)
  155. throw new InvalidOperationException("Invalid private key.");
  156. this._privateKey = new BigInteger[] { value[1], value[0] };
  157. }
  158. }
  159. /// <summary>
  160. /// Initializes a new instance of the <see cref="RsaKey"/> class.
  161. /// </summary>
  162. public RsaKey()
  163. {
  164. }
  165. /// <summary>
  166. /// Initializes a new instance of the <see cref="RsaKey"/> class.
  167. /// </summary>
  168. /// <param name="data">DER encoded private key data.</param>
  169. public RsaKey(byte[] data)
  170. : base(data)
  171. {
  172. if (this._privateKey.Length != 8)
  173. throw new InvalidOperationException("Invalid private key.");
  174. }
  175. #region IDisposable Members
  176. private bool _isDisposed = false;
  177. /// <summary>
  178. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged ResourceMessages.
  179. /// </summary>
  180. public void Dispose()
  181. {
  182. Dispose(true);
  183. GC.SuppressFinalize(this);
  184. }
  185. /// <summary>
  186. /// Releases unmanaged and - optionally - managed resources
  187. /// </summary>
  188. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged ResourceMessages.</param>
  189. protected virtual void Dispose(bool disposing)
  190. {
  191. // Check to see if Dispose has already been called.
  192. if (!this._isDisposed)
  193. {
  194. // If disposing equals true, dispose all managed
  195. // and unmanaged ResourceMessages.
  196. if (disposing)
  197. {
  198. // Dispose managed ResourceMessages.
  199. if (this._digitalSignature != null)
  200. {
  201. this._digitalSignature.Dispose();
  202. this._digitalSignature = null;
  203. }
  204. }
  205. // Note disposing has been done.
  206. this._isDisposed = true;
  207. }
  208. }
  209. /// <summary>
  210. /// Releases unmanaged resources and performs other cleanup operations before the
  211. /// <see cref="SshCommand"/> is reclaimed by garbage collection.
  212. /// </summary>
  213. ~RsaKey()
  214. {
  215. // Do not re-create Dispose clean-up code here.
  216. // Calling Dispose(false) is optimal in terms of
  217. // readability and maintainability.
  218. Dispose(false);
  219. }
  220. #endregion
  221. }
  222. }