栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

基于BC库的国密算法 SM2算法工具

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

基于BC库的国密算法 SM2算法工具

import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.CryptoException;
import org.bouncycastle.crypto.InvalidCipherTextException;
import org.bouncycastle.crypto.engines.SM2Engine;
import org.bouncycastle.crypto.engines.SM2Engine.Mode;
import org.bouncycastle.crypto.params.*;
import org.bouncycastle.crypto.signers.SM2Signer;
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey;
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey;
import org.bouncycastle.math.ec.ECPoint;
import org.bouncycastle.math.ec.custom.gm.SM2P256V1Curve;
 
import java.math.BigInteger;
import java.security.SecureRandom;
 
 

public final class SM2Utils
{
    
    private static final String SM2_ECC_GY_VAL = "BC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0";
    private static final String SM2_ECC_GX_VAL = "32C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE1715A4589334C74C7";
 
    private static final SM2P256V1Curve CURVE = new SM2P256V1Curve();
    private final static BigInteger SM2_ECC_N = CURVE.getOrder();
    private final static BigInteger SM2_ECC_H = CURVE.getCofactor();
 
    private final static BigInteger SM2_ECC_GX = new BigInteger(SM2_ECC_GX_VAL, 16);
    private final static BigInteger SM2_ECC_GY = new BigInteger(SM2_ECC_GY_VAL, 16);
 
    private static final ECPoint G_POINT = CURVE.createPoint(SM2_ECC_GX, SM2_ECC_GY);
    private static final ECDomainParameters DOMAIN_PARAMS = new ECDomainParameters(CURVE, G_POINT, SM2_ECC_N, SM2_ECC_H);
    
 
    private SM2Utils()
    {
    }
 
    
    public static AsymmetricCipherKeyPair generateKeyPairParameter()
    {
        final SecureRandom random = new SecureRandom();
        return BCECUtils.generateKeyPairParameter(DOMAIN_PARAMS, random);
    }
 
    
    public static byte[] encrypt(final BCECPublicKey pubKey, final byte[] srcData) throws InvalidCipherTextException
    {
        return encrypt(Mode.C1C3C2, pubKey, srcData);
    }
 
    
    public static byte[] encrypt(final ECPublicKeyParameters pubKeyParams, final byte[] srcData) throws InvalidCipherTextException
    {
        return encrypt(Mode.C1C3C2, pubKeyParams, srcData);
    }
 
    
    public static byte[] decrypt(final BCECPrivateKey priKey, final byte[] sm2Cipher) throws InvalidCipherTextException
    {
        return decrypt(Mode.C1C3C2, priKey, sm2Cipher);
    }
 
    
    public static byte[] decrypt(final ECPrivateKeyParameters priKeyParams, final byte[] sm2Cipher)
            throws InvalidCipherTextException
    {
        return decrypt(Mode.C1C3C2, priKeyParams, sm2Cipher);
    }
 
    
    public static byte[] sign(BCECPrivateKey priKey, byte[] srcData) throws CryptoException
    {
        return sign(priKey, null, srcData);
    }
 
    
    public static byte[] sign(final ECPrivateKeyParameters priKeyParams, final byte[] srcData) throws CryptoException
    {
        return sign(priKeyParams, null, srcData);
    }
 
    
    public static boolean verify(final BCECPublicKey pubKey, final byte[] srcData, final byte[] sign)
    {
        return verify(pubKey, null, srcData, sign);
    }
 
    
    public static boolean verify(final ECPublicKeyParameters pubKeyParams, final byte[] srcData, final byte[] sign)
    {
        return verify(pubKeyParams, null, srcData, sign);
    }
 
    
    private static byte[] encrypt(final Mode mode, final BCECPublicKey pubKey, final byte[] srcData)
            throws InvalidCipherTextException
    {
        final ECPublicKeyParameters pubKeyParams = BCECUtils.convertPublicKeyToParameters(pubKey);
        return encrypt(mode, pubKeyParams, srcData);
    }
 
    
    private static byte[] encrypt(final Mode mode, final ECPublicKeyParameters pubKeyParams, final byte[] srcData)
            throws InvalidCipherTextException
    {
        final SM2Engine engine = new SM2Engine(mode);
        final ParametersWithRandom pwr = new ParametersWithRandom(pubKeyParams, new SecureRandom());
        engine.init(true, pwr);
        return engine.processBlock(srcData, 0, srcData.length);
    }
 
    
    private static boolean verify(final BCECPublicKey pubKey, final byte[] withId, final byte[] srcData, final byte[] sign)
    {
        final ECPublicKeyParameters pubKeyParams = BCECUtils.convertPublicKeyToParameters(pubKey);
        return verify(pubKeyParams, withId, srcData, sign);
    }
 
    
    private static byte[] decrypt(final Mode mode, final BCECPrivateKey priKey, final byte[] sm2Cipher)
            throws InvalidCipherTextException
    {
        final ECPrivateKeyParameters priKeyParams = BCECUtils.convertPrivateKeyToParameters(priKey);
        return decrypt(mode, priKeyParams, sm2Cipher);
    }
 
    
    private static byte[] decrypt(final Mode mode, final ECPrivateKeyParameters priKeyParams, final byte[] sm2Cipher)
            throws InvalidCipherTextException
    {
        final SM2Engine engine = new SM2Engine(mode);
        engine.init(false, priKeyParams);
        return engine.processBlock(sm2Cipher, 0, sm2Cipher.length);
    }
 
    
    private static byte[] sign(final BCECPrivateKey priKey, final byte[] withId, final byte[] srcData) throws CryptoException
    {
        final ECPrivateKeyParameters priKeyParams = BCECUtils.convertPrivateKeyToParameters(priKey);
        return sign(priKeyParams, withId, srcData);
    }
 
    
    private static byte[] sign(final ECPrivateKeyParameters priKeyParams, final byte[] withId, final byte[] srcData)
            throws CryptoException
    {
        SM2Signer signer = new SM2Signer();
        CipherParameters param = null;
        ParametersWithRandom pwr = new ParametersWithRandom(priKeyParams, new SecureRandom());
        if (withId != null)
        {
            param = new ParametersWithID(pwr, withId);
        }
        else
        {
            param = pwr;
        }
        signer.init(true, param);
        signer.update(srcData, 0, srcData.length);
        return signer.generateSignature();
    }
 
    
    private static boolean verify(final ECPublicKeyParameters pubKeyParams, final byte[] withId, final byte[] srcData,
            final byte[] sign)
    {
        final SM2Signer signer = new SM2Signer();
        CipherParameters param;
        if (withId != null)
        {
            param = new ParametersWithID(pubKeyParams, withId);
        }
        else
        {
            param = pubKeyParams;
        }
        signer.init(false, param);
        signer.update(srcData, 0, srcData.length);
        return signer.verifySignature(sign);
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/827461.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号