SM2前后端交互加解密(已联调测通)

发布时间 2023-05-22 15:00:03作者: 亦寒yihan丶

准备工作:后端(jar包)、前端(js文件)

阿里云盘:
所需文件: https://www.aliyundrive.com/s/wmYT1TMx4az

 

 1.后端java代码SM2工具类:

import com.antherd.smcrypto.sm2.Keypair;
import com.antherd.smcrypto.sm2.Sm2;
import io.netty.util.internal.StringUtil;

public class SM2Encryptor {
    /**
     * 加密,使用公钥
     *
     * @param publicKey
     * @param originalText
     * @return
     */
    public static String encryptText(String publicKey, String originalText) throws Exception {
        if (StringUtil.isNullOrEmpty(publicKey)) {
            throw new Exception("密钥不能为空...");
        }
        if (StringUtil.isNullOrEmpty(originalText)) {
            throw new Exception("明文不能为空...");
        }

        try {
            return Sm2.doEncrypt(originalText, publicKey);//HexUtil.encodeHexStr(cipherText); // 加密结果
        } catch (Exception e) {
            throw new Exception("加密错误:密钥不正确...");
        }
    }

    /**
     * 解密,使用私钥
     *
     * @param privateKey
     * @param cipherText
     * @return
     */
    public static String decryptText(String privateKey, String cipherText) throws Exception {
        if (StringUtil.isNullOrEmpty(privateKey)) {
            throw new Exception("密钥不能为空...");
        }
        if (StringUtil.isNullOrEmpty(cipherText)) {
            throw new Exception("明文不能为空...");
        }
        try {
            return Sm2.doDecrypt(cipherText, privateKey); // new String(sm2.decrypt(sourceData,prvKey)); // 解密结果
        } catch (Exception e) {
            throw new Exception("解密错误:密钥不正确...");
        }
    }

    /**
     * 获取sm2密钥对,
     *
     * @return 返回String[];第0个为公钥,第1个为私钥
     * @throws Exception
     */
    public static String[] generateKeyPair() throws Exception {
        try {
            Keypair keypair = Sm2.generateKeyPairHex();
            String[] result = new String[2];
            if (keypair != null) {
                result[0] = keypair.getPublicKey(); //公钥
                result[1] = keypair.getPrivateKey(); // 私钥
            }
            return result;
        } catch (Exception e) {
            throw new Exception("生成密钥对失败...");
        }
    }

    public static void main(String[] args) throws Exception {
        //调用内部jar方法,生成一对公私钥
        String[] keys = generateKeyPair();
        //公钥
        String publicKey = keys[0];
        System.out.println("公钥" + publicKey);
        //私钥
        String privateKey = keys[1];
        System.out.println("私钥" + privateKey);

        String str = "测试使用SM2加密、解密";

        //加密字符串
        String encryptText = SM2Encryptor.encryptText(publicKey, str);
        System.out.println(encryptText);
        //解密字符串
        String decryptText = SM2Encryptor.decryptText(privateKey, encryptText);
        System.out.println(decryptText);

    }

}

2.前端调用样例

<script>

    $(function () {
        //公钥
        var publicKey = "0496f201087c127de90a49496c8704b7e1d7aa10f12c8874711fcb6639cf617070bc16c5da0e2ef095f39b735837f9afd7df8965b0534023ac1345e6ab93bbe138";
        //私钥
        var privateKey = "0faa8deba74595e0aeb74718cb4b41ae40c3baeb25a0d335e95a19f69baf29d8";
        
        var str = "测试SM2加解密文本ZZ123";
        
        //加密
        var encrText = sm2.doEncrypt(str ,publicKey);
        console.log(encrText)

       //解密
       var decryptText = sm2.doDecrypt(encrText,privateKey)
       console.log(decryptText)
     })
</script>