示例#1
0
 /**
  * Zero argument constructor for generation a key pair There will be a public key and a private
  * key available
  *
  * @throws KOAException This exception will be thrown when there is a problem with generation a
  *     key pair
  */
 public KOAKeyPair() throws KOAException {
   try {
     KEYPAIR_KEY_LENGTH = TechnicalProps.getIntProperty(TechnicalProps.RSA_KEY_LENGTH);
     KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEYPAIR_GENERATOR_ALGORITHM);
     keyPairGen.initialize(KEYPAIR_KEY_LENGTH);
     keyPair = keyPairGen.generateKeyPair();
   } catch (NoSuchAlgorithmException nsae) {
     // KOALogHelper.logError ("KOAKeyPair", "Cannot generate key pair", nsae);
     throw new KOAException(ErrorConstants.SECURITY_KEYPAIR_GENERATE, nsae);
   }
 }
示例#2
0
 /**
  * Constructor for decryption of a key. Only the public of private key of the <code>keyType</code>
  * will be available the other will return null
  *
  * @param password The password used for decryption
  * @param criptKey A stream with the encrypted key
  * @param keyType The type of the key public (<code>PUBLIC_KEY</code>) or private (<code>
  *     PRIVATE_KEY</code>)
  * @throws KOAException This exception will be thrown when there is a problem with decryption
  */
 public KOAKeyPair(String password, InputStream cryptKey, int keyType) throws KOAException {
   try {
     KEYPAIR_KEY_LENGTH = TechnicalProps.getIntProperty(TechnicalProps.RSA_KEY_LENGTH);
     PBEParameterSpec paramSpec = new PBEParameterSpec(SALT, 20);
     PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray());
     SecretKeyFactory kf = SecretKeyFactory.getInstance(KEY_ENCRIPTION_AKGORITHM);
     SecretKey passwordKey = kf.generateSecret(keySpec);
     Cipher cipher = Cipher.getInstance(KEY_ENCRIPTION_AKGORITHM);
     cipher.init(Cipher.UNWRAP_MODE, passwordKey, paramSpec);
     byte[] dummy = new byte[128];
     int length;
     ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
     while ((length = cryptKey.read(dummy)) != -1) {
       byteArray.write(dummy, 0, length);
     }
     if (keyType == PRIVATE_KEY) {
       Key unwrappedKey =
           cipher.unwrap(byteArray.toByteArray(), KEYPAIR_GENERATOR_ALGORITHM, keyType);
       keyPair = new KeyPair(null, (PrivateKey) unwrappedKey);
     } else if (keyType == PUBLIC_KEY) {
       Key unwrappedKey =
           cipher.unwrap(byteArray.toByteArray(), KEYPAIR_GENERATOR_ALGORITHM, keyType);
       keyPair = new KeyPair((PublicKey) unwrappedKey, null);
     } else {
       throw new InvalidKeyException("criptKey does not represent a wrapped key of type keyType");
     }
   } catch (NoSuchAlgorithmException nsae) {
     KOALogHelper.logError("KOAKeyPair", "Cannot decrypt key pair", nsae);
     throw new KOAException(ErrorConstants.SECURITY_KEYPAIR_DECRYPT, nsae);
   } catch (NoSuchPaddingException nspe) {
     KOALogHelper.logError("KOAKeyPair", "Cannot decrypt key pair", nspe);
     throw new KOAException(ErrorConstants.SECURITY_KEYPAIR_DECRYPT, nspe);
   } catch (InvalidKeySpecException ikse) {
     KOALogHelper.logError("KOAKeyPair", "Cannot decrypt key pair", ikse);
     throw new KOAException(ErrorConstants.SECURITY_KEYPAIR_DECRYPT, ikse);
   } catch (InvalidKeyException ike) {
     KOALogHelper.logError("KOAKeyPair", "Cannot decrypt key pair", ike);
     throw new KOAException(ErrorConstants.SECURITY_KEYPAIR_DECRYPT, ike);
   } catch (InvalidAlgorithmParameterException iape) {
     KOALogHelper.logError("KOAKeyPair", "Cannot decrypt key pair", iape);
     throw new KOAException(ErrorConstants.SECURITY_KEYPAIR_DECRYPT, iape);
   } catch (IOException ioe) {
     KOALogHelper.logError("KOAKeyPair", "Cannot decrypt key pair", ioe);
     throw new KOAException(ErrorConstants.SECURITY_KEYPAIR_DECRYPT, ioe);
   }
 }