001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.shiro.crypto;
020
021/**
022 * {@code CipherService} using the {@code Blowfish} cipher algorithm for all encryption, decryption, and key operations.
023 * <p/>
024 * The Blowfish algorithm can support key sizes between {@code 32} and {@code 448} bits<b>*</b>, inclusive.  However,
025 * modern cryptanalysis techniques render keys of 80 bits or less mostly worthless - use {@code 128} or more whenever
026 * possible.
027 * <p/>
028 * Note that this class retains the parent class's default {@link OperationMode#CBC CBC} mode of operation
029 * instead of the typical JDK default of {@link OperationMode#ECB ECB}.  {@code ECB} should not be used in
030 * security-sensitive environments because {@code ECB} does not allow for initialization vectors, which are
031 * considered necessary for strong encryption.  See the {@link DefaultBlockCipherService parent class}'s JavaDoc and the
032 * {@link JcaCipherService JcaCipherService} JavaDoc for more on why the JDK default should not be used and is not
033 * used in this implementation.
034 * <p/>
035 * <b>*</b> Generating and using Blowfish key sizes greater than 128 require installation of the
036 * <a href="http://java.sun.com/javase/downloads/index.jsp">Java Cryptography Extension (JCE) Unlimited Strength
037 * Jurisdiction Policy files</a>.
038 *
039 * @since 1.0
040 */
041public class BlowfishCipherService extends DefaultBlockCipherService {
042
043    private static final String ALGORITHM_NAME = "Blowfish";
044    private static final int BLOCK_SIZE = 64;
045
046    /**
047     * Creates a new {@link CipherService} instance using the {@code Blowfish} cipher algorithm with the following
048     * important cipher default attributes:
049     * <table>
050     * <tr>
051     * <th>Attribute</th>
052     * <th>Value</th>
053     * </tr>
054     * <tr>
055     * <td>{@link #setKeySize keySize}</td>
056     * <td>{@code 128} bits</td>
057     * </tr>
058     * <tr>
059     * <td>{@link #setBlockSize blockSize}</td>
060     * <td>{@code 64} bits (required for {@code Blowfish})</td>
061     * </tr>
062     * <tr>
063     * <td>{@link #setMode mode}</td>
064     * <td>{@link OperationMode#CBC CBC}<b>*</b></td>
065     * </tr>
066     * <tr>
067     * <td>{@link #setPaddingScheme paddingScheme}</td>
068     * <td>{@link PaddingScheme#PKCS5 PKCS5}</td>
069     * </tr>
070     * <tr>
071     * <td>{@link #setInitializationVectorSize(int) initializationVectorSize}</td>
072     * <td>{@code 64} bits</td>
073     * </tr>
074     * <tr>
075     * <td>{@link #setGenerateInitializationVectors(boolean) generateInitializationVectors}</td>
076     * <td>{@code true}<b>**</b></td>
077     * </tr>
078     * </table>
079     * <p/>
080     * <b>*</b> The {@link OperationMode#CBC CBC} operation mode is used instead of the JDK default {@code ECB} to
081     * ensure strong encryption.  {@code ECB} should not be used in security-sensitive environments - see the
082     * {@link DefaultBlockCipherService DefaultBlockCipherService} class JavaDoc's &quot;Operation Mode&quot; section
083     * for more.
084     * <p/>
085     * <b>**</b>In conjunction with the default {@code CBC} operation mode, initialization vectors are generated by
086     * default to ensure strong encryption.  See the {@link JcaCipherService JcaCipherService} class JavaDoc for more.
087     */
088    public BlowfishCipherService() {
089        super(ALGORITHM_NAME);
090        setInitializationVectorSize(BLOCK_SIZE); //like most block ciphers, the IV size is the same as the block size
091    }
092}