internal static void ValidatePbeParameters(
            PbeParameters pbeParameters,
            ReadOnlySpan <char> password,
            ReadOnlySpan <byte> passwordBytes)
        {
            // Leave the ArgumentNullException in the public entrypoints.
            Debug.Assert(pbeParameters != null);

            // Constructor promise.
            Debug.Assert(pbeParameters.IterationCount > 0);

            PbeEncryptionAlgorithm encryptionAlgorithm = pbeParameters.EncryptionAlgorithm;

            switch (encryptionAlgorithm)
            {
            case PbeEncryptionAlgorithm.Aes128Cbc:
            case PbeEncryptionAlgorithm.Aes192Cbc:
            case PbeEncryptionAlgorithm.Aes256Cbc:
                return;

            case PbeEncryptionAlgorithm.TripleDes3KeyPkcs12:
            {
                if (pbeParameters.HashAlgorithm != HashAlgorithmName.SHA1)
                {
                    throw new CryptographicException(
                              SR.Cryptography_UnknownHashAlgorithm,
                              pbeParameters.HashAlgorithm.Name);
                }

                if (passwordBytes.Length > 0 && password.Length == 0)
                {
                    throw AlgorithmKdfRequiresChars(
                              encryptionAlgorithm.ToString());
                }

                return;
            }
            }

            throw new CryptographicException(
                      SR.Cryptography_UnknownAlgorithmIdentifier,
                      encryptionAlgorithm.ToString());
        }
Пример #2
0
        public static string Encrypt(this string text, string keyString, PbeEncryptionAlgorithm encryptionAlgorithm = PbeEncryptionAlgorithm.Aes256Cbc)
        {
            var encryptedString = string.Empty;

            switch (encryptionAlgorithm)
            {
            case PbeEncryptionAlgorithm.Unknown:
                throw new ArgumentException("Encryption algorithm not set");

            case PbeEncryptionAlgorithm.Aes128Cbc:
                if (keyString.Length < 16)
                {
                    throw new ArgumentException("Encryption key should be at least 128 bits");
                }
                encryptedString = EncryptWithAes(text, keyString);
                break;

            case PbeEncryptionAlgorithm.Aes192Cbc:
                if (keyString.Length < 24)
                {
                    throw new ArgumentException("Encryption key should be at least 192 bits");
                }
                encryptedString = EncryptWithAes(text, keyString);
                break;

            case PbeEncryptionAlgorithm.Aes256Cbc:
                if (keyString.Length < 32)
                {
                    throw new ArgumentException("Encryption key should be at least 256 bits");
                }
                encryptedString = EncryptWithAes(text, keyString);
                break;

            case PbeEncryptionAlgorithm.TripleDes3KeyPkcs12:
                encryptedString = EncryptWith3Des(text, keyString);
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(encryptionAlgorithm), encryptionAlgorithm, null);
            }

            return(encryptedString);
        }
Пример #3
0
 public PbeParameters(PbeEncryptionAlgorithm encryptionAlgorithm, HashAlgorithmName hashAlgorithm, int iterationCount)
 {
 }