2 // TripleDES.cs: Handles TripleDES (abstract class)
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
8 // (C) 2004 Novell (http://www.novell.com)
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using System.Globalization;
36 using System.Security.Cryptography;
38 namespace System.Security.Cryptography {
41 // a. FIPS PUB 46-3: TripleDES
42 // http://csrc.nist.gov/publications/fips/fips46-3/fips46-3.pdf
45 // http://webstore.ansi.org/ansidocstore/product.asp?sku=ANSI+X9%2E52%2D1998
47 public abstract class TripleDES : SymmetricAlgorithm {
51 // from SymmetricAlgorithm
54 FeedbackSizeValue = 64;
56 LegalKeySizesValue = new KeySizes [1];
57 LegalKeySizesValue [0] = new KeySizes (128, 192, 64);
59 LegalBlockSizesValue = new KeySizes [1];
60 LegalBlockSizesValue [0] = new KeySizes (64, 64, 0);
63 public override byte[] Key {
65 if (KeyValue == null) {
66 // generate keys as long as we get weak keys
68 while (IsWeakKey (KeyValue))
71 return (byte[]) KeyValue.Clone ();
75 throw new ArgumentNullException ("Key");
76 // this will check for both key size and weak keys
77 if (IsWeakKey (value))
78 throw new CryptographicException (Locale.GetText ("Weak Key"));
79 KeyValue = (byte[]) value.Clone ();
83 // Triple DES is DES in EDE = Encrypt - Decrypt - Encrypt
85 // EDE = Encrypt (a) - Decrypt (b) - Encrypt (a)
86 // if a == b then TripleDES == DES(a) (hence weak key)
87 // with 3 keys (a,b,c)
88 // EDE = Encrypt (a) - Decrypt (b) - Encrypt (c)
89 // if ( a == b ) then TripleDES == DES(c) (hence weak key)
90 // if ( b == c ) then TripleDES == DES(a) (hence weak key)
91 public static bool IsWeakKey (byte[] rgbKey)
93 // 128 bits (16 bytes) is 3 DES with 2 keys
94 if (rgbKey.Length == 16) {
95 // weak if first half == second half
96 for (int i = 0; i < 8; i++)
97 if (rgbKey [i] != rgbKey [i+8])
100 // 192 bits (24 bytes) is 3 DES with 3 keys
101 else if (rgbKey.Length == 24) {
102 bool bFirstCase = true;
103 // weak if first third == second third
104 for (int i = 0; i < 8; i++) {
105 if (rgbKey [i] != rgbKey [i+8]) {
110 // weak if second third == third third
112 for (int i = 8; i < 16; i++)
113 if (rgbKey [i] != rgbKey [i+8])
118 throw new CryptographicException (Locale.GetText ("Wrong Key Length"));
123 public static new TripleDES Create ()
125 return Create ("System.Security.Cryptography.TripleDES");
128 public static new TripleDES Create (string str)
130 return (TripleDES) CryptoConfig.CreateFromName (str);