RSAOAEPKeyExchangeFormatter.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. //
  2. // RSAOAEPKeyExchangeFormatter.cs - Handles OAEP 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. namespace System.Security.Cryptography {
  11. public class RSAOAEPKeyExchangeFormatter : AsymmetricKeyExchangeFormatter {
  12. private RSA rsa;
  13. private RandomNumberGenerator random;
  14. public RSAOAEPKeyExchangeFormatter ()
  15. {
  16. rsa = null;
  17. }
  18. public RSAOAEPKeyExchangeFormatter (AsymmetricAlgorithm key)
  19. {
  20. SetKey (key);
  21. }
  22. public byte[] Parameter {
  23. get { return null; }
  24. set { ; }
  25. }
  26. public override string Parameters {
  27. get { return null; }
  28. }
  29. public RandomNumberGenerator Rng {
  30. get { return random; }
  31. set { random = value; }
  32. }
  33. public override byte[] CreateKeyExchange (byte[] rgbData)
  34. {
  35. if (rsa == null)
  36. throw new CryptographicException ();
  37. if (random == null)
  38. random = RandomNumberGenerator.Create (); // create default
  39. SHA1 sha1 = SHA1.Create ();
  40. return PKCS1.Encrypt_OAEP (rsa, sha1, random, rgbData);
  41. }
  42. public override byte[] CreateKeyExchange (byte[] rgbData, Type symAlgType)
  43. {
  44. // documentation says that symAlgType is not used !?!
  45. // FIXME: must be the same as previous method ?
  46. return CreateKeyExchange (rgbData);
  47. }
  48. public override void SetKey (AsymmetricAlgorithm key)
  49. {
  50. if (key is RSA) {
  51. rsa = (RSA) key;
  52. }
  53. else
  54. throw new CryptographicException ();
  55. }
  56. }
  57. }