Skip to content

Commit 83e20a2

Browse files
committed
gui example extended
1 parent bf9a2cf commit 83e20a2

File tree

19 files changed

+406
-188
lines changed

19 files changed

+406
-188
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,5 +125,5 @@ Here is a list of awesome projects for cryptography:
125125
* [cryptacular](https://github.com/vt-middleware/cryptacular) The friendly complement to the BouncyCastle crypto API for Java.
126126
* [JSch](http://www.jcraft.com/jsch/) JSch is a pure Java implementation of SSH2.
127127
* [Apache Shiro](https://github.com/apache/shiro) Apache Shiro is a powerful and easy-to-use Java security framework that performs authentication, authorization, cryptography, and session management.
128-
128+
* [commons-ssl](http://juliusdavies.ca/commons-ssl/)
129129

crypt-core/src/main/java/de/alpharogroup/crypto/key/KeyExtensions.java

Lines changed: 1 addition & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@
2525
package de.alpharogroup.crypto.key;
2626

2727
import java.io.File;
28-
import java.io.FileInputStream;
2928
import java.io.IOException;
30-
import java.io.InputStreamReader;
3129
import java.nio.file.Files;
3230
import java.security.KeyFactory;
3331
import java.security.NoSuchAlgorithmException;
@@ -38,10 +36,6 @@
3836
import java.security.spec.PKCS8EncodedKeySpec;
3937
import java.security.spec.X509EncodedKeySpec;
4038

41-
import org.apache.commons.codec.binary.Base64;
42-
import org.bouncycastle.util.io.pem.PemObject;
43-
import org.bouncycastle.util.io.pem.PemReader;
44-
4539
import de.alpharogroup.crypto.algorithm.KeyPairGeneratorAlgorithm;
4640
import de.alpharogroup.crypto.key.reader.PemObjectReader;
4741
import de.alpharogroup.crypto.key.reader.PrivateKeyReader;
@@ -52,7 +46,7 @@
5246
/**
5347
* The class {@link KeyExtensions} holds utility methods for read public and private keys from
5448
* files.
55-
*
49+
*
5650
* @deprecated Use instead the reader classes {@link PublicKeyReader}, {@link PrivateKeyReader} and
5751
* {@link PemObjectReader}.
5852
*/
@@ -82,53 +76,6 @@ public class KeyExtensions
8276
/** The Constant BEGIN_RSA_PRIVATE_KEY_PREFIX. */
8377
public static final String BEGIN_RSA_PRIVATE_KEY_PREFIX = "-----BEGIN RSA PRIVATE KEY-----\n";
8478

85-
/**
86-
* Gets the pem object.
87-
*
88-
* @param file
89-
* the file
90-
* @return the pem object
91-
* @throws IOException
92-
* Signals that an I/O exception has occurred.
93-
*/
94-
public static PemObject getPemObject(final File file) throws IOException
95-
{
96-
PemObject pemObject;
97-
final PemReader pemReader = new PemReader(new InputStreamReader(new FileInputStream(file)));
98-
try
99-
{
100-
pemObject = pemReader.readPemObject();
101-
}
102-
finally
103-
{
104-
pemReader.close();
105-
}
106-
return pemObject;
107-
}
108-
109-
/**
110-
* Read public key.
111-
*
112-
* @param file
113-
* the file
114-
* @return the public key
115-
* @throws IOException
116-
* Signals that an I/O exception has occurred.
117-
* @throws NoSuchAlgorithmException
118-
* is thrown if instantiation of the cypher object fails.
119-
* @throws InvalidKeySpecException
120-
* is thrown if generation of the SecretKey object fails.
121-
* @throws NoSuchProviderException
122-
* is thrown if the specified provider is not registered in the security provider
123-
* list.
124-
*/
125-
public static PublicKey readPublicKey(final File file) throws IOException,
126-
NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException
127-
{
128-
final byte[] keyBytes = Files.readAllBytes(file.toPath());
129-
return readPublicKey(keyBytes, "BC");
130-
}
131-
13279
/**
13380
* Read public key.
13481
*
@@ -227,87 +174,4 @@ public static PrivateKey readPrivateKey(final byte[] privateKeyBytes, final Stri
227174
return privateKey;
228175
}
229176

230-
/**
231-
* Read pem private key.
232-
*
233-
* @param file
234-
* the file
235-
* @param securityProvider
236-
* the security provider
237-
* @return the private key
238-
* @throws Exception
239-
* is thrown if if a security error occur
240-
*/
241-
public static PrivateKey readPemPrivateKey(final File file,
242-
final SecurityProvider securityProvider) throws Exception
243-
{
244-
final byte[] keyBytes = Files.readAllBytes(file.toPath());
245-
246-
final String privateKeyAsString = new String(keyBytes)
247-
.replace(BEGIN_RSA_PRIVATE_KEY_PREFIX, "").replace(END_RSA_PRIVATE_KEY_SUFFIX, "")
248-
.trim();
249-
250-
final byte[] decoded = new Base64().decode(privateKeyAsString);
251-
252-
return readPrivateKey(decoded, securityProvider);
253-
}
254-
255-
/**
256-
* Read private key.
257-
*
258-
* @param privateKeyBytes
259-
* the private key bytes
260-
* @param securityProvider
261-
* the security provider
262-
* @return the private key
263-
* @throws NoSuchAlgorithmException
264-
* the no such algorithm exception
265-
* @throws InvalidKeySpecException
266-
* is thrown if generation of the SecretKey object fails.
267-
* @throws NoSuchProviderException
268-
* is thrown if the specified provider is not registered in the security provider
269-
* list.
270-
*/
271-
public static PrivateKey readPrivateKey(final byte[] privateKeyBytes,
272-
final SecurityProvider securityProvider)
273-
throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException
274-
{
275-
return readPrivateKey(privateKeyBytes, securityProvider.name());
276-
}
277-
278-
/**
279-
* reads a public key from a file.
280-
*
281-
* @param file
282-
* the file
283-
* @param securityProvider
284-
* the security provider
285-
* @return the public key
286-
* @throws Exception
287-
* is thrown if if a security error occur
288-
*/
289-
public static PublicKey readPemPublicKey(final File file,
290-
final SecurityProvider securityProvider) throws Exception
291-
{
292-
final byte[] keyBytes = Files.readAllBytes(file.toPath());
293-
final String publicKeyAsString = new String(keyBytes).replace(BEGIN_PUBLIC_KEY_PREFIX, "")
294-
.replace(END_PUBLIC_KEY_SUFFIX, "");
295-
final byte[] decoded = Base64.decodeBase64(publicKeyAsString);
296-
return readPublicKey(decoded, securityProvider);
297-
}
298-
299-
/**
300-
* reads a public key from a file.
301-
*
302-
* @param file
303-
* the file
304-
* @return the public key
305-
* @throws Exception
306-
* is thrown if if a security error occur
307-
*/
308-
public static PublicKey readPemPublicKey(final File file) throws Exception
309-
{
310-
return readPemPublicKey(file, SecurityProvider.BC);
311-
}
312-
313177
}

crypt-core/src/test/java/de/alpharogroup/crypto/key/KeyExtensionsTest.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626

2727
import java.io.File;
2828
import java.security.PrivateKey;
29+
import java.security.Security;
2930

31+
import org.bouncycastle.jce.provider.BouncyCastleProvider;
3032
import org.testng.AssertJUnit;
3133
import org.testng.annotations.Test;
3234

@@ -52,6 +54,7 @@ public void testReadPemPrivateKey() throws Exception
5254
final File privatekeyPemDir = new File(PathFinder.getSrcTestResourcesDir(), "pem");
5355
final File privatekeyPemFile = new File(privatekeyPemDir, "private.pem");
5456

57+
Security.addProvider(new BouncyCastleProvider());
5558
final PrivateKey privateKey = PrivateKeyReader.readPemPrivateKey(privatekeyPemFile,
5659
SecurityProvider.BC);
5760
AssertJUnit.assertNotNull(privateKey);
@@ -74,10 +77,4 @@ public void testReadDerPrivateKey() throws Exception
7477
AssertJUnit.assertNotNull(privateKey);
7578
}
7679

77-
@Test
78-
public void testConvertToBase64() throws Exception
79-
{
80-
81-
}
82-
8380
}

crypt-data/src/main/java/de/alpharogroup/crypto/factories/KeyPairFactory.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.security.NoSuchProviderException;
3333
import java.security.PrivateKey;
3434
import java.security.PublicKey;
35+
import java.security.SecureRandom;
3536
import java.security.spec.InvalidKeySpecException;
3637

3738
import de.alpharogroup.crypto.algorithm.Algorithm;
@@ -143,4 +144,26 @@ public static KeyPairGenerator newKeyPairGenerator(final String algorithm, final
143144
return generator;
144145
}
145146

147+
/**
148+
* Factory method for creating a new {@link KeyPairGenerator} from the given parameters.
149+
*
150+
* @param algorithm
151+
* the algorithm
152+
* @param keySize
153+
* the key size
154+
* @param secureRandom
155+
* the secure random
156+
* @return the new {@link KeyPairGenerator} from the given parameters.
157+
* @throws NoSuchAlgorithmException
158+
* is thrown if no Provider supports a KeyPairGeneratorSpi implementation for the
159+
* specified algorithm.
160+
*/
161+
public static KeyPairGenerator newKeyPairGenerator(final String algorithm, final int keySize,
162+
final SecureRandom secureRandom) throws NoSuchAlgorithmException
163+
{
164+
final KeyPairGenerator generator = KeyPairGenerator.getInstance(algorithm);
165+
generator.initialize(keySize, secureRandom);
166+
return generator;
167+
}
168+
146169
}

