<commit_before>/*
 * Copyright 2011-2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.security.crypto.encrypt;

import java.security.spec.AlgorithmParameterSpec;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;

import org.springframework.security.crypto.codec.Hex;
import org.springframework.security.crypto.keygen.BytesKeyGenerator;
import org.springframework.security.crypto.keygen.KeyGenerators;
import org.springframework.security.crypto.util.EncodingUtils;

/**
 * Encryptor that uses AES encryption.
 *
 * @author <NAME>
 * @author <NAME>
 */
public final class AesBytesEncryptor implements BytesEncryptor {

	private final SecretKey secretKey;

	private final Cipher encryptor;

	private final Cipher decryptor;

	private final BytesKeyGenerator ivGenerator;

	private CipherAlgorithm alg;

	private static final String AES_CBC_ALGORITHM = "AES/CBC/PKCS5Padding";

	private static final String AES_GCM_ALGORITHM = "AES/GCM/NoPadding";

	public AesBytesEncryptor(String password, CharSequence salt) {
		this(password, salt, null);
	}

	public AesBytesEncryptor(String password, CharSequence salt, BytesKeyGenerator ivGenerator) {
		this(password, salt, ivGenerator, CipherAlgorithm.CBC);
	}

	public AesBytesEncryptor(String password, CharSequence salt, BytesKeyGenerator ivGenerator, CipherAlgorithm alg) {
		this(CipherUtils.newSecretKey("PBKDF2WithHmacSHA1",
				new PBEKeySpec(password.toCharArray(), Hex.decode(salt), 1024, 256)), ivGenerator, alg);
	}

	/**
	 * Constructs an encryptor that uses AES encryption.
	 * @param secretKey the secret (symmetric) key
	 * @param ivGenerator the generator used to generate the initialization vector. If
	 * null, then a default algorithm will be used based on the provided
	 * {@link CipherAlgorithm}<commit_msg>Add javadoc at constructors. Closes gh-9361<commit_after>/*
 * Copyright 2011-2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.security.crypto.encrypt;

import java.security.spec.AlgorithmParameterSpec;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;

import org.springframework.security.crypto.codec.Hex;
import org.springframework.security.crypto.keygen.BytesKeyGenerator;
import org.springframework.security.crypto.keygen.KeyGenerators;
import org.springframework.security.crypto.util.EncodingUtils;

/**
 * Encryptor that uses AES encryption.
 *
 * @author <NAME>
 * @author <NAME>
 */
public final class AesBytesEncryptor implements BytesEncryptor {

	private final SecretKey secretKey;

	private final Cipher encryptor;

	private final Cipher decryptor;

	private final BytesKeyGenerator ivGenerator;

	private CipherAlgorithm alg;

	private static final String AES_CBC_ALGORITHM = "AES/CBC/PKCS5Padding";

	private static final String AES_GCM_ALGORITHM = "AES/GCM/NoPadding";

	/**
	 * Constructs an encryptor that uses AES encryption. Example: <code>
	 * AesBytesEncryptor encryptor = new AesBytesEncryptor(yourPassword, <PASSWORD>);
	 * </code>
	 * @param password the password value
	 * @param salt the hex-encoded salt value
	 */
	public AesBytesEncryptor(String password, CharSequence salt) {
		this(password, salt, null);
	}

	/**
	 * Constructs an encryptor that uses AES encryption. Example: <code>
	 * AesBytesEncryptor encryptor =
	 * 		new AesBytesEncryptor(yourPassword, <PASSWORD>, KeyGenerators.secureRandom(16));
	 * </code>
	 * @param password the password value
	 * @param salt the hex-encoded salt value
	 * @param ivGenerator the generator used to generate the initialization vector
	 */
	public AesBytesEncryptor(String password, CharSequence salt, BytesKeyGenerator ivGenerator) {
		this(password, salt, ivGenerator, CipherAlgorithm.CBC);
	}

	/**
	 * Constructs an encryptor that uses AES encryption. Example: <code>
	 * AesBytesEncryptor encryptor =
	 * 		new AesBytesEncryptor(yourPassword, <PASSWORD>, KeyGenerators.secureRandom(16), CipherAlgorithm.GCM);
	 * </code>
	 * @param password the password value
	 * @param salt the hex-encoded salt value
	 * @param ivGenerator the generator used to generate the initialization vector
	 * @param alg the {@link CipherAlgorithm} to be used
	 */
	public AesBytesEncryptor(String password, CharSequence salt, BytesKeyGenerator ivGenerator, CipherAlgorithm alg) {
		this(CipherUtils.newSecretKey("PBKDF2WithHmacSHA1",
				new PBEKeySpec(password.toCharArray(), Hex.decode(salt), 1024, 256)), ivGenerator, alg);
	}

	/**
	 * Constructs an encryptor that uses AES encryption.
	 * @param secretKey the secret (symmetric) key
	 * @param ivGenerator the generator used to generate the initialization vector. If
	 * null, then a default algorithm will be used based on the provided
	 * {@link CipherAlgorithm}
{'fraction_non_alphanumeric': 0.08357723577235772, 'fraction_numerical': 0.008455284552845528, 'mean_word_length': 4.920115495668912, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1570082', 'n_tokens_mistral': 1827, 'n_tokens_neox': 1680, 'n_words': 573}