|
|
|
@ -0,0 +1,97 @@
|
|
|
|
|
package com.supervision.knowsub.util;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.codec.Base64;
|
|
|
|
|
import cn.hutool.core.io.resource.ResourceUtil;
|
|
|
|
|
import cn.hutool.core.util.HexUtil;
|
|
|
|
|
import cn.hutool.crypto.SecureUtil;
|
|
|
|
|
import cn.hutool.crypto.asymmetric.KeyType;
|
|
|
|
|
import cn.hutool.crypto.asymmetric.SM2;
|
|
|
|
|
|
|
|
|
|
import java.security.KeyPair;
|
|
|
|
|
import java.security.PrivateKey;
|
|
|
|
|
import java.security.PublicKey;
|
|
|
|
|
|
|
|
|
|
public class SM2Util {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static SM2 sm2 = getSM2();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 生成密钥; 测试使用,实际使用中请保存到文件中,或者数据库中
|
|
|
|
|
*/
|
|
|
|
|
public static void generateKey() {
|
|
|
|
|
KeyPair pair = SecureUtil.generateKeyPair("SM2");
|
|
|
|
|
PrivateKey privateKey = pair.getPrivate();
|
|
|
|
|
PublicKey publicKey = pair.getPublic();
|
|
|
|
|
|
|
|
|
|
System.out.println("private:"+Base64.encode(privateKey.getEncoded()));
|
|
|
|
|
System.out.println("public:"+Base64.encode(publicKey.getEncoded()));
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 验证签名
|
|
|
|
|
* @param content 需要被验证的内容
|
|
|
|
|
* @param sign 签名
|
|
|
|
|
* @return true:验证通过,false:验证失败
|
|
|
|
|
*/
|
|
|
|
|
public static boolean verify(String content,String sign){
|
|
|
|
|
|
|
|
|
|
return sm2.verifyHex(HexUtil.encodeHexStr(content), sign);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 加密字符串
|
|
|
|
|
* @param content 需要签名的内容
|
|
|
|
|
* @return 加密后的密文 base64编码
|
|
|
|
|
*/
|
|
|
|
|
public static String encryptBase64(String content){
|
|
|
|
|
|
|
|
|
|
return sm2.encryptBase64(content,KeyType.PublicKey);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 对密文进行解密
|
|
|
|
|
* @param data 密文
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static String decrypt(String data){
|
|
|
|
|
|
|
|
|
|
return sm2.decryptStr(data, KeyType.PrivateKey);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 签名
|
|
|
|
|
* @param content 需要签名的内容
|
|
|
|
|
* @return 签名字符串 结果固定为142位
|
|
|
|
|
*/
|
|
|
|
|
public static String sign(String content){
|
|
|
|
|
return sm2.signHex(HexUtil.encodeHexStr(content));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static SM2 getSM2(){
|
|
|
|
|
String privateKey = ResourceUtil.readUtf8Str("sm2_private.key");
|
|
|
|
|
String publicKey = ResourceUtil.readUtf8Str("sm2_public.key");
|
|
|
|
|
return new SM2(privateKey, publicKey);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
|
|
|
|
|
String content = "张三里斯忘我ss **()((##";
|
|
|
|
|
|
|
|
|
|
String encryptBase64 = encryptBase64(content);
|
|
|
|
|
System.out.println("加密结果:"+encryptBase64);
|
|
|
|
|
|
|
|
|
|
String decrypt = decrypt(encryptBase64);
|
|
|
|
|
System.out.println("解密结果:"+decrypt);
|
|
|
|
|
|
|
|
|
|
String sign = sign(content);
|
|
|
|
|
System.out.println("签名结果:"+sign);
|
|
|
|
|
boolean verify = verify(content, sign);
|
|
|
|
|
System.out.println("正向验证结果:"+verify);
|
|
|
|
|
|
|
|
|
|
boolean verify1 = verify("djaoidf", sign);
|
|
|
|
|
System.out.println("反向验证结果:"+verify1);
|
|
|
|
|
}
|
|
|
|
|
}
|