Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
RC2Implementation.cs
Go to the documentation of this file.
1using System;
4
6
7internal sealed class RC2Implementation : RC2
8{
9 public override int EffectiveKeySize
10 {
11 get
12 {
13 return KeySizeValue;
14 }
15 set
16 {
17 if (value != KeySizeValue)
18 {
20 }
21 }
22 }
23
25 {
26 return CreateTransform(Key, IV, encrypting: false);
27 }
28
29 public override ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[] rgbIV)
30 {
31 return CreateTransform(rgbKey, rgbIV.CloneByteArray(), encrypting: false);
32 }
33
35 {
36 return CreateTransform(Key, IV, encrypting: true);
37 }
38
39 public override ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[] rgbIV)
40 {
41 return CreateTransform(rgbKey, rgbIV.CloneByteArray(), encrypting: true);
42 }
43
44 public override void GenerateIV()
45 {
47 }
48
49 public sealed override void GenerateKey()
50 {
52 }
53
54 private ICryptoTransform CreateTransform(byte[] rgbKey, byte[] rgbIV, bool encrypting)
55 {
56 if (rgbKey == null)
57 {
58 throw new ArgumentNullException("rgbKey");
59 }
60 if (!ValidKeySize(rgbKey.Length, out var keySizeBits))
61 {
63 }
64 if (rgbIV != null)
65 {
66 long num = (long)rgbIV.Length * 8L;
67 if (num != BlockSize)
68 {
70 }
71 }
72 if (Mode == CipherMode.CFB)
73 {
75 }
76 int effectiveKeyLength = ((EffectiveKeySizeValue == 0) ? keySizeBits : EffectiveKeySize);
77 return CreateTransformCore(Mode, Padding, rgbKey, effectiveKeyLength, rgbIV, BlockSize / 8, FeedbackSize / 8, GetPaddingSize(), encrypting);
78 }
79
80 protected override bool TryDecryptEcbCore(ReadOnlySpan<byte> ciphertext, Span<byte> destination, PaddingMode paddingMode, out int bytesWritten)
81 {
82 if (!ValidKeySize(Key.Length, out var keySizeBits))
83 {
85 }
86 int effectiveKeyLength = ((EffectiveKeySizeValue == 0) ? keySizeBits : EffectiveKeySize);
87 UniversalCryptoTransform universalCryptoTransform = CreateTransformCore(CipherMode.ECB, paddingMode, Key, effectiveKeyLength, null, BlockSize / 8, 0, BlockSize / 8, encrypting: false);
88 using (universalCryptoTransform)
89 {
90 return universalCryptoTransform.TransformOneShot(ciphertext, destination, out bytesWritten);
91 }
92 }
93
94 protected override bool TryEncryptEcbCore(ReadOnlySpan<byte> plaintext, Span<byte> destination, PaddingMode paddingMode, out int bytesWritten)
95 {
96 if (!ValidKeySize(Key.Length, out var keySizeBits))
97 {
99 }
100 int effectiveKeyLength = ((EffectiveKeySizeValue == 0) ? keySizeBits : EffectiveKeySize);
101 UniversalCryptoTransform universalCryptoTransform = CreateTransformCore(CipherMode.ECB, paddingMode, Key, effectiveKeyLength, null, BlockSize / 8, 0, BlockSize / 8, encrypting: true);
102 using (universalCryptoTransform)
103 {
104 return universalCryptoTransform.TransformOneShot(plaintext, destination, out bytesWritten);
105 }
106 }
107
108 protected override bool TryEncryptCbcCore(ReadOnlySpan<byte> plaintext, ReadOnlySpan<byte> iv, Span<byte> destination, PaddingMode paddingMode, out int bytesWritten)
109 {
110 if (!ValidKeySize(Key.Length, out var keySizeBits))
111 {
113 }
114 int effectiveKeyLength = ((EffectiveKeySizeValue == 0) ? keySizeBits : EffectiveKeySize);
115 UniversalCryptoTransform universalCryptoTransform = CreateTransformCore(CipherMode.CBC, paddingMode, Key, effectiveKeyLength, iv.ToArray(), BlockSize / 8, 0, BlockSize / 8, encrypting: true);
116 using (universalCryptoTransform)
117 {
118 return universalCryptoTransform.TransformOneShot(plaintext, destination, out bytesWritten);
119 }
120 }
121
122 protected override bool TryDecryptCbcCore(ReadOnlySpan<byte> ciphertext, ReadOnlySpan<byte> iv, Span<byte> destination, PaddingMode paddingMode, out int bytesWritten)
123 {
124 if (!ValidKeySize(Key.Length, out var keySizeBits))
125 {
127 }
128 int effectiveKeyLength = ((EffectiveKeySizeValue == 0) ? keySizeBits : EffectiveKeySize);
129 UniversalCryptoTransform universalCryptoTransform = CreateTransformCore(CipherMode.CBC, paddingMode, Key, effectiveKeyLength, iv.ToArray(), BlockSize / 8, 0, BlockSize / 8, encrypting: false);
130 using (universalCryptoTransform)
131 {
132 return universalCryptoTransform.TransformOneShot(ciphertext, destination, out bytesWritten);
133 }
134 }
135
136 protected override bool TryDecryptCfbCore(ReadOnlySpan<byte> ciphertext, ReadOnlySpan<byte> iv, Span<byte> destination, PaddingMode paddingMode, int feedbackSizeInBits, out int bytesWritten)
137 {
139 }
140
141 protected override bool TryEncryptCfbCore(ReadOnlySpan<byte> plaintext, ReadOnlySpan<byte> iv, Span<byte> destination, PaddingMode paddingMode, int feedbackSizeInBits, out int bytesWritten)
142 {
144 }
145
146 private static void ValidateCFBFeedbackSize(int feedback)
147 {
149 }
150
151 private int GetPaddingSize()
152 {
153 return BlockSize / 8;
154 }
155
156 private bool ValidKeySize(int keySizeBytes, out int keySizeBits)
157 {
158 if (keySizeBytes > 268435455)
159 {
160 keySizeBits = 0;
161 return false;
162 }
163 keySizeBits = keySizeBytes << 3;
164 return keySizeBits.IsLegalSize(LegalKeySizes);
165 }
166
167 private static UniversalCryptoTransform CreateTransformCore(CipherMode cipherMode, PaddingMode paddingMode, byte[] key, int effectiveKeyLength, byte[] iv, int blockSize, int feedbackSize, int paddingSize, bool encrypting)
168 {
169 using SafeAlgorithmHandle algorithm = RC2BCryptModes.GetHandle(cipherMode, effectiveKeyLength);
170 BasicSymmetricCipher cipher = new BasicSymmetricCipherBCrypt(algorithm, cipherMode, blockSize, paddingSize, key, ownsParentHandle: true, iv, encrypting);
171 return UniversalCryptoTransform.Create(paddingMode, cipher, encrypting);
172 }
173}
static SafeAlgorithmHandle GetHandle(CipherMode cipherMode, int effectiveKeyLength)
static void ValidateCFBFeedbackSize(int feedback)
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 TryEncryptCfbCore(ReadOnlySpan< byte > plaintext, 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)
override bool TryDecryptEcbCore(ReadOnlySpan< byte > ciphertext, Span< byte > destination, PaddingMode paddingMode, out int bytesWritten)
override ICryptoTransform CreateEncryptor()
static UniversalCryptoTransform CreateTransformCore(CipherMode cipherMode, PaddingMode paddingMode, byte[] key, int effectiveKeyLength, byte[] iv, int blockSize, int feedbackSize, int paddingSize, bool encrypting)
override ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[] rgbIV)
override bool TryDecryptCbcCore(ReadOnlySpan< byte > ciphertext, ReadOnlySpan< byte > iv, Span< byte > destination, PaddingMode paddingMode, out int bytesWritten)
bool ValidKeySize(int keySizeBytes, out int keySizeBits)
override ICryptoTransform CreateDecryptor()
override ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[] rgbIV)
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_RC2_EKSKS2
Definition SR.cs:134
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string Cryptography_CipherModeFeedbackNotSupported
Definition SR.cs:146
static string Cryptography_CipherModeNotSupported
Definition SR.cs:148
static string Cryptography_InvalidKeySize
Definition SR.cs:92
static string Cryptography_InvalidIVSize
Definition SR.cs:102
Definition SR.cs:7