2004-05-06 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / corlib / System.Security.Cryptography / RSAPKCS1KeyExchangeFormatter.cs
1 //
2 // RSAPKCS1KeyExchangeFormatter.cs: Handles PKCS#1 v.1.5 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 System.Globalization;
13 using Mono.Security.Cryptography;
14
15 namespace System.Security.Cryptography {
16         
17         // LAMESPEC: There seems no way to select a hash algorithm. The default 
18         // algorithm, is SHA1 because the class use the PKCS1MaskGenerationMethod -
19         // which default to SHA1.
20         public class RSAPKCS1KeyExchangeFormatter: AsymmetricKeyExchangeFormatter
21         {
22                 private RSA rsa;
23                 private RandomNumberGenerator random;
24         
25                 public RSAPKCS1KeyExchangeFormatter ()
26                 {
27                 }
28         
29                 public RSAPKCS1KeyExchangeFormatter (AsymmetricAlgorithm key)
30                 {
31                         SetKey (key);
32                 }
33         
34                 public RandomNumberGenerator Rng 
35                 {
36                         get { return random; }
37                         set { random = value; }
38                 }
39         
40                 public override string Parameters 
41                 {
42                         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\" />"; }
43                 }
44         
45                 public override byte[] CreateKeyExchange (byte[] rgbData)
46                 {
47                         if (random == null)
48                                 random = RandomNumberGenerator.Create ();  // create default
49                         return PKCS1.Encrypt_v15 (rsa, random, rgbData);
50                 }
51         
52                 public override byte[] CreateKeyExchange (byte[] rgbData, Type symAlgType)
53                 {
54                         // documentation says that symAlgType is not used !?!
55                         return CreateKeyExchange (rgbData);
56                 }
57         
58                 public override void SetKey (AsymmetricAlgorithm key)
59                 {
60                         rsa = (RSA) key;
61                 }
62         }
63 }