/** * Generates a new {@link java.security.Key Key} suitable for this CipherService's {@link #getAlgorithmName() algorithm} * by calling {@link #generateNewKey(int) generateNewKey(128)} (uses a 128 bit size by default). * * @return a new {@link java.security.Key Key}, 128 bits in length. */ public Key generateNewKey() { return generateNewKey(getKeySize()); }
/** * Generates a new {@link Key Key} of the specified size suitable for this CipherService * (based on the {@link #getAlgorithmName() algorithmName} using the JDK {@link javax.crypto.KeyGenerator KeyGenerator}. * * @param keyBitSize the bit size of the key to create * @return the created key suitable for use with this CipherService */ public Key generateNewKey(int keyBitSize) { KeyGenerator kg; try { kg = KeyGenerator.getInstance(getAlgorithmName()); } catch (NoSuchAlgorithmException e) { String msg = "Unable to acquire " + getAlgorithmName() + " algorithm. This is required to function."; throw new IllegalStateException(msg, e); } kg.init(keyBitSize); return kg.generateKey(); }
/** * Overrides the parent implementation to ensure initialization vectors are always generated if streaming is * enabled (block ciphers <em>must</em> use initialization vectors if they are to be used as a stream cipher). If * not being used as a stream cipher, then the value is computed based on whether or not the currently configured * {@link #getModeName modeName} is compatible with initialization vectors as well as the result of the configured * {@link #setGenerateInitializationVectors(boolean) generateInitializationVectors} value. * * @param streaming whether or not streaming is being performed * @return {@code true} if streaming or a value computed based on if the currently configured mode is compatible * with initialization vectors. */ @Override protected boolean isGenerateInitializationVectors(boolean streaming) { return streaming || super.isGenerateInitializationVectors() && isModeInitializationVectorCompatible(getModeName()); }
@Override protected byte[] generateInitializationVector(boolean streaming) { if (streaming) { String streamingModeName = getStreamingModeName(); if (!isModeInitializationVectorCompatible(streamingModeName)) { String msg = "streamingMode attribute value [" + streamingModeName + "] does not support " + "Initialization Vectors. Ensure the streamingMode value represents an operation mode " + "that is compatible with initialization vectors."; throw new IllegalStateException(msg); } } else { String modeName = getModeName(); if (!isModeInitializationVectorCompatible(modeName)) { String msg = "mode attribute value [" + modeName + "] does not support " + "Initialization Vectors. Ensure the mode value represents an operation mode " + "that is compatible with initialization vectors."; throw new IllegalStateException(msg); } } return super.generateInitializationVector(streaming); } }
@Override protected byte[] generateInitializationVector(boolean streaming) { if (streaming) { String streamingModeName = getStreamingModeName(); if (!isModeInitializationVectorCompatible(streamingModeName)) { String msg = "streamingMode attribute value [" + streamingModeName + "] does not support " + "Initialization Vectors. Ensure the streamingMode value represents an operation mode " + "that is compatible with initialization vectors."; throw new IllegalStateException(msg); } } else { String modeName = getModeName(); if (!isModeInitializationVectorCompatible(modeName)) { String msg = "mode attribute value [" + modeName + "] does not support " + "Initialization Vectors. Ensure the mode value represents an operation mode " + "that is compatible with initialization vectors."; throw new IllegalStateException(msg); } } return super.generateInitializationVector(streaming); } }
/** * Generates a new {@link java.security.Key Key} suitable for this CipherService's {@link #getAlgorithmName() algorithm} * by calling {@link #generateNewKey(int) generateNewKey(128)} (uses a 128 bit size by default). * * @return a new {@link java.security.Key Key}, 128 bits in length. */ public Key generateNewKey() { return generateNewKey(getKeySize()); }
/** * Overrides the parent implementation to ensure initialization vectors are always generated if streaming is * enabled (block ciphers <em>must</em> use initialization vectors if they are to be used as a stream cipher). If * not being used as a stream cipher, then the value is computed based on whether or not the currently configured * {@link #getModeName modeName} is compatible with initialization vectors as well as the result of the configured * {@link #setGenerateInitializationVectors(boolean) generateInitializationVectors} value. * * @param streaming whether or not streaming is being performed * @return {@code true} if streaming or a value computed based on if the currently configured mode is compatible * with initialization vectors. */ @Override protected boolean isGenerateInitializationVectors(boolean streaming) { return streaming || super.isGenerateInitializationVectors() && isModeInitializationVectorCompatible(getModeName()); }
/** * Generates a new {@link Key Key} of the specified size suitable for this CipherService * (based on the {@link #getAlgorithmName() algorithmName} using the JDK {@link javax.crypto.KeyGenerator KeyGenerator}. * * @param keyBitSize the bit size of the key to create * @return the created key suitable for use with this CipherService */ public Key generateNewKey(int keyBitSize) { KeyGenerator kg; try { kg = KeyGenerator.getInstance(getAlgorithmName()); } catch (NoSuchAlgorithmException e) { String msg = "Unable to acquire " + getAlgorithmName() + " algorithm. This is required to function."; throw new IllegalStateException(msg, e); } kg.init(keyBitSize); return kg.generateKey(); }