java.lang.Object
cn.dinodev.spring.commons.crypto.Sm2

public class Sm2 extends Object
Author:
Cody Lu
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final org.bouncycastle.math.ec.custom.gm.SM2P256V1Curve
     
    static final int
     
    static final org.bouncycastle.math.ec.ECPoint
     
    static final EllipticCurve
     
    static final ECParameterSpec
     
    static final ECPoint
     
    static final org.bouncycastle.crypto.params.ECDomainParameters
     
    static final BigInteger
     
    static final BigInteger
     
    static final BigInteger
     
    static final BigInteger
     
    static final BigInteger
     
    static final BigInteger
     
    static final BigInteger
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Sm2()
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static org.bouncycastle.crypto.params.ECPrivateKeyParameters
    buildECPrivateKeyParameters(byte[] privateKey)
    构建私钥参数
    static org.bouncycastle.crypto.params.ECPublicKeyParameters
    buildECPublicKeyParameters(byte[] publicKey)
    构建公钥参数
    static byte[]
    decrypt(byte[] input, byte[] privateKey)
    私钥解密, 默认使用SM2Engine.Mode.C1C2C3
    static byte[]
    decrypt(byte[] input, byte[] privateKey, org.bouncycastle.crypto.engines.SM2Engine.Mode mode)
    私钥解密
    static byte[]
    decrypt(byte[] input, org.bouncycastle.crypto.params.ECPrivateKeyParameters ecPrivateKeyParameters, org.bouncycastle.crypto.engines.SM2Engine.Mode mode)
    私钥解密
    static byte[]
    encrypt(byte[] input, byte[] publicKey)
    公钥加密, 默认使用SM2Engine.Mode.C1C2C3
    static byte[]
    encrypt(byte[] input, byte[] publicKey, org.bouncycastle.crypto.engines.SM2Engine.Mode mode)
    公钥加密
    static byte[]
    encrypt(byte[] input, org.bouncycastle.crypto.params.ECPublicKeyParameters ecPublicKeyParameters, org.bouncycastle.crypto.engines.SM2Engine.Mode mode)
    公钥加密
    static KeyPair
    生成密钥对
    static int
    getCurveLength(org.bouncycastle.crypto.params.ECDomainParameters domainParams)
    计算椭圆曲线的长度
    static byte[]
    getRawPrivateKey(org.bouncycastle.crypto.params.ECPrivateKeyParameters privateKey)
    取私钥里的d值
    static byte[]
    getRawPublicKey(org.bouncycastle.crypto.params.ECPublicKeyParameters publicKey)
    取公钥里的XY分量
    sign(byte[] input, byte[] privateKey, byte[] ID)
    私钥签名
    sign(byte[] input, org.bouncycastle.crypto.params.ECPrivateKeyParameters ecPrivateKeyParameters, byte[] ID)
    私钥签名
    static boolean
    verifySign(byte[] input, Sm2Signature signature, byte[] publicKey, byte[] ID)
    公钥验证签名
    static boolean
    verifySign(byte[] input, Sm2Signature signature, org.bouncycastle.crypto.params.ECPublicKeyParameters ecPublicKeyParameters, byte[] ID)
    公钥验证签名

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • CURVE

      public static final org.bouncycastle.math.ec.custom.gm.SM2P256V1Curve CURVE
    • SM2_ECC_P

      public static final BigInteger SM2_ECC_P
    • SM2_ECC_A

      public static final BigInteger SM2_ECC_A
    • SM2_ECC_B

      public static final BigInteger SM2_ECC_B
    • SM2_ECC_N

      public static final BigInteger SM2_ECC_N
    • SM2_ECC_H

      public static final BigInteger SM2_ECC_H
    • SM2_ECC_GX

      public static final BigInteger SM2_ECC_GX
    • SM2_ECC_GY

      public static final BigInteger SM2_ECC_GY
    • G_POINT

      public static final org.bouncycastle.math.ec.ECPoint G_POINT
    • SM2_DOMAIN

      public static final org.bouncycastle.crypto.params.ECDomainParameters SM2_DOMAIN
    • CURVE_LEN

      public static final int CURVE_LEN
    • JDK_CURVE

      public static final EllipticCurve JDK_CURVE
    • JDK_G_POINT

      public static final ECPoint JDK_G_POINT
    • JDK_EC_SPEC

      public static final ECParameterSpec JDK_EC_SPEC
  • Constructor Details

    • Sm2

      public Sm2()
  • Method Details

    • getCurveLength

      public static int getCurveLength(org.bouncycastle.crypto.params.ECDomainParameters domainParams)
      计算椭圆曲线的长度
      Parameters:
      domainParams -
      Returns:
    • generateKeyPair

      public static KeyPair generateKeyPair()
      生成密钥对
      Returns:
    • buildECPublicKeyParameters

      public static org.bouncycastle.crypto.params.ECPublicKeyParameters buildECPublicKeyParameters(byte[] publicKey)
      构建公钥参数
      Parameters:
      publicKey -
      Returns:
    • buildECPrivateKeyParameters

      public static org.bouncycastle.crypto.params.ECPrivateKeyParameters buildECPrivateKeyParameters(byte[] privateKey)
      构建私钥参数
      Parameters:
      privateKey -
      Returns:
    • getRawPrivateKey

      public static byte[] getRawPrivateKey(org.bouncycastle.crypto.params.ECPrivateKeyParameters privateKey)
      取私钥里的d值
      Parameters:
      privateKey -
      Returns:
      Curve长度的字节数组
    • getRawPublicKey

      public static byte[] getRawPublicKey(org.bouncycastle.crypto.params.ECPublicKeyParameters publicKey)
      取公钥里的XY分量
      Parameters:
      publicKey -
      Returns:
      2倍Curve长度的字节数组
    • encrypt

      public static byte[] encrypt(byte[] input, org.bouncycastle.crypto.params.ECPublicKeyParameters ecPublicKeyParameters, org.bouncycastle.crypto.engines.SM2Engine.Mode mode) throws org.bouncycastle.crypto.InvalidCipherTextException
      公钥加密
      Parameters:
      input - 待加密数据
      ecPublicKeyParameters - 公钥参数
      mode - 加密方式
      Returns:
      Throws:
      org.bouncycastle.crypto.InvalidCipherTextException
    • encrypt

      public static byte[] encrypt(byte[] input, byte[] publicKey, org.bouncycastle.crypto.engines.SM2Engine.Mode mode) throws org.bouncycastle.crypto.InvalidCipherTextException
      公钥加密
      Parameters:
      input - 待加密数据
      publicKey - 公钥参数
      mode - 加密方式
      Returns:
      Throws:
      org.bouncycastle.crypto.InvalidCipherTextException
    • encrypt

      public static byte[] encrypt(byte[] input, byte[] publicKey) throws org.bouncycastle.crypto.InvalidCipherTextException
      公钥加密, 默认使用SM2Engine.Mode.C1C2C3
      Parameters:
      input - 待加密数据
      publicKey - 公钥参数
      Returns:
      Throws:
      org.bouncycastle.crypto.InvalidCipherTextException
    • decrypt

      public static byte[] decrypt(byte[] input, org.bouncycastle.crypto.params.ECPrivateKeyParameters ecPrivateKeyParameters, org.bouncycastle.crypto.engines.SM2Engine.Mode mode) throws org.bouncycastle.crypto.InvalidCipherTextException
      私钥解密
      Parameters:
      input - 待解密数据
      ecPrivateKeyParameters - 私钥参数
      mode - 加密方式
      Returns:
      Throws:
      org.bouncycastle.crypto.InvalidCipherTextException
    • decrypt

      public static byte[] decrypt(byte[] input, byte[] privateKey, org.bouncycastle.crypto.engines.SM2Engine.Mode mode) throws org.bouncycastle.crypto.InvalidCipherTextException
      私钥解密
      Parameters:
      input - 待解密数据
      privateKey - 私钥参数
      mode - 加密方式
      Returns:
      Throws:
      org.bouncycastle.crypto.InvalidCipherTextException
    • decrypt

      public static byte[] decrypt(byte[] input, byte[] privateKey) throws org.bouncycastle.crypto.InvalidCipherTextException
      私钥解密, 默认使用SM2Engine.Mode.C1C2C3
      Parameters:
      input - 待解密数据
      privateKey - 私钥参数
      Returns:
      Throws:
      org.bouncycastle.crypto.InvalidCipherTextException
    • sign

      public static Sm2Signature sign(byte[] input, org.bouncycastle.crypto.params.ECPrivateKeyParameters ecPrivateKeyParameters, byte[] ID) throws org.bouncycastle.crypto.CryptoException
      私钥签名
      Parameters:
      input - 待签名数据
      ecPrivateKeyParameters - 私钥数据
      ID - 用户标识
      Returns:
      Throws:
      org.bouncycastle.crypto.CryptoException
    • sign

      public static Sm2Signature sign(byte[] input, byte[] privateKey, byte[] ID) throws org.bouncycastle.crypto.CryptoException
      私钥签名
      Parameters:
      input - 待签名数据
      privateKey - 私钥数据
      ID - 用户标识
      Returns:
      Throws:
      org.bouncycastle.crypto.CryptoException
    • verifySign

      public static boolean verifySign(byte[] input, Sm2Signature signature, org.bouncycastle.crypto.params.ECPublicKeyParameters ecPublicKeyParameters, byte[] ID) throws IOException
      公钥验证签名
      Parameters:
      input - 原始数据
      SM2SignResult - 签名
      ecPublicKeyParameters - 公钥参数
      ID - 用户标识
      Returns:
      Throws:
      IOException
    • verifySign

      public static boolean verifySign(byte[] input, Sm2Signature signature, byte[] publicKey, byte[] ID) throws IOException
      公钥验证签名
      Parameters:
      input - 原始数据
      SM2SignResult - 签名
      publicKey - 公钥参数
      ID - 用户标识
      Returns:
      Throws:
      IOException