|
|
|
@ -1,5 +1,5 @@
@@ -1,5 +1,5 @@
|
|
|
|
|
/* |
|
|
|
|
* Copyright 2013-2014 the original author or authors. |
|
|
|
|
* 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. |
|
|
|
@ -15,13 +15,23 @@
@@ -15,13 +15,23 @@
|
|
|
|
|
*/ |
|
|
|
|
package org.springframework.cloud.context.encrypt; |
|
|
|
|
|
|
|
|
|
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; |
|
|
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
|
import java.io.StringReader; |
|
|
|
|
import java.io.StringWriter; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* @author Dave Syer |
|
|
|
|
* |
|
|
|
|
* @author Biju Kunjummen |
|
|
|
|
*/ |
|
|
|
|
public class EncryptorFactory { |
|
|
|
|
|
|
|
|
@ -34,7 +44,8 @@ public class EncryptorFactory {
@@ -34,7 +44,8 @@ public class EncryptorFactory {
|
|
|
|
|
if (data.contains("RSA PRIVATE KEY")) { |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
encryptor = new RsaSecretEncryptor(data); |
|
|
|
|
String normalizedPemData = normalizePem(data); |
|
|
|
|
encryptor = new RsaSecretEncryptor(normalizedPemData.replaceAll("\\n", "").replaceAll("\\r", "")); |
|
|
|
|
} |
|
|
|
|
catch (IllegalArgumentException e) { |
|
|
|
|
throw new KeyFormatException(e); |
|
|
|
@ -51,6 +62,26 @@ public class EncryptorFactory {
@@ -51,6 +62,26 @@ public class EncryptorFactory {
|
|
|
|
|
return encryptor; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private String normalizePem(String data) { |
|
|
|
|
PEMParser pemParser = new PEMParser(new StringReader(data)); |
|
|
|
|
PEMKeyPair pemKeyPair = null; |
|
|
|
|
try { |
|
|
|
|
pemKeyPair = (PEMKeyPair) pemParser.readObject(); |
|
|
|
|
PrivateKeyInfo privateKeyInfo = pemKeyPair.getPrivateKeyInfo(); |
|
|
|
|
|
|
|
|
|
StringWriter textWriter = new StringWriter(); |
|
|
|
|
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); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|