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

◆ EncryptOrDecrypt() [1/3]

unsafe byte[] System.Security.Cryptography.RSACng.EncryptOrDecrypt ( byte[] data,
RSAEncryptionPadding padding,
bool encrypt )
inlineprivate

Definition at line 160 of file RSACng.cs.

161 {
162 if (data == null)
163 {
164 throw new ArgumentNullException("data");
165 }
166 if (padding == null)
167 {
168 throw new ArgumentNullException("padding");
169 }
171 if (!encrypt && data.Length != num)
172 {
173 throw new CryptographicException(System.SR.Cryptography_RSA_DecryptWrongSize);
174 }
175 if (encrypt && padding.Mode == RSAEncryptionPaddingMode.Pkcs1 && data.Length > num - 11)
176 {
177 throw new CryptographicException(System.SR.Format(System.SR.Cryptography_Encryption_MessageTooLong, num - 11));
178 }
180 if (encrypt && data.Length == 0)
181 {
183 Span<byte> span = new Span<byte>(array, 0, num);
184 try
185 {
186 if (padding == RSAEncryptionPadding.Pkcs1)
187 {
189 }
190 else
191 {
192 if (padding.Mode != RSAEncryptionPaddingMode.Oaep)
193 {
194 throw new CryptographicException(System.SR.Cryptography_UnsupportedPaddingMode);
195 }
197 rsaPaddingProcessor.PadOaep(data, span);
198 }
199 return EncryptOrDecrypt(key, span, global::Interop.NCrypt.AsymmetricPaddingMode.NCRYPT_NO_PADDING_FLAG, null, encrypt);
200 }
201 finally
202 {
203 CryptographicOperations.ZeroMemory(span);
205 }
206 }
207 switch (padding.Mode)
208 {
209 case RSAEncryptionPaddingMode.Pkcs1:
210 return EncryptOrDecrypt(key, data, global::Interop.NCrypt.AsymmetricPaddingMode.NCRYPT_PAD_PKCS1_FLAG, null, encrypt);
211 case RSAEncryptionPaddingMode.Oaep:
212 {
213 IntPtr intPtr = Marshal.StringToHGlobalUni(padding.OaepHashAlgorithm.Name);
214 try
215 {
216 global::Interop.BCrypt.BCRYPT_OAEP_PADDING_INFO bCRYPT_OAEP_PADDING_INFO = default(global::Interop.BCrypt.BCRYPT_OAEP_PADDING_INFO);
217 bCRYPT_OAEP_PADDING_INFO.pszAlgId = intPtr;
218 bCRYPT_OAEP_PADDING_INFO.pbLabel = IntPtr.Zero;
219 bCRYPT_OAEP_PADDING_INFO.cbLabel = 0;
220 global::Interop.BCrypt.BCRYPT_OAEP_PADDING_INFO bCRYPT_OAEP_PADDING_INFO2 = bCRYPT_OAEP_PADDING_INFO;
221 return EncryptOrDecrypt(key, data, global::Interop.NCrypt.AsymmetricPaddingMode.NCRYPT_PAD_OAEP_FLAG, &bCRYPT_OAEP_PADDING_INFO2, encrypt);
222 }
223 finally
224 {
226 }
227 }
228 default:
229 throw new CryptographicException(System.SR.Cryptography_UnsupportedPaddingMode);
230 }
231 }
static void FreeHGlobal(IntPtr hglobal)
Definition Marshal.cs:1680
static unsafe IntPtr StringToHGlobalUni(string? s)
Definition Marshal.cs:1310
static string Cryptography_RSA_DecryptWrongSize
Definition SR.cs:138
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string Cryptography_UnsupportedPaddingMode
Definition SR.cs:160
static string Cryptography_Encryption_MessageTooLong
Definition SR.cs:52
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
unsafe byte[] EncryptOrDecrypt(byte[] data, RSAEncryptionPadding padding, bool encrypt)
Definition RSACng.cs:160
SafeNCryptKeyHandle GetDuplicatedKeyHandle()
Definition RSACng.cs:105
static void PadPkcs1Encryption(ReadOnlySpan< byte > source, Span< byte > destination)
static RsaPaddingProcessor OpenProcessor(HashAlgorithmName hashAlgorithmName)

References System.array, System.Security.Cryptography.RsaPaddingProcessor.BytesRequiredForBitCount(), System.SR.Cryptography_Encryption_MessageTooLong, System.SR.Cryptography_RSA_DecryptWrongSize, System.SR.Cryptography_UnsupportedPaddingMode, System.Security.Cryptography.RSACng.EncryptOrDecrypt(), System.SR.Format(), System.Runtime.InteropServices.Marshal.FreeHGlobal(), System.Security.Cryptography.RSACng.GetDuplicatedKeyHandle(), System.key, System.Security.Cryptography.AsymmetricAlgorithm.KeySize, System.Security.Cryptography.RSAEncryptionPadding.Mode, System.Security.Cryptography.HashAlgorithmName.Name, System.Security.Cryptography.RSAEncryptionPadding.OaepHashAlgorithm, System.Security.Cryptography.RsaPaddingProcessor.OpenProcessor(), System.Security.Cryptography.RsaPaddingProcessor.PadPkcs1Encryption(), System.Security.Cryptography.RSAEncryptionPadding.Pkcs1, System.Security.Cryptography.CryptoPool.Rent(), System.Security.Cryptography.CryptoPool.Return(), System.Runtime.InteropServices.Marshal.StringToHGlobalUni(), System.IntPtr.Zero, and System.Security.Cryptography.CryptographicOperations.ZeroMemory().

Referenced by System.Security.Cryptography.RSACng.Decrypt(), System.Security.Cryptography.RSACng.Encrypt(), System.Security.Cryptography.RSACng.EncryptOrDecrypt(), System.Security.Cryptography.RSACng.EncryptOrDecrypt(), and System.Security.Cryptography.RSACng.TryEncryptOrDecrypt().