crypt-data/src/main/java/de/alpharogroup/crypto/key/PrivateKeyExtensions.java

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
*/
2525
package de.alpharogroup.crypto.key;
2626

27+
import java.io.IOException;
2728
import java.security.KeyFactory;
2829
import java.security.NoSuchAlgorithmException;
2930
import java.security.PrivateKey;
@@ -34,12 +35,18 @@
3435
import java.security.interfaces.RSAPrivateKey;
3536
import java.security.spec.InvalidKeySpecException;
3637
import java.security.spec.RSAPublicKeySpec;
37-
import java.util.List;
38+
39+
import javax.xml.bind.DatatypeConverter;
3840

3941
import org.apache.commons.codec.binary.Base64;
42+
import org.bouncycastle.asn1.ASN1Encodable;
43+
import org.bouncycastle.asn1.ASN1Primitive;
44+
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
45+
import org.bouncycastle.util.io.pem.PemObject;
4046

4147
import de.alpharogroup.crypto.algorithm.KeyPairGeneratorAlgorithm;
4248
import de.alpharogroup.crypto.hex.HexExtensions;
49+
import de.alpharogroup.crypto.key.reader.PemObjectReader;
4350
import de.alpharogroup.crypto.key.reader.PrivateKeyReader;
4451
import lombok.experimental.UtilityClass;
4552

