Initial commit
[mono.git] / mcs / class / referencesource / mscorlib / system / security / cryptography / rc2.cs
1 // ==++==
2 // 
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 // 
5 // ==--==
6 // <OWNER>[....]</OWNER>
7 // 
8
9 //
10 // RC2.cs
11 //
12
13 namespace System.Security.Cryptography {
14 [System.Runtime.InteropServices.ComVisible(true)]
15     public abstract class RC2 : SymmetricAlgorithm
16     {
17         protected int               EffectiveKeySizeValue;
18         private static  KeySizes[] s_legalBlockSizes = {
19           new KeySizes(64, 64, 0)
20         };
21         private static  KeySizes[] s_legalKeySizes = {
22             new KeySizes(40, 1024, 8)  // 1024 bits is theoretical max according to the RFC
23         };
24       
25         //
26         // protected constructors
27         //
28     
29         protected RC2() {
30             KeySizeValue = 128;
31             BlockSizeValue = 64;
32             FeedbackSizeValue = BlockSizeValue;
33             LegalBlockSizesValue = s_legalBlockSizes;
34             LegalKeySizesValue = s_legalKeySizes;
35         }
36     
37         //
38         // public properties
39         //
40
41         public virtual int EffectiveKeySize {
42             get {
43                 if (EffectiveKeySizeValue == 0) return KeySizeValue;
44                 return EffectiveKeySizeValue;
45             }
46             set {
47                 if (value > KeySizeValue) {
48                     throw new CryptographicException(Environment.GetResourceString("Cryptography_RC2_EKSKS"));
49                 } else if (value == 0) {
50                     EffectiveKeySizeValue = value;
51                 } else if (value < 40) {
52                     throw new CryptographicException(Environment.GetResourceString("Cryptography_RC2_EKS40"));
53                 } else {
54                     if (ValidKeySize(value))
55                         EffectiveKeySizeValue = value;
56                     else
57                         throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidKeySize"));
58                 }
59             }
60         }
61
62         public override int KeySize {
63             get { return KeySizeValue; }
64             set { 
65                 if (value < EffectiveKeySizeValue) throw new CryptographicException(Environment.GetResourceString("Cryptography_RC2_EKSKS"));
66                 base.KeySize = value;
67             }
68         }
69         
70         //
71         // public methods
72         //
73
74         new static public RC2 Create() {
75             return Create("System.Security.Cryptography.RC2");
76         }
77
78         new static public RC2 Create(String AlgName) {
79             return (RC2) CryptoConfig.CreateFromName(AlgName);
80         }    
81     }
82 }