com.ovea.tajin.crypto.AesCipherService Maven / Gradle / Ivy
/**
* Copyright (C) 2011 Ovea
*
* Licensed 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 com.ovea.tajin.crypto;
import org.apache.commons.codec.binary.Base64;
/**
* {@code CipherService} using the {@code AES} cipher algorithm for all encryption, decryption, and key operations.
*
* The AES algorithm can support key sizes of {@code 128}, {@code 192} and {@code 256} bits*. This implementation
* defaults to 128 bits.
*
* Note that this class retains the parent class's default {@link OperationMode#CFB CFB} mode of operation
* instead of the typical JDK default of {@link OperationMode#ECB ECB}. {@code ECB} should not be used in
* security-sensitive environments because {@code ECB} does not allow for initialization vectors, which are
* considered necessary for strong encryption. See the {@link DefaultBlockCipherService parent class}'s JavaDoc and the
* {@link JcaCipherService JcaCipherService} JavaDoc for more on why the JDK default should not be used and is not
* used in this implementation.
*
* * Generating and using AES key sizes greater than 128 require installation of the
* Java Cryptography Extension (JCE) Unlimited Strength
* Jurisdiction Policy files.
*
* @since 1.0
*/
public class AesCipherService extends DefaultBlockCipherService {
private static final String ALGORITHM_NAME = "AES";
/**
* Creates a new {@link CipherService} instance using the {@code AES} cipher algorithm with the following
* important cipher default attributes:
*
*
* Attribute
* Value
*
*
* {@link #setKeySize keySize}
* {@code 128} bits
*
*
* {@link #setBlockSize blockSize}
* {@code 128} bits (required for {@code AES}
*
*
* {@link #setMode mode}
* {@link OperationMode#CFB CFB}*
*
*
* {@link #setPaddingScheme paddingScheme}
* {@link PaddingScheme#PKCS5 PKCS5}
*
*
* {@link #setInitializationVectorSize(int) initializationVectorSize}
* {@code 128} bits
*
*
* {@link #setGenerateInitializationVectors(boolean) generateInitializationVectors}
* {@code true}**
*
*
*
* * The {@link OperationMode#CFB CFB} operation mode is used instead of the JDK default {@code ECB} to
* ensure strong encryption. {@code ECB} should not be used in security-sensitive environments - see the
* {@link DefaultBlockCipherService DefaultBlockCipherService} class JavaDoc's "Operation Mode" section
* for more.
*
* **In conjunction with the default {@code CFB} operation mode, initialization vectors are generated by
* default to ensure strong encryption. See the {@link JcaCipherService JcaCipherService} class JavaDoc for more.
*/
public AesCipherService() {
super(ALGORITHM_NAME);
}
public static void main(String... args) throws Exception {
AesCipherService aes = new AesCipherService();
byte[] key = aes.generateNewKey().getEncoded();
String base64 = Base64.encodeBase64URLSafeString(key);
System.out.println(base64);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy