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