@@ -120,6 +127,20 @@ public static String toBase64(final PrivateKey privateKey)
120127
return privateKeyAsBase64String;
121128
}
122129

130+
/**
131+
* Transform the given {@link PrivateKey} to a base64 encoded {@link String} value.
132+
*
133+
* @param privateKey
134+
* the private key
135+
* @return the new base64 encoded {@link String} value.
136+
*/
137+
public static String toBase64Binary(final PrivateKey privateKey)
138+
{
139+
final byte[] encoded = privateKey.getEncoded();
140+
final String privateKeyAsBase64String = DatatypeConverter.printBase64Binary(encoded);
141+
return privateKeyAsBase64String;
142+
}
143+
123144
/**
124145
* Generate the corresponding {@link PublicKey} object from the given {@link PrivateKey} object.
125146
*
@@ -149,28 +170,25 @@ public static PublicKey generatePublicKey(final PrivateKey privateKey)
149170
}
150171

151172

152-
153173
/**
154-
* Transform the public key in pem format.
174+
* Transform the given private key that is in PKCS1 format and returns a {@link String} object
175+
* in pem format.
155176
*
156-
* @param publicKey
157-
* the public key
158-
* @return the public key in pem format
177+
* @param privateKey
178+
* the private key
179+
* @return the {@link String} object in pem format generated from the given private key.
180+
* @throws IOException
181+
* Signals that an I/O exception has occurred.
159182
*/
160-
public static String toPemFormat(final PrivateKey privateKey)
183+
public static String toPemFormat(final PrivateKey privateKey) throws IOException
161184
{
162-
final String publicKeyAsBase64String = toBase64(privateKey);
163-
final List<String> parts = PublicKeyExtensions.splitByFixedLength(publicKeyAsBase64String, 64);
164-
165-
final StringBuilder sb = new StringBuilder();
166-
sb.append(PrivateKeyReader.BEGIN_RSA_PRIVATE_KEY_PREFIX);
167-
for(final String part : parts) {
168-
sb.append(part);
169-
sb.append(System.lineSeparator());
170-
}
171-
sb.append(PrivateKeyReader.END_RSA_PRIVATE_KEY_SUFFIX);
172-
sb.append(System.lineSeparator());
173-
return sb.toString();
185+
final byte[] encoded = privateKey.getEncoded();
186+
final PrivateKeyInfo privateKeyInfo = PrivateKeyInfo.getInstance(encoded);
187+
final ASN1Encodable asn1Encodable = privateKeyInfo.parsePrivateKey();
188+
final ASN1Primitive asn1Primitive = asn1Encodable.toASN1Primitive();
189+
final byte[] privateKeyPKCS1Formatted = asn1Primitive.getEncoded();
190+
return PemObjectReader
191+
.toPemFormat(new PemObject(PrivateKeyReader.RSA_PRIVATE_KEY, privateKeyPKCS1Formatted));
174192
}
175193

