Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches

◆ Encrypt() [2/2]

static unsafe int System.Security.Cryptography.PasswordBasedEncryption.Encrypt ( ReadOnlySpan< char > password,
ReadOnlySpan< byte > passwordBytes,
SymmetricAlgorithm cipher,
bool isPkcs12,
AsnWriter source,
PbeParameters pbeParameters,
ReadOnlySpan< byte > salt,
byte[] destination,
Span< byte > ivDest )
inlinestaticpackage

Definition at line 202 of file PasswordBasedEncryption.cs.

203 {
204 byte[] array = null;
205 byte[] iV = cipher.IV;
206 int encodedLength = source.GetEncodedLength();
207 byte[] array2 = System.Security.Cryptography.CryptoPool.Rent(encodedLength);
208 int num = cipher.KeySize / 8;
209 int iterationCount = pbeParameters.IterationCount;
210 HashAlgorithmName hashAlgorithm = pbeParameters.HashAlgorithm;
211 Encoding uTF = Encoding.UTF8;
212 if (!isPkcs12)
213 {
214 array = ((passwordBytes.Length == 0 && password.Length > 0) ? new byte[uTF.GetByteCount(password)] : ((passwordBytes.Length != 0) ? new byte[passwordBytes.Length] : Array.Empty<byte>()));
215 }
216 fixed (byte* ptr3 = array2)
217 {
218 fixed (byte* ptr2 = array)
219 {
220 byte[] array3;
221 if (isPkcs12)
222 {
223 array3 = new byte[num];
224 System.Security.Cryptography.Pkcs.Pkcs12Kdf.DeriveCipherKey(password, hashAlgorithm, iterationCount, salt, array3);
225 System.Security.Cryptography.Pkcs.Pkcs12Kdf.DeriveIV(password, hashAlgorithm, iterationCount, salt, iV);
226 ivDest.Clear();
227 }
228 else
229 {
230 if (passwordBytes.Length > 0)
231 {
232 passwordBytes.CopyTo(array);
233 }
234 else if (password.Length > 0)
235 {
236 int bytes = uTF.GetBytes(password, array);
237 if (bytes != array.Length)
238 {
239 throw new CryptographicException();
240 }
241 }
242 using (Rfc2898DeriveBytes rfc2898DeriveBytes = new Rfc2898DeriveBytes(array, salt.ToArray(), iterationCount, hashAlgorithm))
243 {
244 array3 = rfc2898DeriveBytes.GetBytes(num);
245 }
246 iV.CopyTo(ivDest);
247 }
248 fixed (byte* ptr = array3)
249 {
250 CryptographicOperations.ZeroMemory(array);
251 using ICryptoTransform cryptoTransform = cipher.CreateEncryptor(array3, iV);
252 int num2 = cipher.BlockSize / 8;
253 int num3 = encodedLength % num2;
254 int num4 = encodedLength - num3;
255 try
256 {
257 if (!source.TryEncode(array2, out var _))
258 {
259 throw new CryptographicException();
260 }
261 int num5 = 0;
262 if (num4 != 0)
263 {
264 num5 = cryptoTransform.TransformBlock(array2, 0, num4, destination, 0);
265 }
266 byte[] array4 = cryptoTransform.TransformFinalBlock(array2, num5, num3);
267 array4.AsSpan().CopyTo(destination.AsSpan(num5));
268 return num5 + array4.Length;
269 }
270 finally
271 {
272 System.Security.Cryptography.CryptoPool.Return(array2, encodedLength);
273 }
274 }
275 }
276 }
277 }
static void Return(byte[] array, int clearSize=-1)
Definition CryptoPool.cs:12
static byte[] Rent(int minimumLength)
Definition CryptoPool.cs:7
static void DeriveCipherKey(ReadOnlySpan< char > password, HashAlgorithmName hashAlgorithm, int iterationCount, ReadOnlySpan< byte > salt, Span< byte > destination)
Definition Pkcs12Kdf.cs:16
static void DeriveIV(ReadOnlySpan< char > password, HashAlgorithmName hashAlgorithm, int iterationCount, ReadOnlySpan< byte > salt, Span< byte > destination)
Definition Pkcs12Kdf.cs:21
static Encoding UTF8
Definition Encoding.cs:526
virtual byte[] GetBytes(char[] chars)
Definition Encoding.cs:781
virtual int GetByteCount(char[] chars)
Definition Encoding.cs:713
void CopyTo(Span< T > destination)
unsafe void Clear()
Definition Span.cs:198

References System.array, System.bytes, System.Span< T >.Clear(), System.ReadOnlySpan< T >.CopyTo(), System.Security.Cryptography.SymmetricAlgorithm.CreateEncryptor(), System.Security.Cryptography.Pkcs.Pkcs12Kdf.DeriveCipherKey(), System.Security.Cryptography.Pkcs.Pkcs12Kdf.DeriveIV(), System.destination, System.Text.Encoding.GetByteCount(), System.Text.Encoding.GetBytes(), System.Security.Cryptography.Rfc2898DeriveBytes.GetBytes(), System.Security.Cryptography.PbeParameters.HashAlgorithm, System.Security.Cryptography.PbeParameters.IterationCount, System.Security.Cryptography.SymmetricAlgorithm.IV, System.ReadOnlySpan< T >.Length, System.Security.Cryptography.CryptoPool.Rent(), System.Security.Cryptography.CryptoPool.Return(), System.source, System.ReadOnlySpan< T >.ToArray(), System.Text.Encoding.UTF8, and System.Security.Cryptography.CryptographicOperations.ZeroMemory().