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