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

org.nervousync.security.crypto.SymmetricCryptoAdapter Maven / Gradle / Ivy

There is a newer version: 1.2.1
Show newest version
/*
 * 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.config.CipherConfig;
import org.nervousync.enumerations.crypto.CryptoMode;
import org.nervousync.exceptions.crypto.CryptoException;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import java.io.ByteArrayOutputStream;

/**
 * 

Abstract symmetric crypto adapter class

*

对称加密解密适配器的抽象类

* * @author Steven Wee [email protected] * @version $Revision: 1.0.0 $ $Date: Jan 13, 2012 13:16:24 $ */ public abstract class SymmetricCryptoAdapter extends BaseCryptoAdapter { /** * Result data bytes output stream * 结果数据二进制数组输出流 */ private ByteArrayOutputStream byteArrayOutputStream; /** *

Constructor for SymmetricCryptoAdapter

*

对称加密解密适配器的抽象类的构造方法

* * @param cipherConfig Cipher configure * 密码设置 * @param cryptoMode Crypto mode * 加密解密模式 * @param cipherKey Crypto key * 加密解密密钥 * * @throws CryptoException * If an error occurs when initialize cipher * 当初始化加密解密实例对象时出现异常 */ protected SymmetricCryptoAdapter(final CipherConfig cipherConfig, final CryptoMode cryptoMode, final CipherKey cipherKey) throws CryptoException { super(cipherConfig, cryptoMode, cipherKey); this.reset(); } /** *

Append parts of given binary data array to current adapter

*

追加给定的二进制字节数组到当前适配器

* * @param dataBytes binary data array * 二进制字节数组 * @param position Data begin position * 数据起始坐标 * @param length Length of data append * 追加的数据长度 * * @throws CryptoException * If an error occurs when process data * 当处理数据时出现异常 */ @Override public final void append(final byte[] dataBytes, final int position, final int length) throws CryptoException { if (dataBytes.length < (position + length)) { throw new CryptoException(0x000000150001L, "Length_Not_Enough_Crypto_Error"); } switch (this.cryptoMode) { case ENCRYPT: case DECRYPT: this.byteArrayOutputStream.write(dataBytes, position, length); break; default: throw new CryptoException(0x000000150003L, "Mode_Invalid_Crypto_Error"); } } /** *

Append parts of given binary data array to current adapter and calculate final result

*

追加给定的二进制字节数组到当前适配器并计算最终结果

* * @param dataBytes binary data array * 二进制字节数组 * @param position Data begin position * 数据起始坐标 * @param length Length of data append * 追加的数据长度 * * @return Calculate result data byte array * 计算的二进制字节数组结果 * * @throws CryptoException * If an error occurs when process data * 当处理数据时出现异常 */ @Override public final byte[] finish(final byte[] dataBytes, final int position, final int length) throws CryptoException { switch (this.cryptoMode) { case ENCRYPT: case DECRYPT: try { this.byteArrayOutputStream.write(dataBytes, position, length); return this.cipher.doFinal(this.byteArrayOutputStream.toByteArray(), Globals.INITIALIZE_INT_VALUE, this.byteArrayOutputStream.size()); } catch (IllegalBlockSizeException | BadPaddingException e) { throw new CryptoException(0x000000150004L, "Process_Data_Crypto_Error", e); } finally { this.reset(); } case SIGNATURE: case VERIFY: throw new CryptoException(0x00000015000CL, "Not_Support_Mode_Crypto_Error"); default: throw new CryptoException(0x000000150003L, "Mode_Invalid_Crypto_Error"); } } /** *

Verify given signature data bytes is valid

*

验证给定的签名二进制数据是合法的

* * @param signature signature data bytes * 签名二进制数据 * * @return Verify result * 验证结果 * * @throws CryptoException * If an error occurs when process data * 当处理数据时出现异常 */ @Override public final boolean verify(final byte[] signature) throws CryptoException { throw new CryptoException(0x00000015000CL, "Not_Support_Mode_Crypto_Error"); } /** *

Reset current adapter

*

重置当前适配器

* * @throws CryptoException * If an error occurs when process data * 当处理数据时出现异常 */ @Override public final void reset() throws CryptoException { switch (this.cryptoMode) { case ENCRYPT: case DECRYPT: this.cipher = this.initCipher(); this.byteArrayOutputStream = new ByteArrayOutputStream(); break; default: throw new CryptoException(0x000000150003L, "Mode_Invalid_Crypto_Error"); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy