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

de.mibos.commons.crypt.CryptInputStream 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.CipherInputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * An input stream for the specified {@link de.mibos.commons.crypt.Crypt} which decrypts a cipher input stream.
* This is simply an extension to the standard {@link javax.crypto.CipherInputStream}, which * reads the initialization vector in the constructor. So both constructors may throw an {@link java.io.IOException}, if failing * to read the initialization vector.
* * @author Michael Bock * @version 1.5 * @since 1.3 * $Id: CryptInputStream.java 85 2014-12-04 23:44:38Z michaels-apps $ */ public class CryptInputStream extends FilterInputStream { /** * An input stream for the specified {@link de.mibos.commons.crypt.Crypt} which decrypts a cipher input stream.
* This is simply an extension to the standard {@link javax.crypto.CipherInputStream}, which * reads the initialization vector in the constructor. So this constructor may throw an {@link java.io.IOException}, if failing * to read the initialization vector.
* * @param crypt the used {@link de.mibos.commons.crypt.Crypt} object * @param input the cipher input stream * @param password the password * @throws IOException {@link java.io.IOException} if failing to read the initialization vector * @throws CryptoInitializationProblem in case of a {@link de.mibos.commons.crypt.CryptoInitializationProblem} problem */ public CryptInputStream(@Nonnull final Crypt crypt, @Nonnull final InputStream input, @Nonnull final CharSequence password) throws IOException { //noinspection ConstantConditions super(null); final byte[] passwordBytes = crypt.getHashedEncryptionKey(password); initCipher(crypt, input, passwordBytes); crypt.resetPasswordBytes(passwordBytes); } /** * An input stream for the specified {@link de.mibos.commons.crypt.Crypt} which decrypts a cipher input stream.
* This is simply an extension to the standard {@link javax.crypto.CipherInputStream}, which * reads the initialization vector in the constructor. So this constructor may throw an {@link java.io.IOException}, if failing * to read the initialization vector.
* * @param crypt the used {@link de.mibos.commons.crypt.Crypt} object * @param input the cipher input stream * @param passwordBytes the password bytes * @throws IOException {@link java.io.IOException} if failing to read the initialization vector * @throws CryptoInitializationProblem in case of a {@link de.mibos.commons.crypt.CryptoInitializationProblem} problem */ public CryptInputStream(@Nonnull final Crypt crypt, @Nonnull final InputStream input, @Nonnull final byte[] passwordBytes) throws IOException { //noinspection ConstantConditions super(null); initCipher(crypt, input, passwordBytes); } /** * Initializes the underlying {@link javax.crypto.CipherInputStream} * * @param crypt the used {@link de.mibos.commons.crypt.Crypt} object * @param input the cipher input stream * @param passwordBytes the password bytes * @throws IOException {@link java.io.IOException} if failing to read the initialization vector * @throws CryptoInitializationProblem in case of a {@link de.mibos.commons.crypt.CryptoInitializationProblem} problem */ private void initCipher(@Nonnull Crypt crypt, @Nonnull InputStream input, @Nonnull byte[] passwordBytes) throws IOException { final byte[] iv = crypt.getIV(input); final Cipher cipher = crypt.getDecryptionCipher(passwordBytes, iv); this.in = new CipherInputStream(input, cipher); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy