新增 SM2Utils、SM3Utils、SM4Utils 国密算法工具类及测试类
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
/**
|
||||
* Copyright (c) 2013-Now http://jeesite.com All rights reserved.
|
||||
* No deletion without permission, or be held responsible to law.
|
||||
*/
|
||||
package com.jeesite.test.codec;
|
||||
|
||||
import com.jeesite.common.codec.EncodeUtils;
|
||||
import com.jeesite.common.codec.SM2Utils;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.PrivateKey;
|
||||
import java.security.PublicKey;
|
||||
|
||||
/**
|
||||
* 国密 SM2 加密解密工具类,非对称加密
|
||||
* @author ThinkGem
|
||||
* @version 2024-07-22
|
||||
*/
|
||||
public class SM2UtilsTest {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
String s = "Hello word! 你好,中文!";
|
||||
System.out.println(s);
|
||||
|
||||
String[] keys = SM2Utils.genKeys();
|
||||
System.out.println("公钥:" + keys[0]);
|
||||
PublicKey publicKey = SM2Utils.toPublicKey(keys[0]);
|
||||
System.out.println("私钥:" + keys[1]);
|
||||
PrivateKey privateKey = SM2Utils.toPrivateKey(keys[1]);
|
||||
|
||||
byte[] data = SM2Utils.encode(s.getBytes(), publicKey);
|
||||
String dataString = EncodeUtils.encodeBase64(data);
|
||||
System.out.println("加密数据:" + dataString);
|
||||
|
||||
byte[] data2 = SM2Utils.decode(data, privateKey);
|
||||
String dataString2 = new String(data2, StandardCharsets.UTF_8);
|
||||
System.out.println("解密数据:" + dataString2);
|
||||
|
||||
byte[] sign = SM2Utils.sign(s.getBytes(), privateKey);
|
||||
System.out.println("数据签名:" + EncodeUtils.encodeBase64(sign));
|
||||
|
||||
boolean b = SM2Utils.verify(s.getBytes(), publicKey, sign);
|
||||
System.out.println("数据验签:" + b);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/**
|
||||
* Copyright (c) 2013-Now http://jeesite.com All rights reserved.
|
||||
* No deletion without permission, or be held responsible to law.
|
||||
*/
|
||||
package com.jeesite.test.codec;
|
||||
|
||||
import com.jeesite.common.codec.SM3Utils;
|
||||
|
||||
/**
|
||||
* 国密 SM3 加密工具类,散列加密,不可逆加密
|
||||
* @author ThinkGem
|
||||
* @version 2024-07-22
|
||||
*/
|
||||
public class SM3UtilsTest {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
String s = "Hello word! 你好,中文!";
|
||||
System.out.println(s);
|
||||
|
||||
String s1 = SM3Utils.sm3(s);
|
||||
System.out.println(s1);
|
||||
|
||||
String key = SM3Utils.genSaltString(8);
|
||||
System.out.println(key);
|
||||
|
||||
String s3 = SM3Utils.sm3(s, key);
|
||||
System.out.println(s3);
|
||||
|
||||
String s4 = SM3Utils.hmacSm3(s, key);
|
||||
System.out.println(s4);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* Copyright (c) 2013-Now http://jeesite.com All rights reserved.
|
||||
* No deletion without permission, or be held responsible to law.
|
||||
*/
|
||||
package com.jeesite.test.codec;
|
||||
|
||||
import com.jeesite.common.codec.EncodeUtils;
|
||||
import com.jeesite.common.codec.SM4Utils;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
* 国密 SM4 加密解密工具类,对称加密
|
||||
* @author ThinkGem
|
||||
* @version 2024-07-22
|
||||
*/
|
||||
public class SM4UtilsTest {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
String s = "Hello word! 你好,中文!";
|
||||
System.out.println(s);
|
||||
|
||||
String k = SM4Utils.genKeyString();
|
||||
System.out.println(k);
|
||||
String s1 = SM4Utils.encode(s, k);
|
||||
System.out.println(s1);
|
||||
String s2 = SM4Utils.decode(s1, k);
|
||||
System.out.println(s2);
|
||||
|
||||
byte[] key = SM4Utils.genKey();
|
||||
byte[] iv = SM4Utils.genIV();
|
||||
byte[] data = SM4Utils.encode(s.getBytes(StandardCharsets.UTF_8), key, iv);
|
||||
System.out.println(EncodeUtils.encodeBase64(data));
|
||||
byte[] data2 = SM4Utils.decode(data, key, iv);
|
||||
System.out.println(new String(data2, StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user