175841d154406081a5a81eb044076008b304d034
[mono.git] / mcs / class / corlib / System.Security.Cryptography / RSAOAEPKeyExchangeFormatter.cs
1 //
2 // RSAOAEPKeyExchangeFormatter.cs - Handles OAEP keyex encryption.
3 //
4 // Author:
5 //      Sebastien Pouliot <sebastien@ximian.com>
6 //
7 // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
8 // (C) 2004 Novell (http://www.novell.com)
9 //
10
11 using System;
12 using Mono.Security.Cryptography;
13
14 namespace System.Security.Cryptography { 
15
16         public class RSAOAEPKeyExchangeFormatter : AsymmetricKeyExchangeFormatter {
17         
18                 private RSA rsa;
19                 private RandomNumberGenerator random;
20                 private byte[] param;
21         
22                 public RSAOAEPKeyExchangeFormatter () 
23                 {
24                         rsa = null;
25                 }
26         
27                 public RSAOAEPKeyExchangeFormatter (AsymmetricAlgorithm key) 
28                 {
29                         SetKey (key);
30                 }
31         
32                 public byte[] Parameter {
33                         get { return param; }
34                         set { param = value; }
35                 }
36         
37                 public override string Parameters {
38                         get { return null; }
39                 }
40         
41                 public RandomNumberGenerator Rng {
42                         get { return random; }
43                         set { random = value; }
44                 }
45         
46                 public override byte[] CreateKeyExchange (byte[] rgbData) 
47                 {
48                         if (random == null)
49                                 random = RandomNumberGenerator.Create ();  // create default
50         
51                         SHA1 sha1 = SHA1.Create ();
52                         return PKCS1.Encrypt_OAEP (rsa, sha1, random, rgbData);
53                 }
54         
55                 public override byte[] CreateKeyExchange (byte[] rgbData, Type symAlgType) 
56                 {
57                         // documentation says that symAlgType is not used !?!
58                         return CreateKeyExchange (rgbData);
59                 }
60         
61                 public override void SetKey (AsymmetricAlgorithm key) 
62                 {
63                         rsa = (RSA) key;
64                 }
65         }
66 }