package SM2;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.BCUtil;
import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.SM2;
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import java.io.IOException;
/**
* 通过工具类hutool进行
* @author Cqi
* @date 2022/12/23
**/
public class SM2Test {
public static void main(String[] args) throws IOException {
//test1();
//test2();
test3();
//test4();
}
public static void test1() {
String data = "123";
String publicKey = "MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAEgbk2jYaFoIFkZt1ybuydUSte9+Kz9+UbSkixkdU1yubGUp4brzqzNJwmV3ZqbIG5ozTG1O6+cFGIQpv3D98d0A==";
String privateKey = "MIGTAgEAMBMGByqGSM49AgEGCCqBHM9VAYItBHkwdwIBAQQgIKDx104mFnwIZGkfj6e15AOmw+yW9yM9qgBJMnqgzMOgCgYIKoEcz1UBgi2hRANCAASBuTaNhoWggWRm3XJu7J1RK1734rP35RtKSLGR1TXK5sZSnhuvOrM0nCZXdmpsgbmjNMbU7r5wUYhCm/cP3x3Q";
//String publicKey = "8682EDB23C846E9EC89A96C404899B108D54E1D40F70D22DC2E31683871B736C940B8477A05E3C5088127BAD889C7D1537FD0CC26036FC932F2733C91618C7EC";
//String privateKey = "5B065C04BEF872D10436C1004ABDEAE1751DDF8774CFB2F99BDD95BD4180BC50";
//KeyPair pair = SecureUtil.generateKeyPair("SM2");
//byte[] privateKey = pair.getPrivate().getEncoded();
//byte[] publicKey = pair.getPublic().getEncoded();
SM2 sm2 = SmUtil.sm2(privateKey, publicKey);
// 公钥加密,私钥解密
//String encryptStr = sm2.encryptBcd(data, KeyType.PublicKey);
//String decryptStr = StrUtil.utf8Str(sm2.decryptStrFromBcd(encryptStr, KeyType.PrivateKey));
//String encryptStr = sm2.encryptBase64(data, KeyType.PublicKey);
//String decryptStr = StrUtil.utf8Str(sm2.decryptStr(encryptStr, KeyType.PrivateKey));//解密为字符串,密文需为Hex(16进制)或Base64字符串
String encryptStr = sm2.encryptHex(data, KeyType.PublicKey);
String decryptStr = StrUtil.utf8Str(sm2.decryptStr("049b13b6c127ecefad6275f3f09b01dc49ae91126a3f873e98cc27e68f066854eeed67dc15607122f030a678b5a6c8d4000c2bebc36b66ba80d3cd1035dc26c65a788ec5713a97a0d9abdfb300b1640ae6ded8a7fad74ed22b164a8204592bee5c1c1e41", KeyType.PrivateKey));//解密为字符串,密文需为Hex(16进制)或Base64字符串
System.out.println("test1加密");
System.out.println(encryptStr);
System.out.println("test1解密:");
System.out.println(decryptStr);
}
public static void test2(){
String data = "123";
String privateKeyHex = "FAB8BBE670FAE338C9E9382B9FB6485225C11A3ECB84C938F10F20A93B6215F0";
String pubX = "9EF573019D9A03B16B0BE44FC8A5B4E8E098F56034C97B312282DD0B4810AFC3";
String pubY = "CC759673ED0FC9B9DC7E6FA38F0E2B121E02654BF37EA6B63FAF2A0D6013EADF";
SM2 sm2 = new SM2(privateKeyHex, pubX, pubY);// OK
//SM2 sm2 = new SM2(null, pubX,pubY);// ERROR
//SM2 sm2 = new SM2(privateKeyHex, null,null);// OK
//String encryptStr = sm2.encryptBcd(data, KeyType.PublicKey);
//String decryptStr = StrUtil.utf8Str(sm2.decryptStrFromBcd(encryptStr, KeyType.PrivateKey));
//String encryptStr = sm2.encryptBase64(data, KeyType.PublicKey);
//String decryptStr = StrUtil.utf8Str(sm2.decryptStr(encryptStr, KeyType.PrivateKey));//解密为字符串,密文需为Hex(16进制)或Base64字符串
String encryptStr = sm2.encryptHex(data, KeyType.PublicKey);
String decryptStr = StrUtil.utf8Str(sm2.decryptStr(encryptStr, KeyType.PrivateKey));//解密为字符串,密文需为Hex(16进制)或Base64字符串
System.out.println("test2加密:");
System.out.println(encryptStr);
System.out.println("test2解密:");
System.out.println(decryptStr);
}
public static void test3() throws IOException {
String data = "123";
String privateKeyHex = "FAB8BBE670FAE338C9E9382B9FB6485225C11A3ECB84C938F10F20A93B6215F0";
String pubXHex = "9EF573019D9A03B16B0BE44FC8A5B4E8E098F56034C97B312282DD0B4810AFC3";
String pubYHex = "CC759673ED0FC9B9DC7E6FA38F0E2B121E02654BF37EA6B63FAF2A0D6013EADF";
ECPublicKeyParameters ecPublicKeyParameters = BCUtil.toSm2Params(pubXHex, pubYHex);
SM2 sm2 = new SM2(null, ecPublicKeyParameters);
//sm2.usePlainEncoding();
//sm2.setMode(SM2Engine.Mode.C1C3C2);
String encryptStr = sm2.encryptHex(data, KeyType.PublicKey);
System.out.println("test2加密:");
System.out.println(encryptStr);
}
public static void test4() throws IOException {
// 16进制字符串进行Base64编码
String hexStr = "0102030405060708090a";
String encode = new BASE64Encoder().encode(HexUtil.decodeHex(hexStr));
System.out.println(encode);
String encode2 = Base64.encode(HexUtil.decodeHex(hexStr));
System.out.println(encode2);
// Base64编码的16进制字符串解码为原16进制字符串
String base64Str = "AQIDBAUGBwgJCg==";
String decode1 = HexUtil.encodeHexStr(new BASE64Decoder().decodeBuffer(base64Str));
System.out.println(decode1);
String decode2 = HexUtil.encodeHexStr(Base64.decode(base64Str));
System.out.println(decode2);
}
}
SM2算法笔记【Hutool】
最新推荐文章于 2024-06-25 14:59:06 发布