├── .gitignore ├── README.md ├── pom.xml └── src ├── main └── java │ └── com │ └── gm │ ├── sm2 │ ├── SM2Constants.java │ ├── SM2Helper.java │ ├── SM2KeyHelper.java │ ├── SM2KeyPair.java │ └── SM2SignResult.java │ ├── sm3 │ └── SM3Helper.java │ └── sm4 │ ├── SM4Helper.java │ ├── SM4KeyHelper.java │ └── SM4ModeAndPaddingEnum.java └── test └── java ├── sm2 └── SM2Test.java ├── sm3 └── SM3Test.java └── sm4 └── SM4Test.java /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # Package Files # 14 | *.jar 15 | *.war 16 | *.nar 17 | *.ear 18 | *.zip 19 | *.tar.gz 20 | *.rar 21 | 22 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 23 | hs_err_pid* 24 | 25 | *.iml 26 | .idea 27 | target -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | **国密 SM2 SM3 SM4 算法, 使用的是bouncycastle包** -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.gm 8 | gm-encryption 9 | 1.0 10 | 11 | 12 | 13 | 1.7 14 | 1.7 15 | utf-8 16 | 17 | 18 | 19 | 20 | commons-codec 21 | commons-codec 22 | 1.10 23 | 24 | 25 | org.bouncycastle 26 | bcprov-jdk15to18 27 | 1.64 28 | 29 | 30 | junit 31 | junit 32 | 4.11 33 | test 34 | 35 | 36 | 37 | 38 | 39 | 40 | org.apache.maven.plugins 41 | maven-compiler-plugin 42 | 43 | ${maven.compiler.source} 44 | ${maven.compiler.target} 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /src/main/java/com/gm/sm2/SM2Constants.java: -------------------------------------------------------------------------------- 1 | package com.gm.sm2; 2 | 3 | import org.bouncycastle.crypto.params.ECDomainParameters; 4 | import org.bouncycastle.math.ec.ECPoint; 5 | import org.bouncycastle.math.ec.custom.gm.SM2P256V1Curve; 6 | 7 | import java.math.BigInteger; 8 | 9 | /** 10 | * @Description: 11 | * @Author: wucheng 12 | * @CreateDate: 2020/2/16 17:02 13 | */ 14 | public class SM2Constants { 15 | 16 | /* 17 | * 以下为SM2推荐曲线参数 18 | */ 19 | public static final SM2P256V1Curve CURVE = new SM2P256V1Curve(); 20 | public final static BigInteger SM2_ECC_P = CURVE.getQ(); 21 | public final static BigInteger SM2_ECC_A = CURVE.getA().toBigInteger(); 22 | public final static BigInteger SM2_ECC_B = CURVE.getB().toBigInteger(); 23 | public final static BigInteger SM2_ECC_N = CURVE.getOrder(); 24 | public final static BigInteger SM2_ECC_H = CURVE.getCofactor(); 25 | public final static BigInteger SM2_ECC_GX = new BigInteger( 26 | "32C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE1715A4589334C74C7", 16); 27 | public final static BigInteger SM2_ECC_GY = new BigInteger( 28 | "BC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0", 16); 29 | public static final ECPoint G_POINT = CURVE.createPoint(SM2_ECC_GX, SM2_ECC_GY); 30 | public static final ECDomainParameters DOMAIN_PARAMS = new ECDomainParameters(CURVE, G_POINT, 31 | SM2_ECC_N, SM2_ECC_H); 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/gm/sm2/SM2Helper.java: -------------------------------------------------------------------------------- 1 | package com.gm.sm2; 2 | 3 | import org.bouncycastle.crypto.CipherParameters; 4 | import org.bouncycastle.crypto.engines.SM2Engine; 5 | import org.bouncycastle.crypto.params.ECPrivateKeyParameters; 6 | import org.bouncycastle.crypto.params.ECPublicKeyParameters; 7 | import org.bouncycastle.crypto.params.ParametersWithID; 8 | import org.bouncycastle.crypto.params.ParametersWithRandom; 9 | import org.bouncycastle.crypto.signers.SM2Signer; 10 | import org.bouncycastle.crypto.signers.StandardDSAEncoding; 11 | import org.bouncycastle.jce.provider.BouncyCastleProvider; 12 | 13 | import java.math.BigInteger; 14 | import java.security.SecureRandom; 15 | import java.security.Security; 16 | 17 | /** 18 | * @Description: 国密sm2非对称加解密算法帮助类 19 | * @Author: wucheng 20 | * @CreateDate: 2020/2/16 16:57 21 | */ 22 | public class SM2Helper { 23 | static{ 24 | Security.addProvider(new BouncyCastleProvider()); 25 | } 26 | 27 | /** 28 | * 公钥加密 29 | * @param input 待加密数据 30 | * @param ecPublicKeyParameters 公钥参数 31 | * @param mode 加密方式 32 | * @return 33 | * @throws Exception 34 | */ 35 | public static byte[] encrypt(byte[] input, ECPublicKeyParameters ecPublicKeyParameters, SM2Engine.Mode mode) throws Exception{ 36 | SM2Engine engine = new SM2Engine(mode); 37 | ParametersWithRandom parametersWithRandom = new ParametersWithRandom(ecPublicKeyParameters, new SecureRandom()); 38 | engine.init(true, parametersWithRandom); 39 | return engine.processBlock(input, 0, input.length); 40 | } 41 | 42 | /** 43 | * 私钥解密 44 | * @param input 待解密数据 45 | * @param ecPrivateKeyParameters 私钥参数 46 | * @param mode 加密方式 47 | * @return 48 | * @throws Exception 49 | */ 50 | public static byte[] decrypt(byte[] input, ECPrivateKeyParameters ecPrivateKeyParameters, SM2Engine.Mode mode) throws Exception{ 51 | SM2Engine engine = new SM2Engine(mode); 52 | engine.init(false, ecPrivateKeyParameters); 53 | return engine.processBlock(input, 0, input.length); 54 | } 55 | 56 | /** 57 | * 私钥签名 58 | * @param input 待签名数据 59 | * @param ecPrivateKeyParameters 私钥数据 60 | * @param ID 用户标识 61 | * @return 62 | * @throws Exception 63 | */ 64 | public static SM2SignResult sign(byte[] input, ECPrivateKeyParameters ecPrivateKeyParameters, byte[] ID) throws Exception{ 65 | SM2Signer signer = new SM2Signer(); 66 | CipherParameters param; 67 | if (ID != null && ID.length>0) { 68 | param = new ParametersWithID(ecPrivateKeyParameters, ID); 69 | } else { 70 | param = ecPrivateKeyParameters; 71 | } 72 | signer.init(true, param); 73 | signer.update(input, 0, input.length); 74 | byte[] sign = signer.generateSignature(); 75 | SM2SignResult SM2SignResult = new SM2SignResult(); 76 | SM2SignResult.decodeStandardDSA(sign); 77 | return SM2SignResult; 78 | } 79 | 80 | /** 81 | * 公钥验证签名 82 | * @param input 原始数据 83 | * @param SM2SignResult 签名 84 | * @param ecPublicKeyParameters 公钥参数 85 | * @param ID 用户标识 86 | * @return 87 | * @throws Exception 88 | */ 89 | public static boolean verifySign(byte[] input, SM2SignResult SM2SignResult, ECPublicKeyParameters ecPublicKeyParameters, byte[] ID) throws Exception{ 90 | BigInteger signR = new BigInteger(1, SM2SignResult.getSignR()); 91 | BigInteger signS = new BigInteger(1, SM2SignResult.getSignS()); 92 | byte[] sign = StandardDSAEncoding.INSTANCE.encode(SM2Constants.SM2_ECC_N, signR, signS); 93 | 94 | SM2Signer signer = new SM2Signer(); 95 | CipherParameters param; 96 | if (ID != null && ID.length>0) { 97 | param = new ParametersWithID(ecPublicKeyParameters, ID); 98 | } else { 99 | param = ecPublicKeyParameters; 100 | } 101 | signer.init(false, param); 102 | signer.update(input, 0, input.length); 103 | return signer.verifySignature(sign); 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /src/main/java/com/gm/sm2/SM2KeyHelper.java: -------------------------------------------------------------------------------- 1 | package com.gm.sm2; 2 | 3 | import org.bouncycastle.crypto.AsymmetricCipherKeyPair; 4 | import org.bouncycastle.crypto.generators.ECKeyPairGenerator; 5 | import org.bouncycastle.crypto.params.ECKeyGenerationParameters; 6 | import org.bouncycastle.crypto.params.ECPrivateKeyParameters; 7 | import org.bouncycastle.crypto.params.ECPublicKeyParameters; 8 | import org.bouncycastle.jce.provider.BouncyCastleProvider; 9 | import org.bouncycastle.math.ec.ECPoint; 10 | 11 | import java.math.BigInteger; 12 | import java.security.SecureRandom; 13 | import java.security.Security; 14 | 15 | /** 16 | * @Description: 17 | * @Author: wucheng 18 | * @CreateDate: 2020/2/16 16:56 19 | */ 20 | public class SM2KeyHelper { 21 | 22 | static{ 23 | Security.addProvider(new BouncyCastleProvider()); 24 | } 25 | 26 | /** 27 | * 生成公私钥 28 | * @return 29 | */ 30 | public static SM2KeyPair generateKeyPair() { 31 | SecureRandom random = new SecureRandom(); 32 | ECKeyGenerationParameters keyGenerationParams = new ECKeyGenerationParameters(SM2Constants.DOMAIN_PARAMS, 33 | random); 34 | ECKeyPairGenerator keyGen = new ECKeyPairGenerator(); 35 | keyGen.init(keyGenerationParams); 36 | AsymmetricCipherKeyPair keyPair = keyGen.generateKeyPair(); 37 | ECPublicKeyParameters ecPublicKeyParameters = (ECPublicKeyParameters) keyPair.getPublic(); 38 | ECPrivateKeyParameters ecPrivateKeyParameters = (ECPrivateKeyParameters) keyPair.getPrivate(); 39 | return new SM2KeyPair(ecPublicKeyParameters.getQ().getAffineXCoord().getEncoded(), ecPublicKeyParameters.getQ().getAffineYCoord().getEncoded(), ecPrivateKeyParameters.getD().toByteArray()); 40 | } 41 | 42 | /** 43 | * 构建公钥参数 44 | * @param sm2KeyPair 45 | * @return 46 | */ 47 | public static ECPublicKeyParameters buildECPublicKeyParameters(SM2KeyPair sm2KeyPair){ 48 | return buildECPublicKeyParameters(sm2KeyPair.getPublicKeyX(), sm2KeyPair.getPublicKeyY()); 49 | } 50 | 51 | /** 52 | * 构建公钥参数 53 | * @param publicKeyX 54 | * @param publicKeyY 55 | * @return 56 | */ 57 | public static ECPublicKeyParameters buildECPublicKeyParameters(byte[] publicKeyX, byte[] publicKeyY){ 58 | ECPoint pointQ = SM2Constants.CURVE.createPoint(new BigInteger(1, publicKeyX), new BigInteger(1, publicKeyY)); 59 | return new ECPublicKeyParameters(pointQ, SM2Constants.DOMAIN_PARAMS); 60 | } 61 | 62 | /** 63 | * 构建私钥参数 64 | * @param privateKey 65 | * @return 66 | */ 67 | public static ECPrivateKeyParameters buildECPrivateKeyParameters(byte[] privateKey){ 68 | BigInteger d = new BigInteger(1, privateKey); 69 | return new ECPrivateKeyParameters(d, SM2Constants.DOMAIN_PARAMS); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/main/java/com/gm/sm2/SM2KeyPair.java: -------------------------------------------------------------------------------- 1 | package com.gm.sm2; 2 | 3 | /** 4 | * @Description: 5 | * @Author: wucheng 6 | * @CreateDate: 2020/2/16 17:07 7 | */ 8 | public class SM2KeyPair { 9 | private byte[] publicKeyX; 10 | private byte[] publicKeyY; 11 | private byte[] privateKey; 12 | 13 | public SM2KeyPair(byte[] publicKeyX, byte[] publicKeyY, byte[] privateKey) { 14 | this.publicKeyX = publicKeyX; 15 | this.publicKeyY = publicKeyY; 16 | this.privateKey = privateKey; 17 | } 18 | 19 | public byte[] getPublicKeyX() { 20 | return publicKeyX; 21 | } 22 | 23 | public byte[] getPublicKeyY() { 24 | return publicKeyY; 25 | } 26 | 27 | public byte[] getPrivateKey() { 28 | return privateKey; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/com/gm/sm2/SM2SignResult.java: -------------------------------------------------------------------------------- 1 | package com.gm.sm2; 2 | 3 | import org.bouncycastle.crypto.signers.DSAEncoding; 4 | import org.bouncycastle.crypto.signers.PlainDSAEncoding; 5 | import org.bouncycastle.crypto.signers.StandardDSAEncoding; 6 | import org.bouncycastle.util.encoders.Hex; 7 | 8 | import java.math.BigInteger; 9 | 10 | /** 11 | * @Description: 12 | * @Author: wucheng 13 | * @CreateDate: 2020/2/16 17:01 14 | */ 15 | public class SM2SignResult { 16 | private byte[] signR; 17 | private byte[] signS; 18 | 19 | public SM2SignResult() { 20 | } 21 | 22 | public SM2SignResult(byte[] signR, byte[] signS) { 23 | this.signR = signR; 24 | this.signS = signS; 25 | } 26 | 27 | public byte[] getSignR() { 28 | return signR; 29 | } 30 | 31 | public byte[] getSignS() { 32 | return signS; 33 | } 34 | 35 | public byte[] mergeRS(){ 36 | byte[] ret = new byte[signR.length+signS.length]; 37 | System.arraycopy(signR, 0, ret, 0, signR.length); 38 | System.arraycopy(signS, 0, ret, signR.length, signS.length); 39 | return ret; 40 | } 41 | 42 | public byte[] encodeStandardDSA() throws Exception { 43 | return encode(StandardDSAEncoding.INSTANCE); 44 | } 45 | 46 | public byte[] encodePlainDSA() throws Exception{ 47 | return encode(PlainDSAEncoding.INSTANCE); 48 | } 49 | 50 | public void decodeStandardDSA(byte[] signDSAEncoding) throws Exception{ 51 | decode(StandardDSAEncoding.INSTANCE, signDSAEncoding); 52 | } 53 | 54 | public void decodePlainDSA(byte[] signDSAEncoding) throws Exception{ 55 | decode(PlainDSAEncoding.INSTANCE, signDSAEncoding); 56 | } 57 | 58 | private byte[] encode(DSAEncoding dsaEncoding) throws Exception { 59 | BigInteger bigIntegerSignR = new BigInteger(Hex.toHexString(getSignR()), 16); 60 | BigInteger bigIntegerSignS = new BigInteger(Hex.toHexString(getSignS()), 16); 61 | return dsaEncoding.encode(SM2Constants.SM2_ECC_N, bigIntegerSignR, bigIntegerSignS); 62 | } 63 | 64 | private void decode(DSAEncoding dsaEncoding, byte[] signDSAEncoding) throws Exception{ 65 | BigInteger[] bigIntegers = dsaEncoding.decode(SM2Constants.SM2_ECC_N, signDSAEncoding); 66 | this.signR = bigIntegers[0].toByteArray(); 67 | this.signS = bigIntegers[1].toByteArray(); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/com/gm/sm3/SM3Helper.java: -------------------------------------------------------------------------------- 1 | package com.gm.sm3; 2 | 3 | import org.bouncycastle.crypto.digests.SM3Digest; 4 | import org.bouncycastle.jce.provider.BouncyCastleProvider; 5 | 6 | import java.security.Security; 7 | 8 | /** 9 | * @Description: 国密SM3摘要算法帮助类 10 | * @Author: wucheng 11 | * @CreateDate: 2020/2/16 16:36 12 | */ 13 | public class SM3Helper { 14 | static{ 15 | Security.addProvider(new BouncyCastleProvider()); 16 | } 17 | 18 | public static byte[] digest(byte[] input){ 19 | SM3Digest sm3Digest = new SM3Digest(); 20 | sm3Digest.update(input, 0, input.length); 21 | byte[] ret = new byte[sm3Digest.getDigestSize()]; 22 | sm3Digest.doFinal(ret, 0); 23 | return ret; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/gm/sm4/SM4Helper.java: -------------------------------------------------------------------------------- 1 | package com.gm.sm4; 2 | 3 | import org.bouncycastle.jce.provider.BouncyCastleProvider; 4 | 5 | import javax.crypto.Cipher; 6 | import javax.crypto.spec.IvParameterSpec; 7 | import javax.crypto.spec.SecretKeySpec; 8 | import java.security.Security; 9 | 10 | /** 11 | * @Description: 国密SM4对称加解密算法帮助类 12 | * @Author: wucheng 13 | * @CreateDate: 2020/2/16 16:38 14 | */ 15 | public class SM4Helper { 16 | static{ 17 | Security.addProvider(new BouncyCastleProvider()); 18 | } 19 | 20 | /** 21 | * SM4 加密 22 | * @param input 明文数据 23 | * @param key 密钥 24 | * @param sm4ModeAndPaddingEnum 加密模式和padding模式 25 | * @param iv 初始向量(ECB模式下传NULL) 26 | * @return 27 | * @throws Exception 28 | */ 29 | public static byte[] encrypt(byte[] input, byte[] key, SM4ModeAndPaddingEnum sm4ModeAndPaddingEnum, byte[] iv) throws Exception{ 30 | return sm4(input, key, sm4ModeAndPaddingEnum, iv, Cipher.ENCRYPT_MODE); 31 | } 32 | 33 | /** 34 | * SM4 解密 35 | * @param input 密文数据 36 | * @param key 密钥 37 | * @param sm4ModeAndPaddingEnum 加密模式和padding模式 38 | * @param iv 初始向量(ECB模式下传NULL) 39 | * @return 40 | * @throws Exception 41 | */ 42 | public static byte[] decrypt(byte[] input, byte[] key, SM4ModeAndPaddingEnum sm4ModeAndPaddingEnum, byte[] iv) throws Exception{ 43 | return sm4(input, key, sm4ModeAndPaddingEnum, iv, Cipher.DECRYPT_MODE); 44 | } 45 | 46 | private static byte[] sm4(byte[] input, byte[] key, SM4ModeAndPaddingEnum sm4ModeAndPaddingEnum, byte[] iv, int mode) throws Exception{ 47 | IvParameterSpec ivParameterSpec = null; 48 | if(iv!=null){ 49 | ivParameterSpec = new IvParameterSpec(iv); 50 | } 51 | SecretKeySpec sm4Key = new SecretKeySpec(key, "SM4"); 52 | Cipher cipher = Cipher.getInstance(sm4ModeAndPaddingEnum.getName(), BouncyCastleProvider.PROVIDER_NAME); 53 | if(ivParameterSpec==null){ 54 | cipher.init(mode, sm4Key); 55 | }else{ 56 | cipher.init(mode, sm4Key, ivParameterSpec); 57 | } 58 | return cipher.doFinal(input); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/com/gm/sm4/SM4KeyHelper.java: -------------------------------------------------------------------------------- 1 | package com.gm.sm4; 2 | 3 | import org.bouncycastle.jce.provider.BouncyCastleProvider; 4 | 5 | import javax.crypto.KeyGenerator; 6 | import java.security.SecureRandom; 7 | import java.security.Security; 8 | 9 | /** 10 | * @Description: 11 | * @Author: wucheng 12 | * @CreateDate: 2020/2/16 16:51 13 | */ 14 | public class SM4KeyHelper { 15 | static{ 16 | Security.addProvider(new BouncyCastleProvider()); 17 | } 18 | 19 | public static byte[] generateKey() throws Exception{ 20 | KeyGenerator kg = KeyGenerator.getInstance("SM4", BouncyCastleProvider.PROVIDER_NAME); 21 | kg.init(128, new SecureRandom()); 22 | return kg.generateKey().getEncoded(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/gm/sm4/SM4ModeAndPaddingEnum.java: -------------------------------------------------------------------------------- 1 | package com.gm.sm4; 2 | 3 | /** 4 | * @Description: SM4对称加解密算法中的模式和padding方式枚举类 5 | * @Author: wucheng 6 | * @CreateDate: 2020/2/16 16:39 7 | */ 8 | public enum SM4ModeAndPaddingEnum { 9 | SM4_ECB_NoPadding("SM4/ECB/NoPadding"), 10 | SM4_ECB_PKCS5Padding("SM4/ECB/PKCS5Padding"), 11 | SM4_ECB_PKCS7Padding("SM4/ECB/PKCS7Padding"), 12 | SM4_CBC_NoPadding("SM4/CBC/NoPadding"), 13 | SM4_CBC_PKCS5Padding("SM4/CBC/PKCS5Padding"), 14 | SM4_CBC_PKCS7Padding("SM4/CBC/PKCS7Padding"); 15 | 16 | private String name; 17 | 18 | SM4ModeAndPaddingEnum(String name) { 19 | this.name = name; 20 | } 21 | 22 | public String getName() { 23 | return name; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/test/java/sm2/SM2Test.java: -------------------------------------------------------------------------------- 1 | package sm2; 2 | 3 | import com.gm.sm2.SM2Helper; 4 | import com.gm.sm2.SM2KeyHelper; 5 | import com.gm.sm2.SM2KeyPair; 6 | import com.gm.sm2.SM2SignResult; 7 | import org.bouncycastle.crypto.engines.SM2Engine; 8 | import org.bouncycastle.crypto.params.ECPrivateKeyParameters; 9 | import org.bouncycastle.crypto.params.ECPublicKeyParameters; 10 | import org.bouncycastle.util.encoders.Hex; 11 | import org.junit.Assert; 12 | import org.junit.Test; 13 | 14 | import java.nio.charset.Charset; 15 | 16 | /** 17 | * @Description: 18 | * @Author: wucheng 19 | * @CreateDate: 2020/2/16 17:10 20 | */ 21 | public class SM2Test { 22 | 23 | public static final Charset charset = Charset.forName("utf-8"); 24 | private static final String ID = "1234"; 25 | private static final String input = "test input 0123456789"; 26 | 27 | 28 | @Test 29 | public void generateKeyPair(){ 30 | SM2KeyPair sm2KeyPair = SM2KeyHelper.generateKeyPair(); 31 | 32 | Assert.assertNotNull("SM2 test generate keyPair failed!",sm2KeyPair); 33 | Assert.assertNotNull("SM2 test generate keyPair failed!", sm2KeyPair.getPublicKeyX()); 34 | Assert.assertNotNull("SM2 test generate keyPair failed!", sm2KeyPair.getPublicKeyY()); 35 | Assert.assertNotNull("SM2 test generate keyPair failed!", sm2KeyPair.getPrivateKey()); 36 | System.out.println("publicKeyX:"+ Hex.toHexString(sm2KeyPair.getPublicKeyX())); 37 | System.out.println("publicKeyY:"+ Hex.toHexString(sm2KeyPair.getPublicKeyY())); 38 | System.out.println("privateKey:"+ Hex.toHexString(sm2KeyPair.getPrivateKey())); 39 | } 40 | 41 | @Test 42 | public void testEncryptAndDecrypt() throws Exception{ 43 | SM2KeyPair sm2KeyPair = SM2KeyHelper.generateKeyPair(); 44 | ECPublicKeyParameters ecPublicKeyParameters = SM2KeyHelper.buildECPublicKeyParameters(sm2KeyPair); 45 | ECPrivateKeyParameters ecPrivateKeyParameters = SM2KeyHelper.buildECPrivateKeyParameters(sm2KeyPair.getPrivateKey()); 46 | 47 | 48 | //C1C2C3 mode 49 | byte[] encryptRet123 = SM2Helper.encrypt(input.getBytes(charset), ecPublicKeyParameters, SM2Engine.Mode.C1C2C3); 50 | System.out.println("SM2 encrypt C1C2C3 mode result:"+Hex.toHexString(encryptRet123)); 51 | byte[] decryptRet123 = SM2Helper.decrypt(encryptRet123, ecPrivateKeyParameters, SM2Engine.Mode.C1C2C3); 52 | Assert.assertEquals("SM2 encrypt and decrypt C1C2C3 mode failed!", input, new String(decryptRet123, charset)); 53 | 54 | 55 | //C1C3C2 mode 56 | byte[] encryptRet132 = SM2Helper.encrypt(input.getBytes(charset), ecPublicKeyParameters, SM2Engine.Mode.C1C3C2); 57 | System.out.println("SM2 encrypt C1C3C2 mode result:"+Hex.toHexString(encryptRet132)); 58 | byte[] decryptRet132 = SM2Helper.decrypt(encryptRet132, ecPrivateKeyParameters, SM2Engine.Mode.C1C3C2); 59 | Assert.assertEquals("SM2 encrypt and decrypt C1C3C2 mode failed!", input, new String(decryptRet132, charset)); 60 | } 61 | 62 | @Test 63 | public void testSignAndVerifySign() throws Exception{ 64 | SM2KeyPair sm2KeyPair = SM2KeyHelper.generateKeyPair(); 65 | ECPublicKeyParameters ecPublicKeyParameters = SM2KeyHelper.buildECPublicKeyParameters(sm2KeyPair); 66 | ECPrivateKeyParameters ecPrivateKeyParameters = SM2KeyHelper.buildECPrivateKeyParameters(sm2KeyPair.getPrivateKey()); 67 | 68 | //sign and verifySign with ID 69 | SM2SignResult signRet = SM2Helper.sign(input.getBytes(charset), ecPrivateKeyParameters, ID.getBytes(charset)); 70 | System.out.println("signResultR_withID:"+Hex.toHexString(signRet.getSignR())); 71 | System.out.println("signResultS_withID:"+Hex.toHexString(signRet.getSignS())); 72 | boolean verifySignRet = SM2Helper.verifySign(input.getBytes(charset), signRet, ecPublicKeyParameters, ID.getBytes(charset)); 73 | Assert.assertTrue("sign and verifySign with ID failed!", verifySignRet); 74 | 75 | //sign and verifySign without ID 76 | signRet = SM2Helper.sign(input.getBytes(charset), ecPrivateKeyParameters, null); 77 | System.out.println("signResultR_withoutID:"+Hex.toHexString(signRet.getSignR())); 78 | System.out.println("signResultS_withoutID:"+Hex.toHexString(signRet.getSignS())); 79 | verifySignRet = SM2Helper.verifySign(input.getBytes(charset), signRet, ecPublicKeyParameters, null); 80 | Assert.assertTrue("sign and verifySign without ID failed!", verifySignRet); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/test/java/sm3/SM3Test.java: -------------------------------------------------------------------------------- 1 | package sm3; 2 | 3 | import com.gm.sm3.SM3Helper; 4 | import org.bouncycastle.util.encoders.Hex; 5 | import org.junit.Assert; 6 | import org.junit.Test; 7 | 8 | /** 9 | * @Description: 10 | * @Author: wucheng 11 | * @CreateDate: 2020/2/16 16:42 12 | */ 13 | public class SM3Test { 14 | 15 | @Test 16 | public void testSm3Digest() throws Exception { 17 | String input = "test input"; 18 | byte[] ret = SM3Helper.digest(input.getBytes("utf-8")); 19 | 20 | Assert.assertNotNull("SM3 test digest failed!", ret); 21 | System.out.println("sm3 digest result:" + Hex.toHexString(ret)); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/test/java/sm4/SM4Test.java: -------------------------------------------------------------------------------- 1 | package sm4; 2 | 3 | import com.gm.sm4.SM4Helper; 4 | import com.gm.sm4.SM4KeyHelper; 5 | import com.gm.sm4.SM4ModeAndPaddingEnum; 6 | import org.apache.commons.codec.binary.Hex; 7 | import org.junit.Assert; 8 | import org.junit.Test; 9 | 10 | /** 11 | * @Description: 12 | * @Author: wucheng 13 | * @CreateDate: 2020/2/16 16:46 14 | */ 15 | public class SM4Test { 16 | 17 | @Test 18 | public void testGenerateKey() throws Exception { 19 | byte[] key = SM4KeyHelper.generateKey(); 20 | Assert.assertNotNull("test SM4 generate key failed!", key); 21 | System.out.println(String.format("key:%s", Hex.encodeHexString(key))); 22 | } 23 | 24 | @Test 25 | public void testEncryptAndDecrypt() throws Exception{ 26 | String inputHex = "0123456789ABCDEF0123456789ABCDEF"; 27 | String keyHex = "4D744E003D713D054E7E407C350E447E"; 28 | String ivHex = "4F723F7349774F063C0C477A367B3278"; 29 | 30 | for(SM4ModeAndPaddingEnum e: SM4ModeAndPaddingEnum.values()){ 31 | byte[] iv = null; 32 | if(!e.getName().contains("ECB")){ 33 | iv = Hex.decodeHex(ivHex.toCharArray()); 34 | } 35 | 36 | byte[] encryptRet = SM4Helper.encrypt(Hex.decodeHex(inputHex.toCharArray()), Hex.decodeHex(keyHex.toCharArray()), e, iv); 37 | System.out.println(String.format("SM4 mode = %s, encryptRet = %s", e.getName(), Hex.encodeHexString(encryptRet))); 38 | byte[] decryptRet = SM4Helper.decrypt(encryptRet, Hex.decodeHex(keyHex.toCharArray()), e, iv); 39 | Assert.assertEquals(String.format("SM4 %s test encrypt and decrypt failed!", e.getName()), inputHex, Hex.encodeHexString(decryptRet).toUpperCase()); 40 | } 41 | } 42 | 43 | } 44 | --------------------------------------------------------------------------------