Terraria v1.4.4.9
Terraria source code documentation
All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Events Macros

◆ Decrypt() [2/4]

static unsafe int System.Security.Cryptography.PasswordBasedEncryption.Decrypt ( in System::Security::Cryptography::Asn1::AlgorithmIdentifierAsn algorithmIdentifier,
ReadOnlySpan< char > password,
ReadOnlySpan< byte > passwordBytes,
ReadOnlySpan< byte > encryptedData,
Span< byte > destination )
inlinestaticpackage

Definition at line 41 of file PasswordBasedEncryption.cs.

42 {
43 SymmetricAlgorithm symmetricAlgorithm = null;
44 bool flag = false;
45 HashAlgorithmName hashAlgorithm;
46 switch (algorithmIdentifier.Algorithm)
47 {
48 case "1.2.840.113549.1.5.3":
49 hashAlgorithm = HashAlgorithmName.MD5;
50 symmetricAlgorithm = DES.Create();
51 break;
52 case "1.2.840.113549.1.5.6":
53 hashAlgorithm = HashAlgorithmName.MD5;
54 symmetricAlgorithm = CreateRC2();
55 break;
56 case "1.2.840.113549.1.5.10":
57 hashAlgorithm = HashAlgorithmName.SHA1;
58 symmetricAlgorithm = DES.Create();
59 break;
60 case "1.2.840.113549.1.5.11":
61 hashAlgorithm = HashAlgorithmName.SHA1;
62 symmetricAlgorithm = CreateRC2();
63 break;
64 case "1.2.840.113549.1.12.1.3":
65 hashAlgorithm = HashAlgorithmName.SHA1;
66 symmetricAlgorithm = TripleDES.Create();
67 flag = true;
68 break;
69 case "1.2.840.113549.1.12.1.4":
70 hashAlgorithm = HashAlgorithmName.SHA1;
71 symmetricAlgorithm = TripleDES.Create();
72 symmetricAlgorithm.KeySize = 128;
73 flag = true;
74 break;
75 case "1.2.840.113549.1.12.1.5":
76 hashAlgorithm = HashAlgorithmName.SHA1;
77 symmetricAlgorithm = CreateRC2();
78 symmetricAlgorithm.KeySize = 128;
79 flag = true;
80 break;
81 case "1.2.840.113549.1.12.1.6":
82 hashAlgorithm = HashAlgorithmName.SHA1;
83 symmetricAlgorithm = CreateRC2();
84 symmetricAlgorithm.KeySize = 40;
85 flag = true;
86 break;
87 case "1.2.840.113549.1.5.13":
88 return Pbes2Decrypt(algorithmIdentifier.Parameters, password, passwordBytes, encryptedData, destination);
89 default:
90 throw new CryptographicException(System.SR.Format(System.SR.Cryptography_UnknownAlgorithmIdentifier, algorithmIdentifier.Algorithm));
91 }
92 using (symmetricAlgorithm)
93 {
94 if (flag)
95 {
96 if (password.IsEmpty && passwordBytes.Length > 0)
97 {
98 throw AlgorithmKdfRequiresChars(algorithmIdentifier.Algorithm);
99 }
100 return Pkcs12PbeDecrypt(algorithmIdentifier, password, hashAlgorithm, symmetricAlgorithm, encryptedData, destination);
101 }
102 using IncrementalHash hasher = IncrementalHash.CreateHash(hashAlgorithm);
103 Span<byte> span = stackalloc byte[128];
104 ReadOnlySpan<byte> password2 = default(Span<byte>);
105 byte[] array = null;
106 Encoding encoding = null;
107 if (passwordBytes.Length > 0 || password.Length == 0)
108 {
109 password2 = passwordBytes;
110 }
111 else
112 {
113 encoding = Encoding.UTF8;
114 int byteCount = encoding.GetByteCount(password);
115 if (byteCount > span.Length)
116 {
118 span = array.AsSpan(0, byteCount);
119 }
120 else
121 {
122 span = span.Slice(0, byteCount);
123 }
124 }
125 fixed (byte* ptr = &MemoryMarshal.GetReference(span))
126 {
127 if (encoding != null)
128 {
129 span = span[..encoding.GetBytes(password, span)];
130 password2 = span;
131 }
132 try
133 {
134 return Pbes1Decrypt(algorithmIdentifier.Parameters, password2, hasher, symmetricAlgorithm, encryptedData, destination);
135 }
136 finally
137 {
138 CryptographicOperations.ZeroMemory(span);
139 if (array != null)
140 {
142 }
143 }
144 }
145 }
146 }
static string Cryptography_UnknownAlgorithmIdentifier
Definition SR.cs:150
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
Definition SR.cs:7
static void Return(byte[] array, int clearSize=-1)
Definition CryptoPool.cs:12
static byte[] Rent(int minimumLength)
Definition CryptoPool.cs:7
static int Pbes1Decrypt(ReadOnlyMemory< byte >? algorithmParameters, ReadOnlySpan< byte > password, IncrementalHash hasher, SymmetricAlgorithm cipher, ReadOnlySpan< byte > encryptedData, Span< byte > destination)
static unsafe int Pbes2Decrypt(ReadOnlyMemory< byte >? algorithmParameters, ReadOnlySpan< char > password, ReadOnlySpan< byte > passwordBytes, ReadOnlySpan< byte > encryptedData, Span< byte > destination)
static int Pkcs12PbeDecrypt(AlgorithmIdentifierAsn algorithmIdentifier, ReadOnlySpan< char > password, HashAlgorithmName hashAlgorithm, SymmetricAlgorithm cipher, ReadOnlySpan< byte > encryptedData, Span< byte > destination)
static CryptographicException AlgorithmKdfRequiresChars(string algId)
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

References System.Security.Cryptography.PasswordBasedEncryption.AlgorithmKdfRequiresChars(), System.array, System.byteCount, System.Security.Cryptography.DES.Create(), System.Security.Cryptography.TripleDES.Create(), System.Security.Cryptography.IncrementalHash.CreateHash(), System.Security.Cryptography.PasswordBasedEncryption.CreateRC2(), System.SR.Cryptography_UnknownAlgorithmIdentifier, System.destination, System.SR.Format(), System.Text.Encoding.GetByteCount(), System.Text.Encoding.GetBytes(), System.ReadOnlySpan< T >.IsEmpty, System.ReadOnlySpan< T >.Length, System.Span< T >.Length, System.Security.Cryptography.HashAlgorithmName.MD5, System.Security.Cryptography.PasswordBasedEncryption.Pbes1Decrypt(), System.Security.Cryptography.PasswordBasedEncryption.Pbes2Decrypt(), System.Security.Cryptography.PasswordBasedEncryption.Pkcs12PbeDecrypt(), System.Security.Cryptography.CryptoPool.Rent(), System.Security.Cryptography.CryptoPool.Return(), System.Security.Cryptography.HashAlgorithmName.SHA1, System.Span< T >.Slice(), System.Text.Encoding.UTF8, and System.Security.Cryptography.CryptographicOperations.ZeroMemory().