当前位置: 首页 > 工具软件 > sm64 > 使用案例 >

国密算法SM2、SM3、SM4

端木狐若
2023-12-01

<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>4.6.8</version>
    <scope>compile</scope>
</dependency>
<!--Bouncy Castle库-->
<dependency>
    <groupId>org.bouncycastle</groupId>
    <artifactId>bcprov-jdk15on</artifactId>
    <version>1.68</version>
</dependency>

import java.security.KeyPair;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.SM2;
import cn.hutool.crypto.symmetric.SymmetricCrypto;

public class SMUtils {
	static String text = "I am testing";
	
	/**
	 * 随机生成的密钥对加密或解密
	 */
	private static void test1(){
        System.out.println("使用随机生成的密钥对加密或解密");
        SM2 sm2 = SmUtil.sm2();
        // 公钥加密
        String encryptStr = sm2.encryptBcd(text, KeyType.PublicKey);
        System.out.println("公钥加密:" + encryptStr);
        //私钥解密
        String decryptStr = StrUtil.utf8Str(sm2.decryptFromBcd(encryptStr, KeyType.PrivateKey));
        System.out.println("私钥解密:" + decryptStr);
        System.out.println();
	}

	/**
	 * 使用自定义密钥对加密或解密
	 */
	private static void test2(){
        System.out.println("使用自定义密钥对加密或解密");
		KeyPair pair = SecureUtil.generateKeyPair("SM2");
        byte[] privateKey = pair.getPrivate().getEncoded();
        byte[] publicKey = pair.getPublic().getEncoded(); 
        System.out.println("自定义私钥:"+Base64.encode(privateKey));
        System.out.println("自定义公钥:"+Base64.encode(publicKey));        
        
        SM2 sm2 = SmUtil.sm2(privateKey, publicKey);
        // 公钥加密
        String encryptStr = sm2.encryptBcd(text, KeyType.PublicKey);
        System.out.println("公钥加密:" + encryptStr);
        //私钥解密
        String decryptStr = StrUtil.utf8Str(sm2.decryptFromBcd(encryptStr, KeyType.PrivateKey));
        System.out.println("私钥解密:" + decryptStr);
        System.out.println();
	}
	
	/**
	 * 使用OpenSSL生成的SM2公钥和私钥加密或解密
	 */
	private static void test3(){
        System.out.println("使用OpenSSL生成的SM2公钥和私钥加密或解密");
		String privateKey = "MIGTAgEAMBMGByqGSM49AgEGCCqBHM9VAYItBHkwdwIBAQQg/xLnbUETyfQCFOQR6PWcPWkqNjntJdHiv71KoHgpNQ+gCgYIKoEcz1UBgi2hRANCAATF5qNXXr1cZoyvuIuxgS5WSEL+aEmWjDUZe3HGSH/74cJyugxup8i5vGpyUmCXP0yZi7o9xUYj8k+nE150hGlf";
        String publicKey = "MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAExeajV169XGaMr7iLsYEuVkhC/mhJlow1GXtxxkh/++HCcroMbqfIubxqclJglz9MmYu6PcVGI/JPpxNedIRpXw==";
 
        SM2 sm2 = SmUtil.sm2(Base64.decode(privateKey), Base64.decode(publicKey));
        String encryptStr = sm2.encryptBcd(text, KeyType.PublicKey);
        System.out.println("公钥加密:" + encryptStr);
        String decryptStr = StrUtil.utf8Str(sm2.decryptFromBcd(encryptStr, KeyType.PrivateKey));
        System.out.println("私钥解密:" + decryptStr);
        System.out.println();

	}
	
	/***
     * 随机密钥 签名和验签
     */
    public static void test4(){ 
        System.out.println("随机密钥 签名和验签");
        SM2 sm2 = SmUtil.sm2();
        byte[] sign = sm2.sign(text.getBytes());
        System.out.println("签名:"+Base64.encode(sign));
        boolean verify = sm2.verify(text.getBytes(), sign);
        System.out.println("验签:" + verify);
        System.out.println();
    }
    
    /***
     * 自定义密钥对 签名和验签
     */
    public static void test5(){
        System.out.println("自定义密钥对 签名和验签");
        KeyPair pair = SecureUtil.generateKeyPair("SM2");
        byte[] privateKey = pair.getPrivate().getEncoded();
        byte[] publicKey = pair.getPublic().getEncoded(); 
        System.out.println("自定义私钥:"+Base64.encode(privateKey));
        System.out.println("自定义公钥:"+Base64.encode(publicKey));
        
        final SM2 sm2 = new SM2(privateKey, publicKey);
       
        byte[] sign = sm2.sign(text.getBytes());

        System.out.println("签名:"+Base64.encode(sign));
 
        boolean verify = sm2.verify(text.getBytes(), sign);
        System.out.println("验签:" + verify);
        System.out.println();
    }

    /***
     * 分开的:私钥签名,公钥验签
     * 密钥使用OpenSSL生成
     */
    public static void test6(){
        System.out.println("密钥使用OpenSSL生成 私钥签名,公钥验签"); 
        String privateKey = "MIGTAgEAMBMGByqGSM49AgEGCCqBHM9VAYItBHkwdwIBAQQg9IKdhayNNAziG9e/NEnyldsZf0f2IZSC44ZjR/NDwAGgCgYIKoEcz1UBgi2hRANCAAS/aw5OYHaV7vztr6hTWjIBULrp37m/EEhFa2GWy4WWhkGDRkMmCUuJxRWygT6i3orgo/GcKDcnac97b9XGQD/h";
        String publicKey = "MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAEv2sOTmB2le787a+oU1oyAVC66d+5vxBIRWthlsuFloZBg0ZDJglLicUVsoE+ot6K4KPxnCg3J2nPe2/VxkA/4Q==";
 
        SM2 sm2Sign = SmUtil.sm2(Base64.decode(privateKey), null);
        byte[] sign = sm2Sign.sign(text.getBytes(), null);

        System.out.println("签名:"+Base64.encode(sign));
         
        SM2 sm2 = SmUtil.sm2(null, Base64.decode(publicKey));
        boolean verify = sm2.verify(text.getBytes(), sign);
        System.out.println("验签:" + verify);
        System.out.println();
    }

    /**
     * sm3加密
     */
    public static void test31(){    	 
        String sm3 = SmUtil.sm3(text);
        System.out.println("加密后:" + sm3);
    }
 
    /**
     * sm4加解密
     */
    public static void test41() {
        SymmetricCrypto sm4 = SmUtil.sm4(); 
        String encryptHex = sm4.encryptHex(text);
        System.out.println("加密后:" + encryptHex);
        String decryptStr = sm4.decryptStr(encryptHex, CharsetUtil.CHARSET_UTF_8);
        System.out.println("解密后:" + decryptStr);
    }



	public static void main(String[] args) {
		// TODO Auto-generated method stub

        System.out.println("============SM2============start"); 
		test1();
		test2();
		test3();
		test4();
		test5();
		test6();
		System.out.println("============SM2============end");
		System.out.println();
		
        System.out.println("============SM3============start"); 
        test31();
		System.out.println("============SM3============end");

		System.out.println();
        System.out.println("============SM3============start"); 
        test41();
		System.out.println("============SM3============end");
	}

}

Central Repository:

 类似资料: