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

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

The newest version!
/*
 * 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.ByteArrayInputStream;
import java.io.FilterInputStream;
import java.io.InputStream;
import java.io.SequenceInputStream;

/**
 * An input stream for the specified {@link Crypt} which encrypts a plaintext input stream.
* This is simply an extension to the standard {@link javax.crypto.CipherInputStream}, which additionally * provides the initialization vector as first input.
* * @author Michael Bock * @version 1.5 * @since 1.5 * $Id: PlainInputStream.java 88 2014-12-05 00:00:29Z michaels-apps $ */ public class PlainInputStream extends FilterInputStream { /** * An input stream for the specified {@link Crypt} which encrypts a plaintext input stream.
* This is simply an extension to the standard {@link javax.crypto.CipherInputStream}, which additionally * provides the initialization vector as first input.
* * @param crypt the used {@link Crypt} object * @param input the cipher input stream * @param password the password * @throws de.mibos.commons.crypt.CryptoInitializationProblem in case of a {@link CryptoInitializationProblem} problem */ public PlainInputStream(@Nonnull final Crypt crypt, @Nonnull final InputStream input, @Nonnull final CharSequence password) { //noinspection ConstantConditions super(null); final byte[] passwordBytes = crypt.getHashedEncryptionKey(password); initCipher(crypt, input, passwordBytes); crypt.resetPasswordBytes(passwordBytes); } /** * An input stream for the specified {@link Crypt} which encrypts a plaintext input stream.
* This is simply an extension to the standard {@link javax.crypto.CipherInputStream}, which additionally * provides the initialization vector as first input.
* * @param crypt the used {@link Crypt} object * @param input the cipher input stream * @param passwordBytes the password bytes * @throws de.mibos.commons.crypt.CryptoInitializationProblem in case of a {@link CryptoInitializationProblem} problem */ public PlainInputStream(@Nonnull final Crypt crypt, @Nonnull final InputStream input, @Nonnull final byte[] passwordBytes) { //noinspection ConstantConditions super(null); initCipher(crypt, input, passwordBytes); } /** * Initializes the underlying {@link javax.crypto.CipherInputStream} * * @param crypt the used {@link Crypt} object * @param input the cipher input stream * @param passwordBytes the password bytes * @throws de.mibos.commons.crypt.CryptoInitializationProblem in case of a {@link CryptoInitializationProblem} problem */ private void initCipher(@Nonnull Crypt crypt, @Nonnull InputStream input, @Nonnull byte[] passwordBytes) { final Cipher cipher = crypt.getEncryptionCipher(passwordBytes); final byte[] iv = cipher.getIV(); final ByteArrayInputStream ivInputStream = new ByteArrayInputStream(iv); this.in = new SequenceInputStream(ivInputStream, new CipherInputStream(input, cipher)); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy