diff --git a/security-modules/pom.xml b/security-modules/pom.xml
index 12c1714e6c..5702cfa98f 100644
--- a/security-modules/pom.xml
+++ b/security-modules/pom.xml
@@ -23,6 +23,7 @@
jwt
oauth2-framework-impl
sql-injection-samples
+ unrecoverablekeyexception
diff --git a/security-modules/unrecoverablekeyexception/.gitignore b/security-modules/unrecoverablekeyexception/.gitignore
new file mode 100644
index 0000000000..f83e8cf07c
--- /dev/null
+++ b/security-modules/unrecoverablekeyexception/.gitignore
@@ -0,0 +1,3 @@
+.idea
+target
+*.iml
diff --git a/security-modules/unrecoverablekeyexception/pom.xml b/security-modules/unrecoverablekeyexception/pom.xml
new file mode 100644
index 0000000000..47fcce0f5b
--- /dev/null
+++ b/security-modules/unrecoverablekeyexception/pom.xml
@@ -0,0 +1,19 @@
+
+
+ 4.0.0
+
+ com.baeldung.unrecoverablekeyexception
+ unrecoverablekeyexception
+
+ unrecoverablekeyexception
+
+
+ com.baeldung
+ security-modules
+ 1.0.0-SNAPSHOT
+ ../pom.xml
+
+
+
\ No newline at end of file
diff --git a/security-modules/unrecoverablekeyexception/src/main/java/com/baeldung/unrecoverablekeyexception/KeyManagerInitializer.java b/security-modules/unrecoverablekeyexception/src/main/java/com/baeldung/unrecoverablekeyexception/KeyManagerInitializer.java
new file mode 100644
index 0000000000..c3434edaa3
--- /dev/null
+++ b/security-modules/unrecoverablekeyexception/src/main/java/com/baeldung/unrecoverablekeyexception/KeyManagerInitializer.java
@@ -0,0 +1,32 @@
+package com.baeldung.unrecoverablekeyexception;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URISyntaxException;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.security.KeyStore;
+import java.security.KeyStoreException;
+import java.security.NoSuchAlgorithmException;
+import java.security.UnrecoverableKeyException;
+import java.security.cert.CertificateException;
+import javax.net.ssl.KeyManagerFactory;
+import javax.net.ssl.X509ExtendedKeyManager;
+
+public class KeyManagerInitializer {
+
+ public static X509ExtendedKeyManager initializeKeyManager(String privateKeyPassword, String keystoreLocation)
+ throws NoSuchAlgorithmException, KeyStoreException, IOException, CertificateException, UnrecoverableKeyException, URISyntaxException {
+ KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
+ KeyStore instance = KeyStore.getInstance(KeyStore.getDefaultType());
+ InputStream resourceAsStream = Files.newInputStream(Paths.get(ClassLoader.getSystemResource(keystoreLocation).toURI()));
+ instance.load(resourceAsStream, "admin123".toCharArray());
+ kmf.init(instance, privateKeyPassword.toCharArray());
+ return (X509ExtendedKeyManager) kmf.getKeyManagers()[0];
+ }
+
+ public static X509ExtendedKeyManager initializeKeyManager(String privateKeyPassword)
+ throws NoSuchAlgorithmException, KeyStoreException, IOException, CertificateException, UnrecoverableKeyException, URISyntaxException {
+ return initializeKeyManager(privateKeyPassword, "single_entry_keystore.jks");
+ }
+}
diff --git a/security-modules/unrecoverablekeyexception/src/main/resources/multi_entry_keystore.jks b/security-modules/unrecoverablekeyexception/src/main/resources/multi_entry_keystore.jks
new file mode 100644
index 0000000000..f75047ab53
Binary files /dev/null and b/security-modules/unrecoverablekeyexception/src/main/resources/multi_entry_keystore.jks differ
diff --git a/security-modules/unrecoverablekeyexception/src/main/resources/single_entry_keystore.jks b/security-modules/unrecoverablekeyexception/src/main/resources/single_entry_keystore.jks
new file mode 100644
index 0000000000..253f36f374
Binary files /dev/null and b/security-modules/unrecoverablekeyexception/src/main/resources/single_entry_keystore.jks differ
diff --git a/security-modules/unrecoverablekeyexception/src/test/java/com/baeldung/unrecoverablekeyexception/KeyManagerInitializerUnitTest.java b/security-modules/unrecoverablekeyexception/src/test/java/com/baeldung/unrecoverablekeyexception/KeyManagerInitializerUnitTest.java
new file mode 100644
index 0000000000..a8d203edc0
--- /dev/null
+++ b/security-modules/unrecoverablekeyexception/src/test/java/com/baeldung/unrecoverablekeyexception/KeyManagerInitializerUnitTest.java
@@ -0,0 +1,45 @@
+package com.baeldung.unrecoverablekeyexception;
+
+import java.security.UnrecoverableKeyException;
+import org.assertj.core.api.Assertions;
+import org.assertj.core.api.ThrowableAssert.ThrowingCallable;
+import org.junit.Test;
+
+public class KeyManagerInitializerUnitTest {
+
+ @Test
+ public void givenPasswordIsCorrect_whenInitializingTheKeyManager_thenNoExceptionIsThrown() {
+ // Given.
+ String privateKeyPassword = "privateKeyPassword";
+
+ // When.
+ ThrowingCallable initializeKeyManager = () -> KeyManagerInitializer.initializeKeyManager(privateKeyPassword);
+
+ // Then.
+ Assertions.assertThatCode(initializeKeyManager).doesNotThrowAnyException();;
+ }
+
+ @Test
+ public void givenPasswordIsWrong_whenInitializingTheKeyManager_thenUnrecoverableKeyExceptionExceptionIsThrown() {
+ // Given.
+ String privateKeyPassword = "wrongPassword";
+
+ // When.
+ ThrowingCallable initializeKeyManager = () -> KeyManagerInitializer.initializeKeyManager(privateKeyPassword);
+
+ // Then.
+ Assertions.assertThatThrownBy(initializeKeyManager).isInstanceOf(UnrecoverableKeyException.class);
+ }
+
+ @Test
+ public void givenMultipleKeysWithDifferentPasswordsInKeystore_whenInitializingTheKeyManager_thenUnrecoverableKeyExceptionIsThrown() {
+ // Given.
+ String firstPrivateKeyPassword = "abc123";
+
+ // When.
+ ThrowingCallable initializeKeyManager = () -> KeyManagerInitializer.initializeKeyManager(firstPrivateKeyPassword, "multi_entry_keystore.jks");
+
+ // Then.
+ Assertions.assertThatThrownBy(initializeKeyManager).isInstanceOf(UnrecoverableKeyException.class);
+ }
+}
\ No newline at end of file