<commit_before>package com.github.jochenw.afw.core.crypt;

import java.io.InputStream;
import java.io.OutputStream;
import java.security.Key;
import java.security.KeyPair;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.cert.Certificate;

import com.github.jochenw.afw.core.util.Reflection;
import com.github.jochenw.afw.core.util.Tupel;

/** Default implementation of {@link IKeyHandler}. Attempts to use the {@link JceKeyHandler},
 * which should work on Java 8. If that doesn't work, uses the {@link BcKeyHandler}.
 */
public class DefaultKeyHandler implements IKeyHandler {
	private static final IKeyHandler keyHandler;
	static {
		IKeyHandler kh;
		try {
			final Object x509CertInfo = Reflection.newObject("sun.security.x509.X509CertInfo");
			kh = new JceKeyHandler();
		} catch (Throwable t) {
			kh = new BcKeyHandler();
		}
		keyHandler = kh;
	}

	@Override
	public KeyPair createKeyPair() {
		return keyHandler.createKeyPair();
	}

	@Override
	public Certificate generateCertificate(String pDn, KeyPair pKeyPair, int pValidity) {
		return keyHandler.generateCertificate(pDn, pKeyPair, pValidity);
	}

	@Override
	public KeyStore createKeyStore(PrivateKey pPrivateKey, Certificate pCertificate, String pAlias, String pStoreType,
			String pStorePass, String pKeyPass) {
		return keyHandler.createKeyStore(pPrivateKey, pCertificate, pAlias, pStoreType, pStorePass, pKeyPass);
	}

	@Override
	public void createKeyStore(OutputStream pOut, PrivateKey pPrivateKey, Certificate pCertificate, String pAlias,
			String pStoreType, String pStorePass, String pKeyPass) {
		keyHandler.createKeyStore(pOut, pPrivateKey, pCertificate, pAlias, pStoreType, pStorePass, pKeyPass);
	}

	@Override
	public Tupel<PrivateKey, Certificate> readPrivateKey(InputStream pIn, String pAlias, String pStorePass,
			String pKeyPass) {
		return keyHandler.readPrivateKey(pIn, pAlias, pStorePass, pKeyPass);
	}

	@Override
	public byte[] encrypt(Key pKey, byte[] pBytes) {
		return keyHandler.encrypt(pKey, pBytes);
	}

	@Override
	public byte[] decrypt(Key pKey, byte[] pBytes) {
		return keyHandler.encrypt(pKey, pBytes);
	}
}<commit_msg>Restoring the old crypto code, as a working base line.<commit_after>package com.github.jochenw.afw.core.crypt;

import java.io.InputStream;
import java.io.OutputStream;
import java.security.Key;
import java.security.KeyPair;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.cert.Certificate;

import com.github.jochenw.afw.core.util.Reflection;
import com.github.jochenw.afw.core.util.Tupel;

/** Default implementation of {@link IKeyHandler}. Uses JCE, if
 * possible, otherwise BouncyCastle.
 */
public class DefaultKeyHandler implements IKeyHandler {
	private static final IKeyHandler handler = newDefaultKeyHandler();

	private static IKeyHandler newDefaultKeyHandler() {
		try {
			// Check, whether the JCE Crypto code is available.
			Reflection.newObject("sun.security.x509.X509CertInfo");
			// If so, use the JceKeyHandler.
			return new JceKeyHandler();
		} catch (Throwable t) {
			// Otherwise, use the BcKeyHandler.
			return new BcKeyHandler();
		}
	}

	@Override
	public KeyPair createKeyPair() {
		return handler.createKeyPair();
	}

	@Override
	public Certificate generateCertificate(String pDn, KeyPair pKeyPair, int pValidity) {
		return handler.generateCertificate(pDn, pKeyPair, pValidity);
	}

	@Override
	public KeyStore createKeyStore(PrivateKey pPrivateKey, Certificate pCertificate, String pAlias, String pStoreType,
			String pStorePass, String pKeyPass) {
		return handler.createKeyStore(pPrivateKey, pCertificate, pAlias, pStoreType, pStorePass, pKeyPass);
	}

	@Override
	public void createKeyStore(OutputStream pOut, PrivateKey pPrivateKey, Certificate pCertificate, String pAlias,
			String pStoreType, String pStorePass, String pKeyPass) {
		handler.createKeyStore(pOut, pPrivateKey, pCertificate, pAlias, pStoreType, pStorePass, pKeyPass);
	}

	@Override
	public Tupel<PrivateKey, Certificate> readPrivateKey(InputStream pIn, String pAlias, String pStorePass,
			String pKeyPass) {
		return handler.readPrivateKey(pIn, pAlias, pStorePass, pKeyPass);
	}

	@Override
	public byte[] encrypt(Key pKey, byte[] pBytes) {
		return handler.encrypt(pKey, pBytes);
	}

	@Override
	public byte[] decrypt(Key pKey, byte[] pBytes) {
		return handler.decrypt(pKey, pBytes);
	}
}
{'fraction_non_alphanumeric': 0.09583428181197359, 'fraction_numerical': 0.003642157978602322, 'mean_word_length': 5.4145985401459855, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4047832', 'n_tokens_mistral': 1469, 'n_tokens_neox': 1373, 'n_words': 361}