Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 1 | package aQute.libg.cryptography; |
| 2 | |
| 3 | import java.math.*; |
| 4 | import java.security.*; |
| 5 | import java.security.interfaces.*; |
| 6 | import java.security.spec.*; |
| 7 | |
| 8 | import aQute.libg.tuple.*; |
| 9 | |
| 10 | public 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 | } |