RSAPKCS1KeyExchangeFormatter.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. //
  2. // RSAPKCS1KeyExchangeFormatter.cs: Handles PKCS#1 v.1.5 keyex encryption.
  3. //
  4. // Author:
  5. // Sebastien Pouliot ([email protected])
  6. //
  7. // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
  8. //
  9. using System;
  10. using System.Security.Cryptography;
  11. namespace System.Security.Cryptography
  12. {
  13. // LAMESPEC: There seems no way to select a hash algorithm. The default
  14. // algorithm, is SHA1 because the class use the PKCS1MaskGenerationMethod -
  15. // which default to SHA1.
  16. public class RSAPKCS1KeyExchangeFormatter: AsymmetricKeyExchangeFormatter
  17. {
  18. private RSA rsa;
  19. private RandomNumberGenerator random;
  20. public RSAPKCS1KeyExchangeFormatter ()
  21. {
  22. }
  23. public RSAPKCS1KeyExchangeFormatter (AsymmetricAlgorithm key)
  24. {
  25. SetKey (key);
  26. }
  27. public RandomNumberGenerator Rng
  28. {
  29. get { return random; }
  30. set { random = value; }
  31. }
  32. public override string Parameters
  33. {
  34. get { return "<enc:KeyEncryptionMethod enc:Algorithm=\"http://www.microsoft.com/xml/security/algorithm/PKCS1-v1.5-KeyEx\" xmlns:enc=\"http://www.microsoft.com/xml/security/encryption/v1.0\" />"; }
  35. }
  36. public override byte[] CreateKeyExchange (byte[] rgbData)
  37. {
  38. if (rsa == null)
  39. throw new CryptographicException ();
  40. if (random == null)
  41. random = RandomNumberGenerator.Create (); // create default
  42. return PKCS1.Encrypt_v15 (rsa, random, rgbData);
  43. }
  44. public override byte[] CreateKeyExchange (byte[] rgbData, Type symAlgType)
  45. {
  46. // documentation says that symAlgType is not used !?!
  47. // FIXME: must be the same as previous method ?
  48. return CreateKeyExchange (rgbData);
  49. }
  50. public override void SetKey (AsymmetricAlgorithm key)
  51. {
  52. if (key != null) {
  53. if (key is RSA) {
  54. rsa = (RSA)key;
  55. }
  56. else
  57. throw new InvalidCastException ();
  58. }
  59. // here null is accepted!
  60. }
  61. }
  62. }