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

◆ TryEncryptOrDecrypt() [1/2]

unsafe bool System.Security.Cryptography.RSACng.TryEncryptOrDecrypt ( ReadOnlySpan< byte > data,
Span< byte > destination,
RSAEncryptionPadding padding,
bool encrypt,
out int bytesWritten )
inlineprivate

Definition at line 233 of file RSACng.cs.

234 {
235 if (padding == null)
236 {
237 throw new ArgumentNullException("padding");
238 }
240 if (!encrypt && data.Length != num)
241 {
242 throw new CryptographicException(System.SR.Cryptography_RSA_DecryptWrongSize);
243 }
244 if (encrypt && padding.Mode == RSAEncryptionPaddingMode.Pkcs1 && data.Length > num - 11)
245 {
246 throw new CryptographicException(System.SR.Format(System.SR.Cryptography_Encryption_MessageTooLong, num - 11));
247 }
249 if (encrypt && data.Length == 0)
250 {
252 Span<byte> span = new Span<byte>(array, 0, num);
253 try
254 {
255 if (padding == RSAEncryptionPadding.Pkcs1)
256 {
258 }
259 else
260 {
261 if (padding.Mode != RSAEncryptionPaddingMode.Oaep)
262 {
263 throw new CryptographicException(System.SR.Cryptography_UnsupportedPaddingMode);
264 }
266 rsaPaddingProcessor.PadOaep(data, span);
267 }
268 return TryEncryptOrDecrypt(key, span, destination, global::Interop.NCrypt.AsymmetricPaddingMode.NCRYPT_NO_PADDING_FLAG, null, encrypt, out bytesWritten);
269 }
270 finally
271 {
272 CryptographicOperations.ZeroMemory(span);
274 }
275 }
276 switch (padding.Mode)
277 {
278 case RSAEncryptionPaddingMode.Pkcs1:
279 return TryEncryptOrDecrypt(key, data, destination, global::Interop.NCrypt.AsymmetricPaddingMode.NCRYPT_PAD_PKCS1_FLAG, null, encrypt, out bytesWritten);
280 case RSAEncryptionPaddingMode.Oaep:
281 {
282 IntPtr intPtr = Marshal.StringToHGlobalUni(padding.OaepHashAlgorithm.Name);
283 try
284 {
285 global::Interop.BCrypt.BCRYPT_OAEP_PADDING_INFO bCRYPT_OAEP_PADDING_INFO = default(global::Interop.BCrypt.BCRYPT_OAEP_PADDING_INFO);
286 bCRYPT_OAEP_PADDING_INFO.pszAlgId = intPtr;
287 bCRYPT_OAEP_PADDING_INFO.pbLabel = IntPtr.Zero;
288 bCRYPT_OAEP_PADDING_INFO.cbLabel = 0;
289 global::Interop.BCrypt.BCRYPT_OAEP_PADDING_INFO bCRYPT_OAEP_PADDING_INFO2 = bCRYPT_OAEP_PADDING_INFO;
290 return TryEncryptOrDecrypt(key, data, destination, global::Interop.NCrypt.AsymmetricPaddingMode.NCRYPT_PAD_OAEP_FLAG, &bCRYPT_OAEP_PADDING_INFO2, encrypt, out bytesWritten);
291 }
292 finally
293 {
295 }
296 }
297 default:
298 throw new CryptographicException(System.SR.Cryptography_UnsupportedPaddingMode);
299 }
300 }
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
SafeNCryptKeyHandle GetDuplicatedKeyHandle()
Definition RSACng.cs:105
unsafe bool TryEncryptOrDecrypt(ReadOnlySpan< byte > data, Span< byte > destination, RSAEncryptionPadding padding, bool encrypt, out int bytesWritten)
Definition RSACng.cs:233
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.destination, 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.Security.Cryptography.RSACng.TryEncryptOrDecrypt(), System.IntPtr.Zero, and System.Security.Cryptography.CryptographicOperations.ZeroMemory().

Referenced by System.Security.Cryptography.RSACng.TryDecrypt(), System.Security.Cryptography.RSACng.TryEncrypt(), and System.Security.Cryptography.RSACng.TryEncryptOrDecrypt().