/** * Gets the IV being used. Only works with algorithms that support IVs. * @return Returns the iv as a IvParameterSpec */ public final IvParameterSpec getIv() { if(type.ivlen == -1){ throw new UnsupportedTypeException(type); } return iv; }
/** * Changes the current iv to the provided iv. Only works with algorithms that support IVs. * @param iv The new iv to use as IvParameterSpec * @throws InvalidAlgorithmParameterException */ public final void setIV(IvParameterSpec iv) throws InvalidAlgorithmParameterException{ if(type.ivlen == -1){ throw new UnsupportedTypeException(type); } this.iv = iv; try { mac.init(key, iv); } catch (InvalidKeyException e) { throw new IllegalArgumentException(e); // Definitely a bug ... } }
/** * Checks to make sure the provided key is a valid Poly1305 key * @param encodedKey Key to check */ private final void checkPoly1305Key(byte[] encodedKey){ if(type != MACType.Poly1305AES){ throw new UnsupportedTypeException(type); } Poly1305KeyGenerator.checkKey(encodedKey); }
/** * Gets the IV being used. Only works with algorithms that support IVs. * @return Returns the iv as a IvParameterSpec */ public IvParameterSpec getIV(){ if(!type.hasIV()){ throw new UnsupportedTypeException(type); } return iv; } }
/** * Changes the current iv to the provided iv and initializes the cipher instances with * the new iv. Only works with algorithms that support IVs, not RijndaelPCFB. * @param iv The new iv to use as IvParameterSpec * @throws InvalidAlgorithmParameterException */ public void setIV(IvParameterSpec iv) throws InvalidAlgorithmParameterException{ if(!type.hasIV()){ throw new UnsupportedTypeException(type); } this.iv = iv; try { encryptCipher.init(Cipher.ENCRYPT_MODE, this.key, this.iv); decryptCipher.init(Cipher.DECRYPT_MODE, this.key, this.iv); } catch (InvalidKeyException e) { throw new IllegalArgumentException(e); } }
/** * Generates a new IV to be used and initializes the cipher instances with * the new iv. Only works with algorithms that support IVs, not RijndaelPCFB. * @return The generated IV */ public IvParameterSpec genIV(){ if(!type.hasIV()){ throw new UnsupportedTypeException(type); } this.iv = KeyGenUtils.genIV(type.ivSize); try { encryptCipher.init(Cipher.ENCRYPT_MODE, this.key, this.iv); decryptCipher.init(Cipher.DECRYPT_MODE, this.key, this.iv); } catch (InvalidKeyException e) { throw new IllegalArgumentException(e); // Definitely a bug ... } catch (InvalidAlgorithmParameterException e) { throw new IllegalArgumentException(e); // Definitely a bug ... } return iv; }
/** * Converts a specified key for a specified algorithm to a PublicKey. Can not handle DSA keys. * @param type The type of key being passed in * @param pub Public key as byte[] * @return Public key as PublicKey */ public static PublicKey getPublicKey(KeyPairType type, byte[] pub){ if(type.equals(KeyPairType.DSA)){ throw new UnsupportedTypeException(type); } try { KeyFactory kf; if (isJava7()) { kf = KeyFactory.getInstance(type.alg, bcProvider); } else { kf = KeyFactory.getInstance(type.alg); } X509EncodedKeySpec xks = new X509EncodedKeySpec(pub); return kf.generatePublic(xks); } catch (NoSuchAlgorithmException e) { throw new Error(e); // Impossible? } catch (InvalidKeySpecException e) { throw new IllegalArgumentException(e); // Indicates passed in key is bogus } }
/** * Generates a new IV to be used. Only works with algorithms that support IVs. * @return The generated IV */ public final IvParameterSpec genIV() { if(type.ivlen == -1){ throw new UnsupportedTypeException(type); } try { setIV(KeyGenUtils.genIV(type.ivlen)); } catch (InvalidAlgorithmParameterException e) { throw new IllegalArgumentException(e); // Definitely a bug ... } return this.iv; } }
/** * Generates a public/private key pair formatted for the algorithm specified * and stores the keys in a KeyPair. Can not handle DSA keys. * @param type The algorithm format that the key pair should be generated for. * @return Returns the generated key pair */ public static KeyPair genKeyPair(KeyPairType type) { if(type.equals(KeyPairType.DSA)){ throw new UnsupportedTypeException(type); } try { KeyPairGenerator kg; if (isJava7()) { kg = KeyPairGenerator.getInstance(type.alg, bcProvider); } else { kg = KeyPairGenerator.getInstance(type.alg); } kg.initialize(type.spec); return kg.generateKeyPair(); } catch (NoSuchAlgorithmException e) { throw new Error(e); // Impossible? } catch (InvalidAlgorithmParameterException e) { throw new Error(e); // Impossible? } }
throw new UnsupportedTypeException(type);
throws InvalidKeyException, InvalidAlgorithmParameterException{ if(iv != null && !type.hasIV()){ throw new UnsupportedTypeException(type, "This type does not take an IV.");