blob: 61bafb52a53d30bf4ea1d78d903794e5ec45a052 [file] [log] [blame]
Stuart McCullochbb014372012-06-07 21:57:32 +00001package aQute.libg.cryptography;
2
3import java.math.*;
4import java.security.*;
5import java.security.interfaces.*;
6import java.security.spec.*;
7
8import aQute.libg.tuple.*;
9
10public class RSA {
11 final static String ALGORITHM = "RSA";
12
13 final static KeyFactory factory = getKeyFactory();
14
15 static private KeyFactory getKeyFactory() {
16 try {
17 return KeyFactory.getInstance(ALGORITHM);
18 } catch (Exception e) {
19 // built in
20 }
21 return null;
22 }
23
24 public static RSAPrivateKey create(RSAPrivateKeySpec keyspec) throws InvalidKeySpecException {
25 return (RSAPrivateKey) factory.generatePrivate(keyspec);
26 }
27 public static RSAPublicKey create(RSAPublicKeySpec keyspec) throws InvalidKeySpecException {
28 return (RSAPublicKey) factory.generatePrivate(keyspec);
29 }
30
31 public static RSAPublicKey createPublic(BigInteger m, BigInteger e) throws InvalidKeySpecException {
32 return create( new RSAPublicKeySpec(m,e));
33 }
34 public static RSAPrivateKey createPrivate(BigInteger m, BigInteger e) throws InvalidKeySpecException {
35 return create( new RSAPrivateKeySpec(m,e));
36 }
37
38 public static Pair<RSAPrivateKey, RSAPublicKey> generate() throws NoSuchAlgorithmException {
39 KeyPairGenerator kpg = KeyPairGenerator.getInstance(ALGORITHM);
40 KeyPair keypair = kpg.generateKeyPair();
41 return new Pair<RSAPrivateKey,RSAPublicKey>( (RSAPrivateKey) keypair.getPrivate(), (RSAPublicKey) keypair.getPublic());
42 }
43}