Search in sources :

Example 1 with CryptoException

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();
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) CryptoException(cn.hutool.crypto.CryptoException) RSAKey(java.security.interfaces.RSAKey) PublicKey(java.security.PublicKey) Key(java.security.Key) PrivateKey(java.security.PrivateKey) CryptoException(cn.hutool.crypto.CryptoException)

Example 2 with CryptoException

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();
    }
}
Also used : RSAKey(java.security.interfaces.RSAKey) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CryptoException(cn.hutool.crypto.CryptoException) RSAKey(java.security.interfaces.RSAKey) PublicKey(java.security.PublicKey) Key(java.security.Key) PrivateKey(java.security.PrivateKey) CryptoException(cn.hutool.crypto.CryptoException)

Example 3 with CryptoException

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;
}
Also used : IOException(java.io.IOException) CryptoException(cn.hutool.crypto.CryptoException)

Example 4 with CryptoException

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();
    }
}
Also used : Cipher(javax.crypto.Cipher) CryptoException(cn.hutool.crypto.CryptoException) PublicKey(java.security.PublicKey) Key(java.security.Key) PrivateKey(java.security.PrivateKey) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) IOException(java.io.IOException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) BadPaddingException(javax.crypto.BadPaddingException) CryptoException(cn.hutool.crypto.CryptoException) InvalidKeyException(java.security.InvalidKeyException)

Example 5 with CryptoException

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();
    }
}
Also used : Cipher(javax.crypto.Cipher) CryptoException(cn.hutool.crypto.CryptoException) PublicKey(java.security.PublicKey) Key(java.security.Key) PrivateKey(java.security.PrivateKey) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) IOException(java.io.IOException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) BadPaddingException(javax.crypto.BadPaddingException) CryptoException(cn.hutool.crypto.CryptoException) InvalidKeyException(java.security.InvalidKeyException)

Aggregations

CryptoException (cn.hutool.crypto.CryptoException)20 IOException (java.io.IOException)10 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)8 InvalidKeyException (java.security.InvalidKeyException)6 Cipher (javax.crypto.Cipher)6 IORuntimeException (cn.hutool.core.io.IORuntimeException)4 Key (java.security.Key)4 PrivateKey (java.security.PrivateKey)4 PublicKey (java.security.PublicKey)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 RSAKey (java.security.interfaces.RSAKey)2 BadPaddingException (javax.crypto.BadPaddingException)2 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)2 InvalidCipherTextException (org.bouncycastle.crypto.InvalidCipherTextException)2 SM2Engine (org.bouncycastle.crypto.engines.SM2Engine)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 X509Certificate (java.security.cert.X509Certificate)1 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)1 WriteLock (java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock)1 CipherInputStream (javax.crypto.CipherInputStream)1