1 using System.Diagnostics.Contracts;
4 // Copyright (c) Microsoft Corporation. All rights reserved.
7 // <OWNER>[....]</OWNER>
14 namespace System.Security.Cryptography {
15 [System.Runtime.InteropServices.ComVisible(true)]
16 public abstract class TripleDES : SymmetricAlgorithm
18 private static KeySizes[] s_legalBlockSizes = {
19 new KeySizes(64, 64, 0)
22 private static KeySizes[] s_legalKeySizes = {
23 new KeySizes(2*64, 3*64, 64)
27 // protected constructors
30 protected TripleDES() {
33 FeedbackSizeValue = BlockSizeValue;
34 LegalBlockSizesValue = s_legalBlockSizes;
35 LegalKeySizesValue = s_legalKeySizes;
42 public override byte[] Key {
44 if (KeyValue == null) {
45 // Never hand back a weak key
48 } while (IsWeakKey(KeyValue));
50 return (byte[]) KeyValue.Clone();
53 if (value == null) throw new ArgumentNullException("value");
54 Contract.EndContractBlock();
55 if (!ValidKeySize(value.Length * 8)) { // must convert bytes to bits
56 throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidKeySize"));
58 if (IsWeakKey(value)) {
59 throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidKey_Weak"),"TripleDES");
61 KeyValue = (byte[]) value.Clone();
62 KeySizeValue = value.Length * 8;
70 new static public TripleDES Create() {
71 return Create("System.Security.Cryptography.TripleDES");
74 new static public TripleDES Create(String str) {
75 return (TripleDES) CryptoConfig.CreateFromName(str);
78 public static bool IsWeakKey(byte[] rgbKey) {
79 // All we have to check for here is (a) we're in 3-key mode (192 bits), and
80 // (b) either K1 == K2 or K2 == K3
81 if (!IsLegalKeySize(rgbKey)) {
82 throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidKeySize"));
84 byte[] rgbOddParityKey = Utils.FixupKeyParity(rgbKey);
85 if (EqualBytes(rgbOddParityKey,0,8,8)) return(true);
86 if ((rgbOddParityKey.Length == 24) && EqualBytes(rgbOddParityKey,8,16,8)) return(true);
94 private static bool EqualBytes(byte[] rgbKey, int start1, int start2, int count) {
95 if (start1 < 0) throw new ArgumentOutOfRangeException("start1", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
96 if (start2 < 0) throw new ArgumentOutOfRangeException("start2", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
97 if ((start1+count) > rgbKey.Length) throw new ArgumentException(Environment.GetResourceString("Argument_InvalidValue"));
98 if ((start2+count) > rgbKey.Length) throw new ArgumentException(Environment.GetResourceString("Argument_InvalidValue"));
99 Contract.EndContractBlock();
100 for (int i = 0; i < count; i++) {
101 if (rgbKey[start1+i] != rgbKey[start2+i]) return(false);
106 private static bool IsLegalKeySize(byte[] rgbKey) {
107 if (rgbKey != null && ((rgbKey.Length == 16) || (rgbKey.Length == 24)))