org.bouncycastle.crypto.engines.SM2Engine Java Examples

The following examples show how to use org.bouncycastle.crypto.engines.SM2Engine. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SM2.java    From julongchain with Apache License 2.0 6 votes vote down vote up
/**
 * 私钥解密
 *
 * @param encryptData
 * @param privateKey
 * @return
 */
public byte[] decrypt(byte[] encryptData, byte[] privateKey) throws CspException{
    if (null == privateKey) {
        throw new CspException("privateKey is null");
    }
    if (privateKey.length == 0) {
        throw new CspException("privateKey's length is 0");
    }
    if (null==encryptData) {
        throw new CspException("plainText is null");
    }
    if (encryptData.length == 0) {
        throw new CspException("plainText's length is 0");
    }
    SM2Engine sm2Engine = new SM2Engine();
    BigInteger d = byte2BigInteger(privateKey);
    ECPrivateKeyParameters privateKeyParameters = new ECPrivateKeyParameters(d, ecc_bc_spec);
    sm2Engine.init(false, privateKeyParameters);
    try {
        byte[] dec = sm2Engine.processBlock(encryptData, 0, encryptData.length);
        return dec;
    } catch (InvalidCipherTextException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example #2
Source File: SM2Util.java    From gmhelper with Apache License 2.0 5 votes vote down vote up
/**
 * @param mode             指定密文结构,旧标准的为C1C2C3,新的[《SM2密码算法使用规范》 GM/T 0009-2012]标准为C1C3C2
 * @param pubKeyParameters 公钥
 * @param srcData          原文
 * @return 根据mode不同,输出的密文C1C2C3排列顺序不同。C1为65字节第1字节为压缩标识,这里固定为0x04,后面64字节为xy分量各32字节。C3为32字节。C2长度与原文一致。
 * @throws InvalidCipherTextException
 */
public static byte[] encrypt(Mode mode, ECPublicKeyParameters pubKeyParameters, byte[] srcData)
        throws InvalidCipherTextException {
    SM2Engine engine = new SM2Engine(mode);
    ParametersWithRandom pwr = new ParametersWithRandom(pubKeyParameters, new SecureRandom());
    engine.init(true, pwr);
    return engine.processBlock(srcData, 0, srcData.length);
}
 
Example #3
Source File: SM2.java    From julongchain with Apache License 2.0 5 votes vote down vote up
/**
 * 公钥加密消息
 *
 * @param input
 * @param publicLKey
 * @return
 */
public byte[] encrypt(byte[] input, byte[] publicLKey) throws CspException {
    if (null == input) {
        throw new CspException("input is null");
    }
    if (input.length == 0) {
        throw new CspException("input data length is 0");
    }
    if (null==publicLKey) {
        throw new CspException("publicLKey is null");
    }
    if (publicLKey.length == 0) {
        throw new CspException("publicLKey's length is 0");
    }
    SM2Engine sm2Engine = new SM2Engine();
    ECPublicKeyParameters ecPub = new ECPublicKeyParameters(byte2ECpoint(publicLKey), ecc_bc_spec);
    ParametersWithRandom parametersWithRandom = new ParametersWithRandom(ecPub);
    sm2Engine.init(true, parametersWithRandom);
    byte[] enc = null;
    try {
        enc = sm2Engine.processBlock(input, 0, input.length);
    } catch (InvalidCipherTextException e) {
        log.error(e.getMessage());
        throw new CspException(e.getMessage());
    }
    return enc;
}
 
Example #4
Source File: SM2Util.java    From gmhelper with Apache License 2.0 3 votes vote down vote up
/**
 * @param mode             指定密文结构,旧标准的为C1C2C3,新的[《SM2密码算法使用规范》 GM/T 0009-2012]标准为C1C3C2
 * @param priKeyParameters 私钥
 * @param sm2Cipher        根据mode不同,需要输入的密文C1C2C3排列顺序不同。C1为65字节第1字节为压缩标识,这里固定为0x04,后面64字节为xy分量各32字节。C3为32字节。C2长度与原文一致。
 * @return 原文。SM2解密返回了数据则一定是原文,因为SM2自带校验,如果密文被篡改或者密钥对不上,都是会直接报异常的。
 * @throws InvalidCipherTextException
 */
public static byte[] decrypt(Mode mode, ECPrivateKeyParameters priKeyParameters, byte[] sm2Cipher)
        throws InvalidCipherTextException {
    SM2Engine engine = new SM2Engine(mode);
    engine.init(false, priKeyParameters);
    return engine.processBlock(sm2Cipher, 0, sm2Cipher.length);
}
 
Example #5
Source File: Sm2Util.java    From littleca with Apache License 2.0 3 votes vote down vote up
/**
 * ECC公钥加密
 *
 * @param pubKey  ECC公钥
 * @param srcData 源数据
 * @return SM2密文,实际包含三部分:ECC公钥、真正的密文、公钥和原文的SM3-HASH值
 * @throws InvalidCipherTextException
 */
public static byte[] encrypt(Sm2PublicKey pubKey, byte[] srcData)
    throws InvalidCipherTextException {
    SM2Engine engine = new SM2Engine();
    ParametersWithRandom pwr = new ParametersWithRandom(pubKey.getPublicKeyParameters(), new SecureRandom());
    engine.init(true, pwr);
    return engine.processBlock(srcData, 0, srcData.length);
}
 
Example #6
Source File: Sm2Util.java    From littleca with Apache License 2.0 3 votes vote down vote up
/**
 * ECC私钥解密
 *
 * @param priKey        ECC私钥
 * @param sm2CipherText SM2密文,实际包含三部分:ECC公钥、真正的密文、公钥和原文的SM3-HASH值
 * @return 原文
 * @throws InvalidCipherTextException
 */
public static byte[] decrypt(Sm2PrivateKey priKey, byte[] sm2CipherText)
    throws InvalidCipherTextException {
    SM2Engine engine = new SM2Engine();
    engine.init(false, priKey.getPrivateKeyParameters());
    return engine.processBlock(sm2CipherText, 0, sm2CipherText.length);
}
 
Example #7
Source File: SM2Util.java    From jiguang-java-client-common with MIT License 3 votes vote down vote up
/**
 * ECC公钥加密
 *
 * @param pubKeyParameters ECC公钥
 * @param srcData          源数据
 * @return SM2密文,实际包含三部分:ECC公钥、真正的密文、公钥和原文的SM3-HASH值
 * @throws InvalidCipherTextException
 */
public static byte[] encrypt(ECPublicKeyParameters pubKeyParameters, byte[] srcData)
    throws InvalidCipherTextException {
    SM2Engine engine = new SM2Engine();
    ParametersWithRandom pwr = new ParametersWithRandom(pubKeyParameters, new SecureRandom());
    engine.init(true, pwr);
    return engine.processBlock(srcData, 0, srcData.length);
}
 
Example #8
Source File: SM2Util.java    From jiguang-java-client-common with MIT License 3 votes vote down vote up
/**
 * ECC私钥解密
 *
 * @param priKeyParameters ECC私钥
 * @param sm2Cipher        SM2密文,实际包含三部分:ECC公钥、真正的密文、公钥和原文的SM3-HASH值
 * @return 原文
 * @throws InvalidCipherTextException
 */
public static byte[] decrypt(ECPrivateKeyParameters priKeyParameters, byte[] sm2Cipher)
    throws InvalidCipherTextException {
    SM2Engine engine = new SM2Engine();
    engine.init(false, priKeyParameters);
    return engine.processBlock(sm2Cipher, 0, sm2Cipher.length);
}