20 switch (encryptionAlgorithm)
46 switch (algorithmIdentifier.Algorithm)
48 case "1.2.840.113549.1.5.3":
52 case "1.2.840.113549.1.5.6":
56 case "1.2.840.113549.1.5.10":
60 case "1.2.840.113549.1.5.11":
64 case "1.2.840.113549.1.12.1.3":
69 case "1.2.840.113549.1.12.1.4":
72 symmetricAlgorithm.KeySize = 128;
75 case "1.2.840.113549.1.12.1.5":
78 symmetricAlgorithm.KeySize = 128;
81 case "1.2.840.113549.1.12.1.6":
84 symmetricAlgorithm.KeySize = 40;
87 case "1.2.840.113549.1.5.13":
92 using (symmetricAlgorithm)
109 password2 = passwordBytes;
127 if (encoding !=
null)
129 span = span[..encoding.
GetBytes(password, span)];
134 return Pbes1Decrypt(algorithmIdentifier.Parameters, password2, hasher, symmetricAlgorithm, encryptedData,
destination);
155 cipher.KeySize = 128;
156 encryptionAlgorithmOid =
"2.16.840.1.101.3.4.1.2";
160 cipher.KeySize = 192;
161 encryptionAlgorithmOid =
"2.16.840.1.101.3.4.1.22";
165 cipher.KeySize = 256;
166 encryptionAlgorithmOid =
"2.16.840.1.101.3.4.1.42";
170 cipher.KeySize = 192;
171 encryptionAlgorithmOid =
"1.2.840.113549.1.12.1.3";
180 hmacOid =
"1.2.840.113549.2.9";
185 hmacOid =
"1.2.840.113549.2.10";
190 hmacOid =
"1.2.840.113549.2.11";
195 hmacOid =
"1.2.840.113549.2.7";
205 byte[] iV = cipher.
IV;
206 int encodedLength =
source.GetEncodedLength();
208 int num = cipher.KeySize / 8;
214 array = ((passwordBytes.Length == 0 && password.Length > 0) ?
new byte[uTF.
GetByteCount(password)] : ((passwordBytes.Length != 0) ?
new byte[passwordBytes.
Length] :
Array.Empty<
byte>()));
216 fixed (
byte* ptr3 = array2)
218 fixed (
byte* ptr2 =
array)
223 array3 =
new byte[num];
230 if (passwordBytes.
Length > 0)
234 else if (password.
Length > 0)
244 array3 = rfc2898DeriveBytes.
GetBytes(num);
248 fixed (
byte* ptr = array3)
252 int num2 = cipher.BlockSize / 8;
253 int num3 = encodedLength % num2;
254 int num4 = encodedLength - num3;
257 if (!
source.TryEncode(array2, out var _))
264 num5 = cryptoTransform.TransformBlock(array2, 0, num4,
destination, 0);
266 byte[] array4 = cryptoTransform.TransformFinalBlock(array2, num5, num3);
268 return num5 + array4.Length;
287 password2 = passwordBytes;
305 if (encoding !=
null)
307 span = span[..encoding.
GetBytes(password, span)];
326 if (!algorithmParameters.HasValue)
335 int? requestedKeyLength;
337 using (rfc2898DeriveBytes)
341 using (symmetricAlgorithm)
344 fixed (
byte* ptr =
bytes)
361 string algorithm = encryptionScheme.Algorithm;
364 case "2.16.840.1.101.3.4.1.2":
365 case "2.16.840.1.101.3.4.1.22":
366 case "2.16.840.1.101.3.4.1.42":
368 int num = algorithm
switch
370 "2.16.840.1.101.3.4.1.2" => 16,
371 "2.16.840.1.101.3.4.1.22" => 24,
372 "2.16.840.1.101.3.4.1.42" => 32,
375 if (requestedKeyLength.HasValue && requestedKeyLength != num)
381 aes.KeySize = num * 8;
384 case "1.2.840.113549.3.7":
385 if (requestedKeyLength.HasValue && requestedKeyLength != 24)
391 case "1.2.840.113549.3.2":
393 if (!encryptionScheme.Parameters.HasValue)
397 if (!requestedKeyLength.HasValue)
402 if (rc2CbcParameters.
Iv.
Length != 8)
407 rC.KeySize = requestedKeyLength.Value * 8;
409 rc2CbcParameters.
Iv.
Span.CopyTo(iv);
410 iv = iv.Slice(0, rc2CbcParameters.
Iv.
Length);
414 if (requestedKeyLength.HasValue && requestedKeyLength != 8)
427 if (!encryptionSchemeParameters.HasValue)
438 iv = iv.Slice(0, bytesWritten);
448 if (!parameters.HasValue)
476 byte[] array2 =
new byte[
value.Length];
477 fixed (
byte* ptr2 =
array)
479 fixed (
byte* ptr = array2)
482 value.CopyTo(array2);
485 requestedKeyLength = pbkdf2Params.
KeyLength;
503 if (!algorithmParameters.HasValue)
520 Pbkdf1(hasher, password, pBEParameter.
Salt.
Span, iterationCount, span);
533 if (!algorithmIdentifier.Parameters.HasValue)
543 Span<byte> span = stackalloc
byte[cipher.BlockSize / 8];
544 Span<byte> span2 = stackalloc
byte[cipher.KeySize / 8];
561 byte[]
array =
new byte[
key.Length];
562 byte[] array2 =
new byte[iv.
Length];
565 fixed (
byte* ptr5 =
array)
567 fixed (
byte* ptr4 = array2)
569 fixed (
byte* ptr3 = array3)
571 fixed (
byte* ptr2 = array4)
578 encryptedData.
CopyTo(array3);
579 int num = cryptoTransform.TransformBlock(array3, 0, encryptedData.
Length, array4, 0);
581 byte[] array5 = cryptoTransform.TransformFinalBlock(
Array.Empty<
byte>(), 0, 0);
582 fixed (
byte* ptr = array5)
588 return num + array5.Length;
612 span = span.
Slice(0, bytesWritten);
613 for (
int i = 1; i < iterationCount; i++)
630 writer.WriteObjectIdentifierForCrypto(encryptionAlgorithmOid);
632 writer.WriteOctetString(salt);
638 writer.WriteObjectIdentifierForCrypto(
"1.2.840.113549.1.5.13");
641 writer.WriteObjectIdentifierForCrypto(
"1.2.840.113549.1.5.12");
643 writer.WriteOctetString(salt);
645 if (hmacOid !=
"1.2.840.113549.2.7")
648 writer.WriteObjectIdentifierForCrypto(hmacOid);
655 writer.WriteObjectIdentifierForCrypto(encryptionAlgorithmOid);
656 writer.WriteOctetString(iv);
665 if (iterationCount <= 0 || (iterationLimit.HasValue && iterationCount > iterationLimit.Value))
669 return iterationCount;
static bool IsRC2Supported
static string Cryptography_Der_Invalid_Encoding
static string Cryptography_AlgorithmNotSupported
static string Cryptography_UnknownAlgorithmIdentifier
static string Cryptography_UnknownHashAlgorithm
static string Cryptography_AlgKdfRequiresChars
static string Argument_InvalidValue
static string Format(string resourceFormat, object p1)
static void Return(byte[] array, int clearSize=-1)
static byte[] Rent(int minimumLength)
static void ZeroMemory(Span< byte > buffer)
static IncrementalHash CreateHash(HashAlgorithmName hashAlgorithm)
void AppendData(byte[] data)
bool TryGetHashAndReset(Span< byte > destination, out int bytesWritten)
static SymmetricAlgorithm OpenCipher(System.Security.Cryptography.Asn1.AlgorithmIdentifierAsn encryptionScheme, int? requestedKeyLength, ref Span< byte > iv)
static int Pbes1Decrypt(ReadOnlyMemory< byte >? algorithmParameters, ReadOnlySpan< byte > password, IncrementalHash hasher, SymmetricAlgorithm cipher, ReadOnlySpan< byte > encryptedData, Span< byte > destination)
static void InitiateEncryption(PbeParameters pbeParameters, out SymmetricAlgorithm cipher, out string hmacOid, out string encryptionAlgorithmOid, out bool isPkcs12)
static void ReadIvParameter(ReadOnlyMemory< byte >? encryptionSchemeParameters, int length, ref Span< byte > iv)
static unsafe int Encrypt(ReadOnlySpan< char > password, ReadOnlySpan< byte > passwordBytes, SymmetricAlgorithm cipher, bool isPkcs12, AsnWriter source, PbeParameters pbeParameters, ReadOnlySpan< byte > salt, byte[] destination, Span< byte > ivDest)
static unsafe int Pbes2Decrypt(ReadOnlyMemory< byte >? algorithmParameters, ReadOnlySpan< char > password, ReadOnlySpan< byte > passwordBytes, ReadOnlySpan< byte > encryptedData, Span< byte > destination)
static SymmetricAlgorithm OpenCipher(AlgorithmIdentifierAsn encryptionScheme, int? requestedKeyLength, ref Span< byte > iv)
static int Pkcs12PbeDecrypt(AlgorithmIdentifierAsn algorithmIdentifier, ReadOnlySpan< char > password, HashAlgorithmName hashAlgorithm, SymmetricAlgorithm cipher, ReadOnlySpan< byte > encryptedData, Span< byte > destination)
static int NormalizeIterationCount(int iterationCount, int? iterationLimit=null)
static CryptographicException AlgorithmKdfRequiresChars(string algId)
static unsafe Rfc2898DeriveBytes OpenPbkdf2(ReadOnlySpan< byte > password, ReadOnlyMemory< byte >? parameters, out int? requestedKeyLength)
static unsafe int Decrypt(in AlgorithmIdentifierAsn algorithmIdentifier, ReadOnlySpan< char > password, ReadOnlySpan< byte > passwordBytes, ReadOnlySpan< byte > encryptedData, Span< byte > destination)
static void Pbkdf1(IncrementalHash hasher, ReadOnlySpan< byte > password, ReadOnlySpan< byte > salt, int iterationCount, Span< byte > dk)
static int Pkcs12PbeDecrypt(System.Security.Cryptography.Asn1.AlgorithmIdentifierAsn algorithmIdentifier, ReadOnlySpan< char > password, HashAlgorithmName hashAlgorithm, SymmetricAlgorithm cipher, ReadOnlySpan< byte > encryptedData, Span< byte > destination)
static void WritePbeAlgorithmIdentifier(AsnWriter writer, bool isPkcs12, string encryptionAlgorithmOid, Span< byte > salt, int iterationCount, string hmacOid, Span< byte > iv)
static unsafe int Decrypt(in System.Security.Cryptography.Asn1.AlgorithmIdentifierAsn algorithmIdentifier, ReadOnlySpan< char > password, ReadOnlySpan< byte > passwordBytes, ReadOnlySpan< byte > encryptedData, Span< byte > destination)
static unsafe int Decrypt(SymmetricAlgorithm cipher, ReadOnlySpan< byte > key, ReadOnlySpan< byte > iv, ReadOnlySpan< byte > encryptedData, Span< byte > destination)
static unsafe int Pbes2Decrypt(ReadOnlyMemory< byte >? algorithmParameters, ReadOnlySpan< byte > password, ReadOnlySpan< byte > encryptedData, Span< byte > destination)
static void ValidatePbeParameters(PbeParameters pbeParameters, ReadOnlySpan< char > password, ReadOnlySpan< byte > passwordBytes)
HashAlgorithmName HashAlgorithm
PbeEncryptionAlgorithm EncryptionAlgorithm
static void DeriveCipherKey(ReadOnlySpan< char > password, HashAlgorithmName hashAlgorithm, int iterationCount, ReadOnlySpan< byte > salt, Span< byte > destination)
static void DeriveIV(ReadOnlySpan< char > password, HashAlgorithmName hashAlgorithm, int iterationCount, ReadOnlySpan< byte > salt, Span< byte > destination)
override byte[] GetBytes(int cb)
virtual ICryptoTransform CreateEncryptor()
virtual ICryptoTransform CreateDecryptor()
static new TripleDES Create()
virtual byte[] GetBytes(char[] chars)
virtual int GetByteCount(char[] chars)
unsafe ReadOnlySpan< T > Span
void CopyTo(Span< T > destination)
readonly bool HasNullEquivalentParameters()
ReadOnlyMemory< byte >? Parameters
static PBEParameter Decode(ReadOnlyMemory< byte > encoded, AsnEncodingRules ruleSet)
ReadOnlyMemory< byte > Salt
static PBES2Params Decode(ReadOnlyMemory< byte > encoded, AsnEncodingRules ruleSet)
AlgorithmIdentifierAsn KeyDerivationFunc
AlgorithmIdentifierAsn EncryptionScheme
AlgorithmIdentifierAsn Prf
static Pbkdf2Params Decode(ReadOnlyMemory< byte > encoded, AsnEncodingRules ruleSet)
ReadOnlyMemory< byte >? Specified
AlgorithmIdentifierAsn? OtherSource
ReadOnlyMemory< byte > Iv
int GetEffectiveKeyBits()
static Rc2CbcParameters Decode(ReadOnlyMemory< byte > encoded, AsnEncodingRules ruleSet)
static HashAlgorithmName SHA1
static HashAlgorithmName SHA512
static HashAlgorithmName SHA256
static HashAlgorithmName SHA384
static HashAlgorithmName MD5
Span< T > Slice(int start)