Package cn.dinodev.spring.commons.crypto
Class Sm2
java.lang.Object
cn.dinodev.spring.commons.crypto.Sm2
SM2椭圆曲线加密算法工具类
提供SM2密钥生成、加密、解密等功能的静态方法
- Since:
- 2022-05-06
- 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) 使用字节数组私钥解密数据(默认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) 使用字节数组公钥加密数据(默认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生成SM2密钥对。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[] userId) 使用字节数组私钥对数据进行数字签名。static Sm2Signaturesign(byte[] input, org.bouncycastle.crypto.params.ECPrivateKeyParameters ecPrivateKeyParameters, byte[] userId) 使用私钥对数据进行数字签名。static booleanverifySign(byte[] input, Sm2Signature signature, byte[] publicKey, byte[] userId) 使用字节数组公钥验证数字签名。static booleanverifySign(byte[] input, Sm2Signature signature, org.bouncycastle.crypto.params.ECPublicKeyParameters ecPublicKeyParameters, byte[] userId) 使用公钥验证数字签名。
-
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
生成SM2密钥对。使用安全随机数生成器创建一对SM2椭圆曲线密钥,包含公钥和私钥。
- Returns:
- 包含公钥和私钥字节数组的密钥对
-
buildECPublicKeyParameters
public static org.bouncycastle.crypto.params.ECPublicKeyParameters buildECPublicKeyParameters(byte[] publicKey) 构建椭圆曲线公钥参数。根据64字节的公钥数据(前32字节为x坐标,后32字节为y坐标)构建ECPublicKeyParameters对象。
- Parameters:
publicKey- 64字节的公钥数据- Returns:
- 椭圆曲线公钥参数对象
-
buildECPrivateKeyParameters
public static org.bouncycastle.crypto.params.ECPrivateKeyParameters buildECPrivateKeyParameters(byte[] privateKey) 构建椭圆曲线私钥参数。根据私钥字节数组构建ECPrivateKeyParameters对象。
- Parameters:
privateKey- 私钥字节数组- Returns:
- 椭圆曲线私钥参数对象
-
getRawPrivateKey
public static byte[] getRawPrivateKey(org.bouncycastle.crypto.params.ECPrivateKeyParameters privateKey) 提取私钥中的d值。从ECPrivateKeyParameters对象中提取私钥的d值,并转换为固定长度的字节数组。
- Parameters:
privateKey- 椭圆曲线私钥参数对象- Returns:
- 曲线长度的私钥字节数组
-
getRawPublicKey
public static byte[] getRawPublicKey(org.bouncycastle.crypto.params.ECPublicKeyParameters publicKey) 提取公钥中的XY坐标分量。从ECPublicKeyParameters对象中提取公钥的x和y坐标,组合成64字节的数组。
- Parameters:
publicKey- 椭圆曲线公钥参数对象- Returns:
- 2倍曲线长度的公钥字节数组(x坐标+y坐标)
-
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 使用公钥加密数据。使用指定的公钥参数和加密模式对输入数据进行SM2加密。
- Parameters:
input- 待加密的数据ecPublicKeyParameters- 椭圆曲线公钥参数mode- SM2加密模式(C1C2C3或C1C3C2)- 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 使用字节数组公钥加密数据(默认C1C2C3模式)。将64字节的公钥字节数组转换为椭圆曲线公钥参数,然后使用SM2Engine.Mode.C1C2C3模式进行加密。
- Parameters:
input- 待加密的数据publicKey- 64字节的公钥字节数组(x坐标+y坐标)- 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 使用私钥解密数据。使用指定的私钥参数和解密模式对输入数据进行SM2解密。
- Parameters:
input- 待解密的数据ecPrivateKeyParameters- 椭圆曲线私钥参数mode- SM2解密模式(C1C2C3或C1C3C2)- 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 使用字节数组私钥解密数据。将32字节的私钥字节数组转换为椭圆曲线私钥参数,然后使用指定模式进行解密。
- Parameters:
input- 待解密的数据privateKey- 32字节的私钥字节数组mode- SM2解密模式(C1C2C3或C1C3C2)- Returns:
- 解密后的数据
- Throws:
org.bouncycastle.crypto.InvalidCipherTextException- 如果解密过程中发生错误
-
decrypt
public static byte[] decrypt(byte[] input, byte[] privateKey) throws org.bouncycastle.crypto.InvalidCipherTextException 使用字节数组私钥解密数据(默认C1C2C3模式)。将32字节的私钥字节数组转换为椭圆曲线私钥参数,然后使用SM2Engine.Mode.C1C2C3模式进行解密。
- Parameters:
input- 待解密的数据privateKey- 32字节的私钥字节数组- Returns:
- 解密后的数据
- Throws:
org.bouncycastle.crypto.InvalidCipherTextException- 如果解密过程中发生错误
-
sign
public static Sm2Signature sign(byte[] input, org.bouncycastle.crypto.params.ECPrivateKeyParameters ecPrivateKeyParameters, byte[] userId) throws org.bouncycastle.crypto.CryptoException 使用私钥对数据进行数字签名。使用指定的私钥参数和用户标识对输入数据进行SM2数字签名。 如果用户标识为空或null,则不使用用户标识进行签名。
- Parameters:
input- 待签名的数据ecPrivateKeyParameters- 椭圆曲线私钥参数userId- 用户标识字节数组,可以为null- Returns:
- Sm2Signature对象,包含r和s分量的签名结果
- Throws:
org.bouncycastle.crypto.CryptoException- 如果签名过程中发生错误
-
sign
public static Sm2Signature sign(byte[] input, byte[] privateKey, byte[] userId) throws org.bouncycastle.crypto.CryptoException 使用字节数组私钥对数据进行数字签名。将32字节的私钥字节数组转换为椭圆曲线私钥参数,然后进行SM2数字签名。
- Parameters:
input- 待签名的数据privateKey- 32字节的私钥字节数组userId- 用户标识字节数组,可以为null- Returns:
- Sm2Signature对象,包含r和s分量的签名结果
- Throws:
org.bouncycastle.crypto.CryptoException- 如果签名过程中发生错误
-
verifySign
public static boolean verifySign(byte[] input, Sm2Signature signature, org.bouncycastle.crypto.params.ECPublicKeyParameters ecPublicKeyParameters, byte[] userId) throws IOException 使用公钥验证数字签名。使用指定的公钥参数和用户标识验证输入数据的SM2数字签名。 如果用户标识为空或null,则不使用用户标识进行验证。
- Parameters:
input- 原始数据signature- Sm2Signature签名对象,包含r和s分量ecPublicKeyParameters- 椭圆曲线公钥参数userId- 用户标识字节数组,可以为null- Returns:
- 如果签名验证成功返回true,否则返回false
- Throws:
IOException- 如果验证过程中发生IO错误
-
verifySign
public static boolean verifySign(byte[] input, Sm2Signature signature, byte[] publicKey, byte[] userId) throws IOException 使用字节数组公钥验证数字签名。将64字节的公钥字节数组转换为椭圆曲线公钥参数,然后验证输入数据的SM2数字签名。
- Parameters:
input- 原始数据signature- Sm2Signature签名对象,包含r和s分量publicKey- 64字节的公钥字节数组(x坐标+y坐标)userId- 用户标识字节数组,可以为null- Returns:
- 如果签名验证成功返回true,否则返回false
- Throws:
IOException- 如果验证过程中发生IO错误
-