This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5k
/
TripleDesImplementation.cs
81 lines (67 loc) · 2.86 KB
/
TripleDesImplementation.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Security.Cryptography;
namespace Internal.Cryptography
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA5350")]
internal sealed partial class TripleDesImplementation : TripleDES
{
private const int BitsPerByte = 8;
private static readonly RandomNumberGenerator s_rng = RandomNumberGenerator.Create();
public override KeySizes[] LegalKeySizes
{
get
{
// CNG does not support 128-bit keys.
// Only support 192-bit keys on all platforms for simplicity.
return new KeySizes[] { new KeySizes(minSize: 3 * 64, maxSize: 3 * 64, skipSize: 0) };
}
}
public override ICryptoTransform CreateDecryptor()
{
return CreateTransform(this.Key, this.IV, encrypting: false);
}
public override ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[] rgbIV)
{
return CreateTransform(rgbKey, rgbIV.CloneByteArray(), encrypting: false);
}
public override ICryptoTransform CreateEncryptor()
{
return CreateTransform(this.Key, this.IV, encrypting: true);
}
public override ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[] rgbIV)
{
return CreateTransform(rgbKey, rgbIV.CloneByteArray(), encrypting: true);
}
public override void GenerateIV()
{
byte[] iv = new byte[BlockSize / BitsPerByte];
s_rng.GetBytes(iv);
IV = iv;
}
public sealed override void GenerateKey()
{
byte[] key = new byte[KeySize / BitsPerByte];
s_rng.GetBytes(key);
Key = key;
}
private ICryptoTransform CreateTransform(byte[] rgbKey, byte[] rgbIV, bool encrypting)
{
// note: rgbIV is guaranteed to be cloned before this method, so no need to clone it again
if (rgbKey == null)
throw new ArgumentNullException(nameof(rgbKey));
long keySize = rgbKey.Length * (long)BitsPerByte;
if (keySize > int.MaxValue || !((int)keySize).IsLegalSize(this.LegalKeySizes))
throw new ArgumentException(SR.Cryptography_InvalidKeySize, nameof(rgbKey));
if (rgbIV != null)
{
long ivSize = rgbIV.Length * (long)BitsPerByte;
if (ivSize != BlockSize)
throw new ArgumentException(SR.Cryptography_InvalidIVSize, nameof(rgbIV));
}
return CreateTransformCore(Mode, Padding, rgbKey, rgbIV, BlockSize / BitsPerByte, encrypting);
}
}
}