Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
TripleDesImplementation.cs
Go to the documentation of this file.
1using System;
4
6
7internal sealed class TripleDesImplementation : TripleDES
8{
10 {
12 }
13
15 {
16 return CreateTransform(Key, IV, encrypting: false);
17 }
18
19 public override ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[] rgbIV)
20 {
21 return CreateTransform(rgbKey, rgbIV.CloneByteArray(), encrypting: false);
22 }
23
25 {
26 return CreateTransform(Key, IV, encrypting: true);
27 }
28
29 public override ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[] rgbIV)
30 {
31 return CreateTransform(rgbKey, rgbIV.CloneByteArray(), encrypting: true);
32 }
33
34 public override void GenerateIV()
35 {
37 }
38
39 public sealed override void GenerateKey()
40 {
42 }
43
44 private ICryptoTransform CreateTransform(byte[] rgbKey, byte[] rgbIV, bool encrypting)
45 {
46 if (rgbKey == null)
47 {
48 throw new ArgumentNullException("rgbKey");
49 }
50 long num = (long)rgbKey.Length * 8L;
51 if (num > int.MaxValue || !((int)num).IsLegalSize(LegalKeySizes))
52 {
54 }
55 if (rgbIV != null)
56 {
57 long num2 = (long)rgbIV.Length * 8L;
58 if (num2 != BlockSize)
59 {
61 }
62 }
63 if (rgbKey.Length == 16)
64 {
65 byte[] array = new byte[24];
66 Array.Copy(rgbKey, array, 16);
67 Array.Copy(rgbKey, 0, array, 16, 8);
68 rgbKey = array;
69 }
70 if (Mode == CipherMode.CFB)
71 {
73 }
74 return CreateTransformCore(Mode, Padding, rgbKey, rgbIV, BlockSize / 8, this.GetPaddingSize(Mode, FeedbackSize), FeedbackSize / 8, encrypting);
75 }
76
77 protected override bool TryDecryptEcbCore(ReadOnlySpan<byte> ciphertext, Span<byte> destination, PaddingMode paddingMode, out int bytesWritten)
78 {
79 UniversalCryptoTransform universalCryptoTransform = CreateTransformCore(CipherMode.ECB, paddingMode, Key, null, BlockSize / 8, BlockSize / 8, 0, encrypting: false);
80 using (universalCryptoTransform)
81 {
82 return universalCryptoTransform.TransformOneShot(ciphertext, destination, out bytesWritten);
83 }
84 }
85
86 protected override bool TryEncryptEcbCore(ReadOnlySpan<byte> plaintext, Span<byte> destination, PaddingMode paddingMode, out int bytesWritten)
87 {
88 UniversalCryptoTransform universalCryptoTransform = CreateTransformCore(CipherMode.ECB, paddingMode, Key, null, BlockSize / 8, BlockSize / 8, 0, encrypting: true);
89 using (universalCryptoTransform)
90 {
91 return universalCryptoTransform.TransformOneShot(plaintext, destination, out bytesWritten);
92 }
93 }
94
95 protected override bool TryEncryptCbcCore(ReadOnlySpan<byte> plaintext, ReadOnlySpan<byte> iv, Span<byte> destination, PaddingMode paddingMode, out int bytesWritten)
96 {
97 UniversalCryptoTransform universalCryptoTransform = CreateTransformCore(CipherMode.CBC, paddingMode, Key, iv.ToArray(), BlockSize / 8, BlockSize / 8, 0, encrypting: true);
98 using (universalCryptoTransform)
99 {
100 return universalCryptoTransform.TransformOneShot(plaintext, destination, out bytesWritten);
101 }
102 }
103
104 protected override bool TryDecryptCbcCore(ReadOnlySpan<byte> ciphertext, ReadOnlySpan<byte> iv, Span<byte> destination, PaddingMode paddingMode, out int bytesWritten)
105 {
106 UniversalCryptoTransform universalCryptoTransform = CreateTransformCore(CipherMode.CBC, paddingMode, Key, iv.ToArray(), BlockSize / 8, BlockSize / 8, 0, encrypting: false);
107 using (universalCryptoTransform)
108 {
109 return universalCryptoTransform.TransformOneShot(ciphertext, destination, out bytesWritten);
110 }
111 }
112
113 protected override bool TryDecryptCfbCore(ReadOnlySpan<byte> ciphertext, ReadOnlySpan<byte> iv, Span<byte> destination, PaddingMode paddingMode, int feedbackSizeInBits, out int bytesWritten)
114 {
115 ValidateCFBFeedbackSize(feedbackSizeInBits);
116 UniversalCryptoTransform universalCryptoTransform = CreateTransformCore(CipherMode.CFB, paddingMode, Key, iv.ToArray(), BlockSize / 8, feedbackSizeInBits / 8, feedbackSizeInBits / 8, encrypting: false);
117 using (universalCryptoTransform)
118 {
119 return universalCryptoTransform.TransformOneShot(ciphertext, destination, out bytesWritten);
120 }
121 }
122
123 protected override bool TryEncryptCfbCore(ReadOnlySpan<byte> plaintext, ReadOnlySpan<byte> iv, Span<byte> destination, PaddingMode paddingMode, int feedbackSizeInBits, out int bytesWritten)
124 {
125 ValidateCFBFeedbackSize(feedbackSizeInBits);
126 UniversalCryptoTransform universalCryptoTransform = CreateTransformCore(CipherMode.CFB, paddingMode, Key, iv.ToArray(), BlockSize / 8, feedbackSizeInBits / 8, feedbackSizeInBits / 8, encrypting: true);
127 using (universalCryptoTransform)
128 {
129 return universalCryptoTransform.TransformOneShot(plaintext, destination, out bytesWritten);
130 }
131 }
132
133 private static void ValidateCFBFeedbackSize(int feedback)
134 {
135 if (feedback != 8 && feedback != 64)
136 {
138 }
139 }
140
141 private static UniversalCryptoTransform CreateTransformCore(CipherMode cipherMode, PaddingMode paddingMode, byte[] key, byte[] iv, int blockSize, int paddingSize, int feedbackSize, bool encrypting)
142 {
143 SafeAlgorithmHandle sharedHandle = TripleDesBCryptModes.GetSharedHandle(cipherMode, feedbackSize);
144 BasicSymmetricCipher cipher = new BasicSymmetricCipherBCrypt(sharedHandle, cipherMode, blockSize, paddingSize, key, ownsParentHandle: false, iv, encrypting);
145 return UniversalCryptoTransform.Create(paddingMode, cipher, encrypting);
146 }
147}
static SafeAlgorithmHandle GetSharedHandle(CipherMode cipherMode, int feedback)
override bool TryDecryptEcbCore(ReadOnlySpan< byte > ciphertext, Span< byte > destination, PaddingMode paddingMode, out int bytesWritten)
override ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[] rgbIV)
static UniversalCryptoTransform CreateTransformCore(CipherMode cipherMode, PaddingMode paddingMode, byte[] key, byte[] iv, int blockSize, int paddingSize, int feedbackSize, bool encrypting)
override bool TryEncryptCbcCore(ReadOnlySpan< byte > plaintext, ReadOnlySpan< byte > iv, Span< byte > destination, PaddingMode paddingMode, out int bytesWritten)
override bool TryDecryptCbcCore(ReadOnlySpan< byte > ciphertext, ReadOnlySpan< byte > iv, Span< byte > destination, PaddingMode paddingMode, out int bytesWritten)
override ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[] rgbIV)
override bool TryEncryptCfbCore(ReadOnlySpan< byte > plaintext, ReadOnlySpan< byte > iv, Span< byte > destination, PaddingMode paddingMode, int feedbackSizeInBits, out int bytesWritten)
ICryptoTransform CreateTransform(byte[] rgbKey, byte[] rgbIV, bool encrypting)
override bool TryDecryptCfbCore(ReadOnlySpan< byte > ciphertext, ReadOnlySpan< byte > iv, Span< byte > destination, PaddingMode paddingMode, int feedbackSizeInBits, out int bytesWritten)
override bool TryEncryptEcbCore(ReadOnlySpan< byte > plaintext, Span< byte > destination, PaddingMode paddingMode, out int bytesWritten)
static UniversalCryptoTransform Create(PaddingMode paddingMode, BasicSymmetricCipher cipher, bool encrypting)
bool TransformOneShot(ReadOnlySpan< byte > input, Span< byte > output, out int bytesWritten)
static unsafe void Copy(Array sourceArray, Array destinationArray, int length)
Definition Array.cs:624
static string Cryptography_CipherModeFeedbackNotSupported
Definition SR.cs:146
static string Cryptography_InvalidKeySize
Definition SR.cs:92
static string Cryptography_InvalidIVSize
Definition SR.cs:102
Definition SR.cs:7