View Javadoc
1 /* 2 * Copyright �, Aegeus Technology Limited. All rights reserved. 3 */ 4 package jsdsi.util; 5 6 import java.security.InvalidAlgorithmParameterException; 7 import java.security.KeyPair; 8 import java.security.KeyPairGenerator; 9 import java.security.KeyPairGeneratorSpi; 10 import java.security.NoSuchAlgorithmException; 11 import java.security.NoSuchProviderException; 12 import java.security.SecureRandom; 13 import java.security.spec.AlgorithmParameterSpec; 14 15 import jsdsi.JsdsiRuntimeException; 16 import jsdsi.PublicKey; 17 import jsdsi.RSAPublicKey; 18 19 /*** 20 * @author Sean Radford 21 * @version $Revision: 1.6 $ $Date: 2004/11/12 09:53:49 $ 22 */ 23 public class RSAKeyPairGeneratorSpi extends KeyPairGeneratorSpi { 24 25 public static final String ALGORITHM = "RSA"; 26 27 private RSAKeyGenParameterSpec spec; 28 29 private Integer keySize; 30 31 private SecureRandom random; 32 33 private KeyPairGenerator keyPairGenerator; 34 35 /*** 36 * 37 */ 38 public RSAKeyPairGeneratorSpi() { 39 super(); 40 } 41 42 /*** 43 * @see java.security.KeyPairGeneratorSpi#initialize(java.security.spec.AlgorithmParameterSpec, 44 * java.security.SecureRandom) 45 */ 46 public void initialize(AlgorithmParameterSpec params, SecureRandom random) 47 throws InvalidAlgorithmParameterException { 48 if (params instanceof RSAKeyGenParameterSpec) { 49 RSAKeyGenParameterSpec p = (RSAKeyGenParameterSpec) params; 50 this.spec = new RSAKeyGenParameterSpec(p.getKeysize(), 51 p.getPublicExponent(), p 52 .getProvider(), p.getUris()); 53 this.random = random; 54 } else { 55 throw new IllegalArgumentException("Illegal AlgorithmParamterSpec class: " 56 + params.getClass().getName()); 57 } 58 } 59 60 /*** 61 * @see java.security.KeyPairGeneratorSpi#initialize(int, java.security.SecureRandom) 62 */ 63 public void initialize(int keysize, SecureRandom random) { 64 this.keySize = new Integer(keysize); 65 this.random = random; 66 } 67 68 /*** 69 * @see java.security.KeyPairGeneratorSpi#generateKeyPair() 70 */ 71 public KeyPair generateKeyPair() { 72 try { 73 KeyPairGenerator kpg = getKeyPairGenerator(); 74 KeyPair kp = kpg.generateKeyPair(); 75 PublicKey pubKey = null; 76 if (this.spec != null && this.spec.getUris() != null) { 77 pubKey = new RSAPublicKey((java.security.interfaces.RSAPublicKey) kp.getPublic(), 78 this.spec.getUris()); 79 } else { 80 pubKey = new RSAPublicKey((java.security.interfaces.RSAPublicKey) kp.getPublic()); 81 } 82 return new KeyPair(pubKey, kp.getPrivate()); 83 } catch (NoSuchAlgorithmException e) { 84 throw new JsdsiRuntimeException(e.toString(), e); 85 } catch (NoSuchProviderException e) { 86 throw new JsdsiRuntimeException(e.toString(), e); 87 } catch (InvalidAlgorithmParameterException e) { 88 throw new JsdsiRuntimeException(e.toString(), e); 89 } 90 } 91 92 private KeyPairGenerator getKeyPairGenerator() throws NoSuchAlgorithmException, 93 NoSuchProviderException, InvalidAlgorithmParameterException { 94 if (this.keyPairGenerator == null) { 95 if (this.spec != null) { 96 String provider = null; 97 if (this.spec.getProvider() != null 98 && !this.spec.getProvider().equals("")) { 99 provider = this.spec.getProvider(); 100 } 101 this.keyPairGenerator = (provider == null) ? KeyPairGenerator 102 .getInstance(ALGORITHM) : KeyPairGenerator.getInstance(ALGORITHM, provider); 103 if (this.spec.getKeysize() > 0 104 || this.spec.getPublicExponent() != null) { 105 this.keyPairGenerator.initialize(this.spec); 106 } 107 } else { 108 this.keyPairGenerator = KeyPairGenerator.getInstance(ALGORITHM); 109 if (this.keySize != null) { 110 this.keyPairGenerator.initialize(this.keySize.intValue()); 111 } 112 } 113 } 114 return this.keyPairGenerator; 115 } 116 117 }

This page was automatically generated by Maven