1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.Diagnostics.Contracts;
namespace System.Security.Cryptography {
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class TripleDES : SymmetricAlgorithm
{
private static KeySizes[] s_legalBlockSizes = {
new KeySizes(64, 64, 0)
};
private static KeySizes[] s_legalKeySizes = {
new KeySizes(2*64, 3*64, 64)
};
//
// protected constructors
//
protected TripleDES() {
KeySizeValue = 3*64;
BlockSizeValue = 64;
FeedbackSizeValue = BlockSizeValue;
LegalBlockSizesValue = s_legalBlockSizes;
LegalKeySizesValue = s_legalKeySizes;
}
//
// public properties
//
public override byte[] Key {
get {
if (KeyValue == null) {
// Never hand back a weak key
do {
GenerateKey();
} while (IsWeakKey(KeyValue));
}
return (byte[]) KeyValue.Clone();
}
set {
if (value == null) throw new ArgumentNullException("value");
Contract.EndContractBlock();
if (!ValidKeySize(value.Length * 8)) { // must convert bytes to bits
throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidKeySize"));
}
if (IsWeakKey(value)) {
throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidKey_Weak"),"TripleDES");
}
KeyValue = (byte[]) value.Clone();
KeySizeValue = value.Length * 8;
}
}
//
// public methods
//
new static public TripleDES Create() {
return Create("System.Security.Cryptography.TripleDES");
}
new static public TripleDES Create(String str) {
return (TripleDES) CryptoConfig.CreateFromName(str);
}
public static bool IsWeakKey(byte[] rgbKey) {
// All we have to check for here is (a) we're in 3-key mode (192 bits), and
// (b) either K1 == K2 or K2 == K3
if (!IsLegalKeySize(rgbKey)) {
throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidKeySize"));
}
byte[] rgbOddParityKey = Utils.FixupKeyParity(rgbKey);
if (EqualBytes(rgbOddParityKey,0,8,8)) return(true);
if ((rgbOddParityKey.Length == 24) && EqualBytes(rgbOddParityKey,8,16,8)) return(true);
return(false);
}
//
// private methods
//
private static bool EqualBytes(byte[] rgbKey, int start1, int start2, int count) {
if (start1 < 0) throw new ArgumentOutOfRangeException("start1", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
if (start2 < 0) throw new ArgumentOutOfRangeException("start2", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
if ((start1+count) > rgbKey.Length) throw new ArgumentException(Environment.GetResourceString("Argument_InvalidValue"));
if ((start2+count) > rgbKey.Length) throw new ArgumentException(Environment.GetResourceString("Argument_InvalidValue"));
Contract.EndContractBlock();
for (int i = 0; i < count; i++) {
if (rgbKey[start1+i] != rgbKey[start2+i]) return(false);
}
return(true);
}
private static bool IsLegalKeySize(byte[] rgbKey) {
if (rgbKey != null && ((rgbKey.Length == 16) || (rgbKey.Length == 24)))
return(true);
return(false);
}
}
}
|