176194
}

crypt-data/src/main/java/de/alpharogroup/crypto/key/PublicKeyExtensions.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535

3636
import de.alpharogroup.crypto.hex.HexExtensions;
3737
import de.alpharogroup.crypto.key.reader.PublicKeyReader;
38-
import de.alpharogroup.string.StringExtensions;
3938
import lombok.experimental.UtilityClass;
4039

4140
/**
@@ -145,7 +144,7 @@ public static String toHexString(final PublicKey publicKey, final boolean lowerC
145144
* @param input the input
146145
* @param fixedLength the fixed length
147146
* @return the list with the splitted {@link String} objects
148-
* @deprecated use instead the same name method from {@link StringExtensions}
147+
* @deprecated use instead the same name method from StringExtensions
149148
*/
150149
@Deprecated
151150
public static List<String> splitByFixedLength(final String input, final int fixedLength) {

crypt-data/src/main/java/de/alpharogroup/crypto/key/reader/PemObjectReader.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@
2828
import java.io.FileInputStream;
2929
import java.io.IOException;
3030
import java.io.InputStreamReader;
31+
import java.io.StringWriter;
3132

3233
import org.bouncycastle.util.io.pem.PemObject;
3334
import org.bouncycastle.util.io.pem.PemReader;
35+
import org.bouncycastle.util.io.pem.PemWriter;
3436

3537
/**
3638
* The class {@link PemObjectReader} is a utility class for reading {@link PemObject} from a file.
@@ -61,4 +63,23 @@ public static PemObject getPemObject(final File file) throws IOException
6163
}
6264
return pemObject;
6365
}
66+
67+
68+
/**
69+
* Transform the given {@link PemObject} object in pem format {@link String} object.
70+
*
71+
* @return the pem object
72+
* @return the {@link String} object in pem format
73+
* @throws IOException
74+
* Signals that an I/O exception has occurred.
75+
*/
76+
public static String toPemFormat(final PemObject pemObject) throws IOException
77+
{
78+
final StringWriter stringWriter = new StringWriter();
79+
final PemWriter pemWriter = new PemWriter(stringWriter);
80+
pemWriter.writeObject(pemObject);
81+
pemWriter.close();
82+
final String pemString = stringWriter.toString();
83+
return pemString;
84+
}
6485
}

0 commit comments

Comments
 (0)