/** * Calls the {@link javax.crypto.Cipher#doFinal(byte[]) doFinal(bytes)} method, propagating any exception that * might arise in an {@link CryptoException} * * @param cipher the JDK Cipher to finalize (perform the actual cryption) * @param bytes the bytes to crypt * @return the resulting crypted byte array. * @throws CryptoException if there is an illegal block size or bad padding */ private byte[] crypt(javax.crypto.Cipher cipher, byte[] bytes) throws CryptoException { try { return cipher.doFinal(bytes); } catch (Exception e) { String msg = "Unable to execute 'doFinal' with cipher instance [" + cipher + "]."; throw new CryptoException(msg, e); } }
private void encrypt(InputStream in, OutputStream out, byte[] key, byte[] iv, boolean prependIv) throws CryptoException { if (prependIv && iv != null && iv.length > 0) { try { //first write the IV: out.write(iv); } catch (IOException e) { throw new CryptoException(e); } } crypt(in, out, key, iv, javax.crypto.Cipher.ENCRYPT_MODE); }
throw new CryptoException(msg, e);
private void decrypt(InputStream in, OutputStream out, byte[] key, boolean ivPrepended) throws CryptoException { byte[] iv = null; //No Initialization Vector provided as a method argument - check if we need to read the IV from the stream: if (ivPrepended) { //we are generating IVs, so we need to read the previously-generated IV from the stream before //we decrypt the rest of the stream (we need the IV to decrypt): int ivSize = getInitializationVectorSize(); int ivByteSize = ivSize / BITS_PER_BYTE; iv = new byte[ivByteSize]; int read; try { read = in.read(iv); } catch (IOException e) { String msg = "Unable to correctly read the Initialization Vector from the input stream."; throw new CryptoException(msg, e); } if (read != ivByteSize) { throw new CryptoException("Unable to read initialization vector bytes from the InputStream. " + "This is required when initialization vectors are autogenerated during an encryption " + "operation."); } } decrypt(in, out, key, iv); }
/** * Returns a new {@link javax.crypto.Cipher Cipher} instance to use for encryption/decryption operations. The * Cipher's {@code transformationString} for the {@code Cipher}.{@link javax.crypto.Cipher#getInstance getInstance} * call is obtaind via the {@link #getTransformationString(boolean) getTransformationString} method. * * @param streaming {@code true} if the cipher instance will be used as a stream cipher, {@code false} if it will be * used as a block cipher. * @return a new JDK {@code Cipher} instance. * @throws CryptoException if a new Cipher instance cannot be constructed based on the * {@link #getTransformationString(boolean) getTransformationString} value. */ private javax.crypto.Cipher newCipherInstance(boolean streaming) throws CryptoException { String transformationString = getTransformationString(streaming); try { return javax.crypto.Cipher.getInstance(transformationString); } catch (Exception e) { String msg = "Unable to acquire a Java JCA Cipher instance using " + javax.crypto.Cipher.class.getName() + ".getInstance( \"" + transformationString + "\" ). " + getAlgorithmName() + " under this configuration is required for the " + getClass().getName() + " instance to function."; throw new CryptoException(msg, e); } }
private void crypt(InputStream in, OutputStream out, byte[] keyBytes, byte[] iv, int cryptMode) throws CryptoException { if (in == null) { throw new NullPointerException("InputStream argument cannot be null."); } if (out == null) { throw new NullPointerException("OutputStream argument cannot be null."); } javax.crypto.Cipher cipher = initNewCipher(cryptMode, keyBytes, iv, true); CipherInputStream cis = new CipherInputStream(in, cipher); int bufSize = getStreamingBufferSize(); byte[] buffer = new byte[bufSize]; int bytesRead; try { while ((bytesRead = cis.read(buffer)) != -1) { out.write(buffer, 0, bytesRead); } } catch (IOException e) { throw new CryptoException(e); } }
public ByteSource decrypt(byte[] ciphertext, byte[] key) throws CryptoException { byte[] encrypted = ciphertext; //No IV, check if we need to read the IV from the stream: byte[] iv = null; if (isGenerateInitializationVectors(false)) { try { //We are generating IVs, so the ciphertext argument array is not actually 100% cipher text. Instead, it //is: // - the first N bytes is the initialization vector, where N equals the value of the // 'initializationVectorSize' attribute. // - the remaining bytes in the method argument (arg.length - N) is the real cipher text. //So we need to chunk the method argument into its constituent parts to find the IV and then use //the IV to decrypt the real ciphertext: int ivSize = getInitializationVectorSize(); int ivByteSize = ivSize / BITS_PER_BYTE; //now we know how large the iv is, so extract the iv bytes: iv = new byte[ivByteSize]; System.arraycopy(ciphertext, 0, iv, 0, ivByteSize); //remaining data is the actual encrypted ciphertext. Isolate it: int encryptedSize = ciphertext.length - ivByteSize; encrypted = new byte[encryptedSize]; System.arraycopy(ciphertext, ivByteSize, encrypted, 0, encryptedSize); } catch (Exception e) { String msg = "Unable to correctly extract the Initialization Vector or ciphertext."; throw new CryptoException(msg, e); } } return decrypt(encrypted, key, iv); }
/** * Calls the {@link javax.crypto.Cipher#doFinal(byte[]) doFinal(bytes)} method, propagating any exception that * might arise in an {@link CryptoException} * * @param cipher the JDK Cipher to finalize (perform the actual cryption) * @param bytes the bytes to crypt * @return the resulting crypted byte array. * @throws CryptoException if there is an illegal block size or bad padding */ private byte[] crypt(javax.crypto.Cipher cipher, byte[] bytes) throws CryptoException { try { return cipher.doFinal(bytes); } catch (Exception e) { String msg = "Unable to execute 'doFinal' with cipher instance [" + cipher + "]."; throw new CryptoException(msg, e); } }
private void encrypt(InputStream in, OutputStream out, byte[] key, byte[] iv, boolean prependIv) throws CryptoException { if (prependIv && iv != null && iv.length > 0) { try { //first write the IV: out.write(iv); } catch (IOException e) { throw new CryptoException(e); } } crypt(in, out, key, iv, javax.crypto.Cipher.ENCRYPT_MODE); }
throw new CryptoException(msg, e);
private void decrypt(InputStream in, OutputStream out, byte[] key, boolean ivPrepended) throws CryptoException { byte[] iv = null; //No Initialization Vector provided as a method argument - check if we need to read the IV from the stream: if (ivPrepended) { //we are generating IVs, so we need to read the previously-generated IV from the stream before //we decrypt the rest of the stream (we need the IV to decrypt): int ivSize = getInitializationVectorSize(); int ivByteSize = ivSize / BITS_PER_BYTE; iv = new byte[ivByteSize]; int read; try { read = in.read(iv); } catch (IOException e) { String msg = "Unable to correctly read the Initialization Vector from the input stream."; throw new CryptoException(msg, e); } if (read != ivByteSize) { throw new CryptoException("Unable to read initialization vector bytes from the InputStream. " + "This is required when initialization vectors are autogenerated during an encryption " + "operation."); } } decrypt(in, out, key, iv); }
/** * Returns a new {@link javax.crypto.Cipher Cipher} instance to use for encryption/decryption operations. The * Cipher's {@code transformationString} for the {@code Cipher}.{@link javax.crypto.Cipher#getInstance getInstance} * call is obtaind via the {@link #getTransformationString(boolean) getTransformationString} method. * * @param streaming {@code true} if the cipher instance will be used as a stream cipher, {@code false} if it will be * used as a block cipher. * @return a new JDK {@code Cipher} instance. * @throws CryptoException if a new Cipher instance cannot be constructed based on the * {@link #getTransformationString(boolean) getTransformationString} value. */ private javax.crypto.Cipher newCipherInstance(boolean streaming) throws CryptoException { String transformationString = getTransformationString(streaming); try { return javax.crypto.Cipher.getInstance(transformationString); } catch (Exception e) { String msg = "Unable to acquire a Java JCA Cipher instance using " + javax.crypto.Cipher.class.getName() + ".getInstance( \"" + transformationString + "\" ). " + getAlgorithmName() + " under this configuration is required for the " + getClass().getName() + " instance to function."; throw new CryptoException(msg, e); } }
private void crypt(InputStream in, OutputStream out, byte[] keyBytes, byte[] iv, int cryptMode) throws CryptoException { if (in == null) { throw new NullPointerException("InputStream argument cannot be null."); } if (out == null) { throw new NullPointerException("OutputStream argument cannot be null."); } javax.crypto.Cipher cipher = initNewCipher(cryptMode, keyBytes, iv, true); CipherInputStream cis = new CipherInputStream(in, cipher); int bufSize = getStreamingBufferSize(); byte[] buffer = new byte[bufSize]; int bytesRead; try { while ((bytesRead = cis.read(buffer)) != -1) { out.write(buffer, 0, bytesRead); } } catch (IOException e) { throw new CryptoException(e); } }
public ByteSource decrypt(byte[] ciphertext, byte[] key) throws CryptoException { byte[] encrypted = ciphertext; //No IV, check if we need to read the IV from the stream: byte[] iv = null; if (isGenerateInitializationVectors(false)) { try { //We are generating IVs, so the ciphertext argument array is not actually 100% cipher text. Instead, it //is: // - the first N bytes is the initialization vector, where N equals the value of the // 'initializationVectorSize' attribute. // - the remaining bytes in the method argument (arg.length - N) is the real cipher text. //So we need to chunk the method argument into its constituent parts to find the IV and then use //the IV to decrypt the real ciphertext: int ivSize = getInitializationVectorSize(); int ivByteSize = ivSize / BITS_PER_BYTE; //now we know how large the iv is, so extract the iv bytes: iv = new byte[ivByteSize]; System.arraycopy(ciphertext, 0, iv, 0, ivByteSize); //remaining data is the actual encrypted ciphertext. Isolate it: int encryptedSize = ciphertext.length - ivByteSize; encrypted = new byte[encryptedSize]; System.arraycopy(ciphertext, ivByteSize, encrypted, 0, encryptedSize); } catch (Exception e) { String msg = "Unable to correctly extract the Initialization Vector or ciphertext."; throw new CryptoException(msg, e); } } return decrypt(encrypted, key, iv); }