### Question:
PopTokenBuilderUtils { public static RSAPrivateKey encryptedKeyPemStringToRsaPrivateKey(String encryptedPrivateKeyPemString, String privateKeyPassword) throws PopPrivateKeyParseException { if (StringUtils.isBlank(encryptedPrivateKeyPemString)) { throw new IllegalArgumentException("The encryptedPrivateKeyPemString should not be null or empty"); } if (StringUtils.isBlank(privateKeyPassword)) { throw new IllegalArgumentException("The privateKeyPassword should not be null or empty"); } try { if (encryptedPrivateKeyPemString.startsWith(PKCS_8_ENCRYPTED_PRIVATE_KEY_PREFIX) && encryptedPrivateKeyPemString.contains(PKCS_8_ENCRYPTED_PRIVATE_KEY_SUFFIX)) { return buildRsaPrivateKey(encryptedPrivateKeyPemString, privateKeyPassword); } else { throw new PopPrivateKeyParseException( "The encryptedPrivateKeyPemString contains unsupported format, only PKCS#8 format is currently supported"); } } catch (PopPrivateKeyParseException ex) { throw ex; } catch (Exception ex) { throw new PopPrivateKeyParseException( "Error occurred while converting encryptedPrivateKeyPemString to RSAPrivateKey, error: " + ex.toString(), ex); } }  static RSAPrivateKey keyPemStringToRsaPrivateKey(String privateKeyPemString); static RSAPrivateKey encryptedKeyPemStringToRsaPrivateKey(String encryptedPrivateKeyPemString, String privateKeyPassword);  }### Answer:
@Test public void aes256EncryptedKeyPemStringToRsaPrivateKeyWithPassword__validPrivateKeyPemStringInput__successfullyReturnsRsaPrivateKey() throws Exception { RSAPrivateKey rsaPrivateKey = PopTokenBuilderUtils.encryptedKeyPemStringToRsaPrivateKey(aes256EncryptedPrivateKeyPemString, privateKeyPassword); assertNotNull("The rsaPrivateKey should not be null", rsaPrivateKey); }
@Test public void des3EncryptedKeyPemStringToRsaPrivateKeyWithPassword__validPrivateKeyPemStringInput__successfullyReturnsRsaPrivateKey() throws Exception { RSAPrivateKey rsaPrivateKey = PopTokenBuilderUtils.encryptedKeyPemStringToRsaPrivateKey(des3EncryptedPrivateKeyPemString, privateKeyPassword); assertNotNull("The rsaPrivateKey should not be null", rsaPrivateKey); }
@Test public void encryptedKeyPemStringToRsaPrivateKeyWithPassword__nullPrivateKeyPemStringInput__throwsIllegalArgumentException() throws Exception { try { PopTokenBuilderUtils.encryptedKeyPemStringToRsaPrivateKey(null, null); fail("Should have thrown IllegalArgumentException"); } catch (Exception ex) { logger.error("Error occurred while executing the test case, error: " + ex.toString(), ex); assertEquals(IllegalArgumentException.class, ex.getClass()); assertEquals("The encryptedPrivateKeyPemString should not be null or empty", ex.getMessage()); } }
@Test public void encryptedKeyPemStringToRsaPrivateKeyWithPassword__invalidPrivateKeyPemString__throwsPopPrivateKeyParseException() { try { PopTokenBuilderUtils.encryptedKeyPemStringToRsaPrivateKey("INVALID KEY PEM STRING", "foobar"); fail("Should have thrown PopPrivateKeyParseException"); } catch (Exception ex) { logger.error("Error occurred while executing the test case, error: " + ex.toString(), ex); assertEquals(PopPrivateKeyParseException.class, ex.getClass()); assertEquals("The encryptedPrivateKeyPemString contains unsupported format, only PKCS#8 format is currently supported", ex.getMessage()); } }