Search in sources :

Example 16 with CryptoException

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);
        }
    }
}
Also used : CipherOutputStream(javax.crypto.CipherOutputStream) IORuntimeException(cn.hutool.core.io.IORuntimeException) Cipher(javax.crypto.Cipher) CryptoException(cn.hutool.crypto.CryptoException) IORuntimeException(cn.hutool.core.io.IORuntimeException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) IOException(java.io.IOException) CryptoException(cn.hutool.crypto.CryptoException) InvalidKeyException(java.security.InvalidKeyException)

Example 17 with CryptoException

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

Example 18 with CryptoException

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();
    }
}
Also used : InvalidCipherTextException(org.bouncycastle.crypto.InvalidCipherTextException) SM2Engine(org.bouncycastle.crypto.engines.SM2Engine) CryptoException(cn.hutool.crypto.CryptoException)

Example 19 with CryptoException

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

Example 20 with CryptoException

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

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