use of cn.hutool.crypto.CryptoException in project hutool by looly.
the class SymmetricCrypto method encrypt.
@Override
public void encrypt(InputStream data, OutputStream out, boolean isClose) throws IORuntimeException {
lock.lock();
CipherOutputStream cipherOutputStream = null;
try {
final Cipher cipher = initMode(Cipher.ENCRYPT_MODE);
cipherOutputStream = new CipherOutputStream(out, cipher);
long length = IoUtil.copy(data, cipherOutputStream);
if (this.isZeroPadding) {
final int blockSize = cipher.getBlockSize();
if (blockSize > 0) {
// 按照块拆分后的数据中多余的数据
final int remainLength = (int) (length % blockSize);
if (remainLength > 0) {
// 补充0
cipherOutputStream.write(new byte[blockSize - remainLength]);
cipherOutputStream.flush();
}
}
}
} catch (IORuntimeException e) {
throw e;
} catch (Exception e) {
throw new CryptoException(e);
} finally {
lock.unlock();
// issue#I4EMST@Gitee
// CipherOutputStream必须关闭,才能完全写出
IoUtil.close(cipherOutputStream);
if (isClose) {
IoUtil.close(data);
}
}
}
use of cn.hutool.crypto.CryptoException in project hutool by looly.
the class SymmetricCrypto method decrypt.
// --------------------------------------------------------------------------------- Decrypt
@Override
public byte[] decrypt(byte[] bytes) {
final int blockSize;
final byte[] decryptData;
lock.lock();
try {
final Cipher cipher = initMode(Cipher.DECRYPT_MODE);
blockSize = cipher.getBlockSize();
decryptData = cipher.doFinal(bytes);
} catch (Exception e) {
throw new CryptoException(e);
} finally {
lock.unlock();
}
return removePadding(decryptData, blockSize);
}
use of cn.hutool.crypto.CryptoException in project hutool by looly.
the class SM2 method encrypt.
/**
* 加密,SM2非对称加密的结果由C1,C2,C3三部分组成,其中:
*
* <pre>
* C1 生成随机数的计算出的椭圆曲线点
* C2 密文数据
* C3 SM3的摘要值
* </pre>
*
* @param data 被加密的bytes
* @param pubKeyParameters 公钥参数
* @return 加密后的bytes
* @throws CryptoException 包括InvalidKeyException和InvalidCipherTextException的包装异常
* @since 5.1.6
*/
public byte[] encrypt(byte[] data, CipherParameters pubKeyParameters) throws CryptoException {
lock.lock();
final SM2Engine engine = getEngine();
try {
engine.init(true, pubKeyParameters);
return engine.processBlock(data, 0, data.length);
} catch (InvalidCipherTextException e) {
throw new CryptoException(e);
} finally {
lock.unlock();
}
}
use of cn.hutool.crypto.CryptoException in project hutool by looly.
the class Sign method verify.
/**
* 用公钥检验数字签名的合法性
*
* @param data 数据
* @param sign 签名
* @return 是否验证通过
*/
public boolean verify(byte[] data, byte[] sign) {
lock.lock();
try {
signature.initVerify(this.publicKey);
signature.update(data);
return signature.verify(sign);
} catch (Exception e) {
throw new CryptoException(e);
} finally {
lock.unlock();
}
}
use of cn.hutool.crypto.CryptoException in project hutool by looly.
the class Sign method sign.
/**
* 生成签名
*
* @param data {@link InputStream} 数据流
* @param bufferLength 缓存长度,不足1使用 {@link IoUtil#DEFAULT_BUFFER_SIZE} 做为默认值
* @return 签名bytes
* @since 5.7.0
*/
public byte[] sign(InputStream data, int bufferLength) {
if (bufferLength < 1) {
bufferLength = IoUtil.DEFAULT_BUFFER_SIZE;
}
final byte[] buffer = new byte[bufferLength];
lock.lock();
try {
signature.initSign(this.privateKey);
byte[] result;
try {
int read = data.read(buffer, 0, bufferLength);
while (read > -1) {
signature.update(buffer, 0, read);
read = data.read(buffer, 0, bufferLength);
}
result = signature.sign();
} catch (Exception e) {
throw new CryptoException(e);
}
return result;
} catch (Exception e) {
throw new CryptoException(e);
} finally {
lock.unlock();
}
}
Aggregations