All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.mibos.commons.crypt.CryptOutputStream Maven / Gradle / Ivy

/*
 * Copyright 2014 Michael Bock
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.mibos.commons.crypt;

import javax.annotation.Nonnull;
import javax.crypto.Cipher;
import javax.crypto.CipherOutputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * An output stream for the specified {@link de.mibos.commons.crypt.Crypt}.
 * This is simply an extension to the standard {@link javax.crypto.CipherOutputStream}, which
 * writes the initialization vector in the constructor. So both constructors may throw an {@link java.io.IOException}, if failing
 * to write the initialization vector.
 * Please ensure that close is always called explicitly for this stream object. Failing to do so will result in
 * missing cipher text bytes.
 *
 * @author Michael Bock
 * @version 1.5
 * @since 1.3
 * Id: CryptOutputStream.java 45 2014-09-20 19:04:30Z michaels-apps $
 */
public class CryptOutputStream extends FilterOutputStream {

    /**
     * An output stream for the specified {@link de.mibos.commons.crypt.Crypt}.
     * This is simply an extension to the standard {@link javax.crypto.CipherOutputStream}, which
     * writes the initialization vector in the constructor. So this constructor may throw an {@link java.io.IOException}, if failing
     * to write the initialization vector.
*

* In contrast to the {@link javax.crypto.CipherOutputStream} it only supports encrypting a plain text, not decrypting a cipher text.
*

* Please ensure that close is always called explicitly for this stream object. Failing to do so will result in * missing cipher text bytes. * * @param crypt the used {@link de.mibos.commons.crypt.Crypt} object * @param output the cipher output stream * @param password the password * @throws IOException {@link java.io.IOException} if failing to write the initialization vector * @throws CryptoInitializationProblem in case of a {@link de.mibos.commons.crypt.CryptoInitializationProblem} problem */ public CryptOutputStream(@Nonnull final Crypt crypt, @Nonnull final OutputStream output, @Nonnull final CharSequence password) throws IOException { //noinspection ConstantConditions super(null); final byte[] passwordBytes = crypt.getHashedEncryptionKey(password); initCipher(crypt, output, passwordBytes); crypt.resetPasswordBytes(passwordBytes); } /** * An output stream for the specified {@link de.mibos.commons.crypt.Crypt}. * This is simply an extension to the standard {@link javax.crypto.CipherOutputStream}, which * writes the initialization vector in the constructor. So this constructor may throw an {@link java.io.IOException}, if failing * to write the initialization vector. * Please ensure that close is always called explicitly for this stream object. Failing to do so will result in * missing cipher text bytes. * * @param crypt the used {@link de.mibos.commons.crypt.Crypt} object * @param output the cipher output stream * @param passwordBytes the password bytes * @throws IOException {@link java.io.IOException} if failing to write the initialization vector * @throws CryptoInitializationProblem in case of a {@link de.mibos.commons.crypt.CryptoInitializationProblem} problem */ public CryptOutputStream(@Nonnull final Crypt crypt, @Nonnull final OutputStream output, @Nonnull final byte[] passwordBytes) throws IOException { //noinspection ConstantConditions super(null); initCipher(crypt, output, passwordBytes); } /** * Initializes the underlying {@link javax.crypto.CipherOutputStream} * * @param crypt the used {@link de.mibos.commons.crypt.Crypt} object * @param output the cipher output stream * @param passwordBytes the password bytes * @throws IOException {@link java.io.IOException} if failing to write the initialization vector * @throws CryptoInitializationProblem in case of a {@link de.mibos.commons.crypt.CryptoInitializationProblem} problem */ private void initCipher(@Nonnull Crypt crypt, @Nonnull OutputStream output, @Nonnull byte[] passwordBytes) throws IOException { final Cipher cipher = crypt.getEncryptionCipher(passwordBytes); final byte[] iv = cipher.getIV(); output.write(iv); this.out = new CipherOutputStream(output, cipher); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy