org.nervousync.security.crypto.BaseCryptoAdapter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of utils-jdk11 Show documentation
Show all versions of utils-jdk11 Show documentation
Java utility collections, development by Nervousync Studio (NSYC)
/*
* Licensed to the Nervousync Studio (NSYC) 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 org.nervousync.security.crypto;
import org.nervousync.commons.Globals;
import org.nervousync.security.api.SecureAdapter;
import org.nervousync.security.config.CipherConfig;
import org.nervousync.enumerations.crypto.CryptoMode;
import org.nervousync.exceptions.crypto.CryptoException;
import org.nervousync.utils.SecurityUtils;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import java.security.Key;
/**
* Abstract basic crypto adapter class
* 加密解密适配器的抽象类
*
* @author Steven Wee [email protected]
* @version $Revision: 1.0.0 $ $Date: Jan 13, 2012 11:30:24 $
*/
public abstract class BaseCryptoAdapter extends SecureAdapter {
/**
* Cipher configure
* 密码设置
*/
protected final CipherConfig cipherConfig;
/**
* Crypto mode
* 加密解密模式
*/
protected final CryptoMode cryptoMode;
/**
* Crypto key
* 加密解密密钥
*/
protected final CipherKey cipherKey;
/**
* Cipher instance
* 加密解密实例对象
* The Cipher.
*/
protected Cipher cipher;
/**
* Constructor for BaseCryptoAdapter
* 加密解密适配器的构造方法
*
* @param cipherConfig Cipher configure
* 密码设置
* @param cryptoMode Crypto mode
* 加密解密模式
* @param cipherKey Crypto key
* 加密解密密钥
*/
protected BaseCryptoAdapter(final CipherConfig cipherConfig, final CryptoMode cryptoMode,
final CipherKey cipherKey) {
this.cipherConfig = cipherConfig;
this.cryptoMode = cryptoMode;
this.cipherKey = cipherKey;
}
/**
* Abstract method for initialize cipher instance
* 抽象方法用于初始化加密解密实例对象
*
* @return Initialized cipher instance
* 初始化的加密解密实例对象
*
* @throws CryptoException
* If an error occurs when initialize cipher
* 当初始化加密解密实例对象时出现异常
*/
protected abstract Cipher initCipher() throws CryptoException;
/**
* Generate cipher instance using given parameters
* 根据给定的参数信息初始化加密解密实例对象
*
* @param key Crypto key
* 加密解密密钥
* @param ivLength Length of IV data array
* 向量二进制数据的长度
*
* @return Generated cipher instance
* 生成的加密解密密钥
*
* @throws CryptoException
* If an error occurs when generate cipher
* 当生成的加密解密密钥时出现异常
*/
protected final Cipher generateCipher(final Key key, final int ivLength) throws CryptoException {
IvParameterSpec ivParameterSpec = null;
if (ivLength > 0) {
byte[] ivContent = new byte[ivLength];
System.arraycopy(SecurityUtils.SHA256(this.cipherKey.getKeyBytes()),
Globals.INITIALIZE_INT_VALUE, ivContent, Globals.INITIALIZE_INT_VALUE, ivContent.length);
ivParameterSpec = new IvParameterSpec(ivContent);
}
try {
Cipher cipherInstance = Cipher.getInstance(this.cipherConfig.toString(), "BC");
switch (this.cryptoMode) {
case ENCRYPT:
cipherInstance.init(Cipher.ENCRYPT_MODE, key, ivParameterSpec);
break;
case DECRYPT:
cipherInstance.init(Cipher.DECRYPT_MODE, key, ivParameterSpec);
break;
default:
throw new CryptoException(0x000000150009L, "Mode_Invalid_Crypto_Error");
}
return cipherInstance;
} catch (Exception e) {
if (e instanceof CryptoException) {
throw (CryptoException) e;
}
throw new CryptoException(0x00000015000BL, "Init_Cipher_Crypto_Error", e);
}
}
/**
* Cipher key define
* 加密解密密钥定义
*
* @author Steven Wee [email protected]
* @version $Revision: 1.0.0 $ $Date: Jan 13, 2012 11:32:08 $
*/
public static final class CipherKey {
/**
* Key size
* 密钥长度
*/
private final int keySize;
/**
* Key data bytes
* 密钥字节数组
*/
private final byte[] keyBytes;
/**
* Random algorithm
* 随机数算法
*/
private final String randomAlgorithm;
/**
* Asymmetric crypto key instance
* 非对称算法密钥实例对象
*/
private final Key key;
/**
* Constructor for CipherKey
* 加密解密密钥定义的构造方法
*
* @param keyBytes Key data bytes
* 密钥字节数组
*/
public CipherKey(final byte[] keyBytes) {
this(Globals.DEFAULT_VALUE_INT, keyBytes, Globals.DEFAULT_VALUE_STRING, null);
}
/**
* Constructor for CipherKey
* 加密解密密钥定义的构造方法
*
* @param keySize Key size
* 密钥长度
* @param keyBytes Key data bytes
* 密钥字节数组
* @param randomAlgorithm Random algorithm
* 随机数算法
*/
public CipherKey(final int keySize, final byte[] keyBytes, final String randomAlgorithm) {
this(keySize, keyBytes, randomAlgorithm, null);
}
/**
* Constructor for CipherKey
* 加密解密密钥定义的构造方法
*
* @param key Asymmetric crypto key instance
* 非对称算法密钥实例对象
*/
public CipherKey(final Key key) {
this(Globals.DEFAULT_VALUE_INT, new byte[0], Globals.DEFAULT_VALUE_STRING, key);
}
/**
* Constructor for CipherKey
* 加密解密密钥定义的构造方法
*
* @param keySize Key size
* 密钥长度
* @param keyBytes Key data bytes
* 密钥字节数组
* @param randomAlgorithm Random algorithm
* 随机数算法
* @param key Asymmetric crypto key instance
* 非对称算法密钥实例对象
*/
private CipherKey(final int keySize, final byte[] keyBytes, final String randomAlgorithm, final Key key) {
this.keySize = keySize;
this.keyBytes = keyBytes;
this.randomAlgorithm = randomAlgorithm;
this.key = key;
}
/**
* Getter method for Key size
* 密钥长度的Getter方法
*
* @return Key size
* 密钥长度
*/
public int getKeySize() {
return keySize;
}
/**
* Getter method for Key data bytes
* 密钥字节数组的Getter方法
*
* @return Key data bytes
* 密钥字节数组
*/
public byte[] getKeyBytes() {
return keyBytes;
}
/**
* Getter method for Random algorithm
* 随机数算法的Getter方法
*
* @return Random algorithm
* 随机数算法
*/
public String getRandomAlgorithm() {
return randomAlgorithm;
}
/**
* Getter method for Asymmetric crypto key instance
* 非对称算法密钥实例对象的Getter方法
*
* @return Asymmetric crypto key instance
* 非对称算法密钥实例对象
*/
public Key getKey() {
return key;
}
}
}