lalala
[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 (spouliot@motus.com)
6 //
7 // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
8 //
9
10 using System;
11 using Mono.Security.Cryptography;
12
13 namespace System.Security.Cryptography {
14         
15         // LAMESPEC: There seems no way to select a hash algorithm. The default 
16         // algorithm, is SHA1 because the class use the PKCS1MaskGenerationMethod -
17         // which default to SHA1.
18         public class RSAPKCS1KeyExchangeFormatter: AsymmetricKeyExchangeFormatter
19         {
20                 private RSA rsa;
21                 private RandomNumberGenerator random;
22         
23                 public RSAPKCS1KeyExchangeFormatter ()
24                 {
25                 }
26         
27                 public RSAPKCS1KeyExchangeFormatter (AsymmetricAlgorithm key)
28                 {
29                         SetKey (key);
30                 }
31         
32                 public RandomNumberGenerator Rng 
33                 {
34                         get { return random; }
35                         set { random = value; }
36                 }
37         
38                 public override string Parameters 
39                 {
40                         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\" />"; }
41                 }
42         
43                 public override byte[] CreateKeyExchange (byte[] rgbData)
44                 {
45                         if (rsa == null)
46                                 throw new CryptographicException ();
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                         // FIXME: must be the same as previous method ?
56                         return CreateKeyExchange (rgbData);
57                 }
58         
59                 public override void SetKey (AsymmetricAlgorithm key)
60                 {
61                         if (key != null) {
62                                 if (key is RSA) {
63                                         rsa = (RSA)key;
64                                 }
65                                 else
66                                         throw new InvalidCastException ();
67                         }
68                         // here null is accepted!
69                 }
70         }
71 }