/*
 * Copyright 2013-2018 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
 *
 *      http://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.cloud.context.encrypt;

import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;

import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import org.bouncycastle.openssl.MiscPEMGenerator;
import org.bouncycastle.openssl.PEMKeyPair;
import org.bouncycastle.openssl.PEMParser;
import org.bouncycastle.util.io.pem.PemObjectGenerator;
import org.bouncycastle.util.io.pem.PemWriter;

import org.springframework.security.crypto.encrypt.Encryptors;
import org.springframework.security.crypto.encrypt.TextEncryptor;
import org.springframework.security.rsa.crypto.RsaSecretEncryptor;

/**
 * @author Dave Syer
 * @author Biju Kunjummen
 */
public class EncryptorFactory {

	private String salt = "deadbeef";

	public EncryptorFactory() {
	}

	public EncryptorFactory(String salt) {
		this.salt = salt;
	}

	public TextEncryptor create(String data) {

		TextEncryptor encryptor;
		if (data.contains("RSA PRIVATE KEY")) {

			try {
				String normalizedPemData = normalizePem(data);
				encryptor = new RsaSecretEncryptor(
						normalizedPemData.replaceAll("\\n", "").replaceAll("\\r", ""));
			}
			catch (IllegalArgumentException e) {
				throw new KeyFormatException(e);
			}

		}
		else if (data.startsWith("ssh-rsa") || data.contains("RSA PUBLIC KEY")) {
			throw new KeyFormatException();
		}
		else {
			encryptor = Encryptors.text(data, salt);
		}

		return encryptor;
	}

	private String normalizePem(String data) {
		PEMKeyPair pemKeyPair = null;
		try (PEMParser pemParser = new PEMParser(new StringReader(data))) {
			pemKeyPair = (PEMKeyPair) pemParser.readObject();
			PrivateKeyInfo privateKeyInfo = pemKeyPair.getPrivateKeyInfo();

			StringWriter textWriter = new StringWriter();
			try (PemWriter pemWriter = new PemWriter(textWriter)) {
				PemObjectGenerator pemObjectGenerator = new MiscPEMGenerator(
						privateKeyInfo);

				pemWriter.writeObject(pemObjectGenerator);
				pemWriter.flush();
				return textWriter.toString();
			}
		}
		catch (IOException e) {
			throw new RuntimeException(e.getMessage(), e);
		}
	}

}

@@ -747,24 +747,56 @@
 tringWriter;
+%0Aimport java.util.regex.Pattern;
 %0A%0Aimport org
@@ -1361,16 +1361,100 @@
 tory %7B%0A%0A
+%09private static final Pattern NEWLINE_ESCAPE_PATTERN = Pattern.compile(%22%5C%5Cr%7C%5C%5Cn%22);%0A%0A
 %09private
@@ -1803,46 +1803,56 @@
 %09%09%09%09
-normalizedPemData.replaceAll(%22%5C%5Cn%22, %22%22
+NEWLINE_ESCAPE_PATTERN.matcher(normalizedPemData
 ).re
@@ -1864,15 +1864,8 @@
 All(
-%22%5C%5Cr%22, 
 %22%22))

Refactor String#replaceAll (#417)