use of cn.hutool.crypto.CryptoException in project hutool by looly.
the class RSA method encryptStr.
/**
* 分组加密
*
* @param data 数据
* @param keyType 密钥类型
* @param charset 加密前编码
* @return 加密后的密文
* @throws CryptoException 加密异常
* @since 3.1.1
*/
public String encryptStr(String data, KeyType keyType, Charset charset) {
Key key = getKeyByType(keyType);
// 加密数据长度 <= 模长-11
int maxBlockSize = ((RSAKey) key).getModulus().bitLength() / 8 - 11;
final byte[] dataBytes = StrUtil.bytes(data, charset);
final int inputLen = dataBytes.length;
lock.lock();
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
clipher.init(Cipher.ENCRYPT_MODE, key);
int offSet = 0;
byte[] cache;
// 剩余长度
int remainLength = inputLen;
// 对数据分段加密
while (remainLength > 0) {
cache = clipher.doFinal(dataBytes, offSet, Math.min(remainLength, maxBlockSize));
out.write(cache, 0, cache.length);
offSet += maxBlockSize;
remainLength = inputLen - offSet;
}
return BCD.bcdToStr(out.toByteArray());
} catch (Exception e) {
throw new CryptoException(e);
} finally {
lock.unlock();
}
}
use of cn.hutool.crypto.CryptoException in project hutool by looly.
the class RSA method decryptStr.
/**
* 分组解密
*
* @param data 数据
* @param keyType 密钥类型
* @param charset 加密前编码
* @return 解密后的密文
* @since 3.1.1
*/
public String decryptStr(String data, KeyType keyType, Charset charset) {
final Key key = getKeyByType(keyType);
// 模长
final int maxBlockSize = ((RSAKey) key).getModulus().bitLength() / 8;
byte[] dataBytes = BCD.ascToBcd(StrUtil.bytes(data, charset));
int inputLen = dataBytes.length;
lock.lock();
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
clipher.init(Cipher.DECRYPT_MODE, key);
int offSet = 0;
byte[] cache;
// 剩余长度
int remainLength = inputLen;
// 对数据分段解密
while (remainLength > 0) {
cache = clipher.doFinal(dataBytes, offSet, Math.min(remainLength, maxBlockSize));
out.write(cache, 0, cache.length);
offSet += maxBlockSize;
remainLength = inputLen - offSet;
}
return StrUtil.str(out.toByteArray(), charset);
} catch (Exception e) {
throw new CryptoException(e);
} finally {
lock.unlock();
}
}
use of cn.hutool.crypto.CryptoException in project hutool by looly.
the class HMac method digest.
/**
* 生成摘要
*
* @param data {@link InputStream} 数据流
* @param bufferLength 缓存长度,不足1使用 {@link IoUtil#DEFAULT_BUFFER_SIZE} 做为默认值
* @return 摘要bytes
*/
public byte[] digest(InputStream data, int bufferLength) {
if (bufferLength < 1) {
bufferLength = IoUtil.DEFAULT_BUFFER_SIZE;
}
byte[] buffer = new byte[bufferLength];
byte[] result = null;
try {
int read = data.read(buffer, 0, bufferLength);
while (read > -1) {
mac.update(buffer, 0, read);
read = data.read(buffer, 0, bufferLength);
}
result = mac.doFinal();
} catch (IOException e) {
throw new CryptoException(e);
} finally {
mac.reset();
}
return result;
}
use of cn.hutool.crypto.CryptoException in project hutool by looly.
the class AsymmetricCrypto method encrypt.
// --------------------------------------------------------------------------------- Encrypt
@Override
public byte[] encrypt(byte[] data, KeyType keyType) {
final Key key = getKeyByType(keyType);
lock.lock();
try {
final Cipher cipher = initMode(Cipher.ENCRYPT_MODE, key);
if (this.encryptBlockSize < 0) {
// 在引入BC库情况下,自动获取块大小
final int blockSize = cipher.getBlockSize();
if (blockSize > 0) {
this.encryptBlockSize = blockSize;
}
}
return doFinal(data, this.encryptBlockSize < 0 ? data.length : this.encryptBlockSize);
} catch (Exception e) {
throw new CryptoException(e);
} finally {
lock.unlock();
}
}
use of cn.hutool.crypto.CryptoException in project hutool by looly.
the class AsymmetricCrypto method decrypt.
// --------------------------------------------------------------------------------- Decrypt
@Override
public byte[] decrypt(byte[] data, KeyType keyType) {
final Key key = getKeyByType(keyType);
lock.lock();
try {
final Cipher cipher = initMode(Cipher.DECRYPT_MODE, key);
if (this.decryptBlockSize < 0) {
// 在引入BC库情况下,自动获取块大小
final int blockSize = cipher.getBlockSize();
if (blockSize > 0) {
this.decryptBlockSize = blockSize;
}
}
return doFinal(data, this.decryptBlockSize < 0 ? data.length : this.decryptBlockSize);
} catch (Exception e) {
throw new CryptoException(e);
} finally {
lock.unlock();
}
}
Aggregations