Search in sources :

Example 1 with SM2Engine

use of org.bouncycastle.crypto.engines.SM2Engine in project hutool by looly.

the class SM2 method decrypt.

/**
 * 解密
 *
 * @param data                 SM2密文,实际包含三部分:ECC公钥、真正的密文、公钥和原文的SM3-HASH值
 * @param privateKeyParameters 私钥参数
 * @return 加密后的bytes
 * @throws CryptoException 包括InvalidKeyException和InvalidCipherTextException的包装异常
 * @since 5.1.6
 */
public byte[] decrypt(byte[] data, CipherParameters privateKeyParameters) throws CryptoException {
    lock.lock();
    final SM2Engine engine = getEngine();
    try {
        engine.init(false, privateKeyParameters);
        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 2 with SM2Engine

use of org.bouncycastle.crypto.engines.SM2Engine in project hutool by looly.

the class SM2 method getEngine.

/**
 * 获取{@link SM2Engine},此对象为懒加载模式
 *
 * @return {@link SM2Engine}
 */
private SM2Engine getEngine() {
    if (null == this.engine) {
        Assert.notNull(this.digest, "digest must be not null !");
        this.engine = new SM2Engine(this.digest, this.mode);
    }
    this.digest.reset();
    return this.engine;
}
Also used : SM2Engine(org.bouncycastle.crypto.engines.SM2Engine)

Example 3 with SM2Engine

use of org.bouncycastle.crypto.engines.SM2Engine 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)

Aggregations

SM2Engine (org.bouncycastle.crypto.engines.SM2Engine)3 CryptoException (cn.hutool.crypto.CryptoException)2 InvalidCipherTextException (org.bouncycastle.crypto.InvalidCipherTextException)2