Package cn.dinodev.spring.commons.crypto
Class Sm2
java.lang.Object
cn.dinodev.spring.commons.crypto.Sm2
- Author:
- Cody Lu
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final org.bouncycastle.math.ec.custom.gm.SM2P256V1Curvestatic final intstatic final org.bouncycastle.math.ec.ECPointstatic final EllipticCurvestatic final ECParameterSpecstatic final ECPointstatic final org.bouncycastle.crypto.params.ECDomainParametersstatic final BigIntegerstatic final BigIntegerstatic final BigIntegerstatic final BigIntegerstatic final BigIntegerstatic final BigIntegerstatic final BigInteger -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic org.bouncycastle.crypto.params.ECPrivateKeyParametersbuildECPrivateKeyParameters(byte[] privateKey) 构建私钥参数static org.bouncycastle.crypto.params.ECPublicKeyParametersbuildECPublicKeyParameters(byte[] publicKey) 构建公钥参数static byte[]decrypt(byte[] input, byte[] privateKey) 私钥解密, 默认使用SM2Engine.Mode.C1C2C3static 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.C1C2C3static 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 intgetCurveLength(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分量static Sm2Signaturesign(byte[] input, byte[] privateKey, byte[] ID) 私钥签名static Sm2Signaturesign(byte[] input, org.bouncycastle.crypto.params.ECPrivateKeyParameters ecPrivateKeyParameters, byte[] ID) 私钥签名static booleanverifySign(byte[] input, Sm2Signature signature, byte[] publicKey, byte[] ID) 公钥验证签名static booleanverifySign(byte[] input, Sm2Signature signature, org.bouncycastle.crypto.params.ECPublicKeyParameters ecPublicKeyParameters, byte[] ID) 公钥验证签名
-
Field Details
-
CURVE
public static final org.bouncycastle.math.ec.custom.gm.SM2P256V1Curve CURVE -
SM2_ECC_P
-
SM2_ECC_A
-
SM2_ECC_B
-
SM2_ECC_N
-
SM2_ECC_H
-
SM2_ECC_GX
-
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
-
JDK_G_POINT
-
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
生成密钥对- 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
-