1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied.  See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 module hunt.shiro.crypto.BlowfishCipherService;
20 
21 import hunt.shiro.crypto.DefaultBlockCipherService;
22 
23 /**
24  * {@code CipherService} using the {@code Blowfish} cipher algorithm for all encryption, decryption, and key operations.
25  * <p/>
26  * The Blowfish algorithm can support key sizes between {@code 32} and {@code 448} bits<b>*</b>, inclusive.  However,
27  * modern cryptanalysis techniques render keys of 80 bits or less mostly worthless - use {@code 128} or more whenever
28  * possible.
29  * <p/>
30  * Note that this class retains the parent class's default {@link OperationMode#CBC CBC} mode of operation
31  * instead of the typical JDK default of {@link OperationMode#ECB ECB}.  {@code ECB} should not be used in
32  * security-sensitive environments because {@code ECB} does not allow for initialization vectors, which are
33  * considered necessary for strong encryption.  See the {@link DefaultBlockCipherService parent class}'s JavaDoc and the
34  * {@link JcaCipherService JcaCipherService} JavaDoc for more on why the JDK default should not be used and is not
35  * used in this implementation.
36  * <p/>
37  * <b>*</b> Generating and using Blowfish key sizes greater than 128 require installation of the
38  * <a href="http://java.sun.com/javase/downloads/index.jsp">Java Cryptography Extension (JCE) Unlimited Strength
39  * Jurisdiction Policy files</a>.
40  *
41  * @since 1.0
42  */
43 class BlowfishCipherService : DefaultBlockCipherService {
44 
45     private enum string ALGORITHM_NAME = "Blowfish";
46     private enum int BLOCK_SIZE = 64;
47 
48     /**
49      * Creates a new {@link CipherService} instance using the {@code Blowfish} cipher algorithm with the following
50      * important cipher default attributes:
51      * <table>
52      * <tr>
53      * <th>Attribute</th>
54      * <th>Value</th>
55      * </tr>
56      * <tr>
57      * <td>{@link #setKeySize keySize}</td>
58      * <td>{@code 128} bits</td>
59      * </tr>
60      * <tr>
61      * <td>{@link #setBlockSize blockSize}</td>
62      * <td>{@code 64} bits (required for {@code Blowfish})</td>
63      * </tr>
64      * <tr>
65      * <td>{@link #setMode mode}</td>
66      * <td>{@link OperationMode#CBC CBC}<b>*</b></td>
67      * </tr>
68      * <tr>
69      * <td>{@link #setPaddingScheme paddingScheme}</td>
70      * <td>{@link PaddingScheme#PKCS5 PKCS5}</td>
71      * </tr>
72      * <tr>
73      * <td>{@link #setInitializationVectorSize(int) initializationVectorSize}</td>
74      * <td>{@code 64} bits</td>
75      * </tr>
76      * <tr>
77      * <td>{@link #setGenerateInitializationVectors(boolean) generateInitializationVectors}</td>
78      * <td>{@code true}<b>**</b></td>
79      * </tr>
80      * </table>
81      * <p/>
82      * <b>*</b> The {@link OperationMode#CBC CBC} operation mode is used instead of the JDK default {@code ECB} to
83      * ensure strong encryption.  {@code ECB} should not be used in security-sensitive environments - see the
84      * {@link DefaultBlockCipherService DefaultBlockCipherService} class JavaDoc's &quot;Operation Mode&quot; section
85      * for more.
86      * <p/>
87      * <b>**</b>In conjunction with the default {@code CBC} operation mode, initialization vectors are generated by
88      * default to ensure strong encryption.  See the {@link JcaCipherService JcaCipherService} class JavaDoc for more.
89      */
90     this() {
91         super(ALGORITHM_NAME);
92         setInitializationVectorSize(BLOCK_SIZE); //like most block ciphers, the IV size is the same as the block size
93     }
94 }