package de.alpharogroup.crypto.factories;

import java.io.File;
import java.security.PrivateKey;
import java.security.Security;

import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.testng.annotations.Test;

import de.alpharogroup.crypto.key.reader.PrivateKeyReader;
import de.alpharogroup.crypto.provider.SecurityProvider;
import de.alpharogroup.file.search.PathFinder;

public class KeyPairFactoryTest
{

	@Test
	public void testProtectPrivateKeyWithPassword() throws Exception
	{
		final File privatekeyPemDir = new File(PathFinder.getSrcTestResourcesDir(), "pem");
		final File privatekeyPemFile = new File(privatekeyPemDir, "private.pem");

		Security.addProvider(new BouncyCastleProvider());
		final String privateKeyAsBase64String = PrivateKeyReader
			.readPemFileAsBase64(privatekeyPemFile);

		final PrivateKey privateKey = PrivateKeyReader.readPemPrivateKey(privatekeyPemFile,
			SecurityProvider.BC);

		final byte[] pwprotectedKey = KeyPairFactory.protectPrivateKeyWithPassword(privateKey,
			"secret");


	}

}

@@ -47,22 +47,53 @@
 ort 
-java.io.File
+static org.testng.AssertJUnit.assertEquals
 ;%0A
+%0A
 impo
@@ -104,27 +104,15 @@
 ava.
-security.PrivateKey
+io.File
 ;%0Aim
@@ -134,77 +134,20 @@
 ity.
-Security;%0A%0Aimport org.bouncycastle.jce.provider.BouncyCastleProvider;
+PrivateKey;%0A
 %0Aimp
@@ -266,124 +266,393 @@
 oup.
-crypto.provider.SecurityProvider;%0Aimport de.alpharogroup.file.search.PathFinder;%0A%0Apublic class KeyPairFactoryTest%0A%7B%0A
+file.search.PathFinder;%0A%0A/**%0A * The unit test class for the class %7B@link KeyPairFactory%7D.%0A */%0Apublic class KeyPairFactoryTest %7B%0A%0A%09/**%0A%09 * Test method for%0A%09 * %7B@link KeyPairFactory#protectPrivateKeyWithPassword(PrivateKey, String)%7D%0A%09 * and%0A%09 * %7B@link KeyPairFactory#decryptPasswordProtectedPrivateKey(byte%5B%5D, String, String)%7D%0A%09 *%0A%09 * @throws Exception%0A%09 *             the exception%0A%09 */
 %0A%09@T
@@ -720,18 +720,17 @@
 xception
-%0A%09
+ 
 %7B%0A%09%09fina
@@ -737,28 +737,27 @@
 l File p
-rivatekeyPem
+ublickeyDer
 Dir = ne
@@ -801,19 +801,19 @@
 Dir(), %22
-pem
+der
 %22);%0A%09%09fi
@@ -827,27 +827,27 @@
 e privatekey
-Pem
+Der
 File = new F
@@ -851,28 +851,27 @@
 w File(p
-rivatekeyPem
+ublickeyDer
 Dir, %22pr
@@ -880,107 +880,45 @@
 ate.
-pem
+der
 %22);%0A%0A%09%09
-Security.addProvider(new BouncyCastleProvider());%0A%09%09final String privateKeyAsBase64String
+final PrivateKey privateKey
  = P
@@ -936,32 +936,23 @@
 ader
-%0A%09%09%09
 .readP
-emFileAsBase64
+rivateKey
 (pri
@@ -962,93 +962,132 @@
 ekey
-Pem
+Der
 File);%0A
-%0A
 %09%09
-final PrivateKey priva
+String password = %22secret%22;%0A%09%09final byte%5B%5D pwprotec
 te
+d
 Key = 
-PrivateKeyReader.readPemPrivateKey
+KeyPairFactory.protectPrivateKeyWithPassword
 (private
 keyP
@@ -1086,72 +1086,42 @@
 vate
-keyPemFile,%0A%09%09%09SecurityProvider.BC);%0A%0A%09%09final byte%5B%5D pwprotected
+Key, password);%0A%0A%09%09PrivateKey priv
 Key 
@@ -1129,39 +1129,56 @@
  KeyPairFactory.
-p
+decryptPasswordP
 rotect
+ed
 PrivateKeyWithPa
@@ -1175,48 +1175,79 @@
 eKey
-WithPassword(privateKey,%0A%09%09%09%22secret%22);%0A%0A
+(pwprotectedKey, password, %22RSA%22);%0A%09%09assertEquals(privateKey, privKey);
 %0A%09%7D%0A

Update KeyPairFactoryTest.java