keshe
发送方a
代码:
package org.example;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.HexUtil;
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;
import org.apache.commons.codec.binary.Base64;
import java.security.KeyPair;
import cn.superfw.crypto.gmsm.sm2.SM2EngineExtend;
import cn.superfw.crypto.gmsm.sm2.SM2KeyPair;
import cn.superfw.crypto.gmsm.sm3.*;
import cn.superfw.crypto.gmsm.sm4.*;
/**
* <pre>
* hutool SmUtil
* </pre>
*
* @author loopy_y
* @since 2022/5/15
*/
public class Main{
public static void main(String[] args) {
// test1();
test2();
// test3();
test4();
// test5();
// test6();
System.out.println("======== 摘要加密算法SM4S,开始 ========");
String key = "A85CA99B75A2CAE27A659F50260C822E";
String input = "20201310hzx 20201303zyb 20201327ljm";
//
// System.out.println("Key:" + key);
// System.out.println("原文:" + input);
//
// System.out.println();
// System.out.println("ECB模式");
//
String output = SM4.encrypt(key, input);
// System.out.println("加密:" + output);
// System.out.println("解密:" + SM4.decrypt(key, output));
//
// System.out.println();
System.out.println("密钥为:A85CA99B75A2CAE27A659F50260C822E");
String iv = "12345678123456781234567812345678";
output = SM4.encrypt(key, input, iv);
System.out.println("加密:" + output);
System.out.println("解密:" + SM4.decrypt(key, output, iv));
}
public static void test2() {
System.out.println("======== SM2使用自定义密钥对加密或解密,开始 ========");
String text = "A85CA99B75A2CAE27A659F50260C822E";
KeyPair pair = SecureUtil.generateKeyPair("SM2");
byte[] privateKey = pair.getPrivate().getEncoded();
System.out.println("私钥:"+ Base64.encodeBase64String(privateKey));
byte[] publicKey = pair.getPublic().getEncoded();
System.out.println("公钥:"+ Base64.encodeBase64String(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("======== SM2使用自定义密钥生成的密钥对加密或解密,结束 ========");
}
public static void test4() {
System.out.println("======== 摘要加密算法SM3,开始 ========");
String text = "20201310hzx 20201303zyb 20201327ljm";
System.out.println("text:" + text);
String digestHex = SmUtil.sm3(text);
System.out.println("digestHex:" + digestHex);
}
// public static void test3() {
// System.out.println("======== SM2签名和验签,开始 ========");
//
// String content = "20201310hzx";
// final SM2 sm2 = SmUtil.sm2();
// String sign = sm2.signHex(HexUtil.encodeHexStr(content));
// System.out.println("sign:" + sign);
//
// boolean verify = sm2.verifyHex(HexUtil.encodeHexStr(content), sign);
// System.out.println("校验结果为:" + verify);
//
// System.out.println("======== SM2签名和验签,结束 ========");
// }
public static void test5() {
// System.out.println("======== 对称加密SM4,开始 ========");
//
// String content = "20201310hzx";
// SymmetricCrypto sm4 = SmUtil.sm4();
//
// String encryptHex = sm4.encryptHex(content);
// System.out.println("密文:" + encryptHex);
// String decryptStr = sm4.decryptStr(encryptHex, CharsetUtil.CHARSET_UTF_8);
// System.out.println("明文:" + decryptStr);
//
// System.out.println("======== 对称加密SM4,结束 ========");
String key = SM4.generateKey();
String input = "国密商密";
String output = SM4.encrypt(key, input);
System.out.println("CBC模式");
String iv = "12345678123456781234567812345678";
output = SM4.encrypt(key, input);
System.out.println("加密:" + output);
System.out.println("解密:" + SM4.decrypt(key, output, iv));
}
/***
* 分开的:私钥签名,公钥验签
*/
// public static void test6(){
// System.out.println("======== 私钥签名公钥验签,开始 ========");
// String txt = "20201310hzx";
//
// String privateKey = "MIGTAgEAMBMGByqGSM49AgEGCCqBHM9VAYItBHkwdwIBAQQgfVNnk3eKogEiEAvYinFGZev31dT4gQqcEAAidzIKhC2gCgYIKoEcz1UBgi2hRANCAATolVEVuON8S9aOpnogLTzXzo0Dx58LMFjex7XPPcPtSmuq1Rh/ZM2qbYdZyzdnca8eCeR+cg+44rb/TyPRlH23";
// String publicKey = "MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAE6JVRFbjjfEvWjqZ6IC08186NA8efCzBY3se1zz3D7UprqtUYf2TNqm2HWcs3Z3GvHgnkfnIPuOK2/08j0ZR9tw==";
//
// SM2 sm2Sign = SmUtil.sm2(privateKey, null);
// byte[] sign = sm2Sign.sign(txt.getBytes(), null);
// System.out.println("sign:" + Base64.encodeBase64String(sign));
//
// SM2 sm2 = SmUtil.sm2(null, publicKey);
// boolean verify = sm2.verify(txt.getBytes(), sign);
// System.out.print("verify:" + verify);
// System.out.println("======== 私钥签名公钥验签,结束 ========");
// }
// public static void test1() {
// System.out.println("======== SM2使用随机生成的密钥对加密或解密,开始 ========");
//
// String text = "20201310hzx";
// 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("======== SM2使用随机生成的密钥对加密或解密,结束 ========");
// }
}
接收方b:
代码:
package org.example;
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.superfw.crypto.gmsm.sm2.SM2EngineExtend;
import cn.superfw.crypto.gmsm.sm2.SM2KeyPair;
import cn.superfw.crypto.gmsm.sm3.*;
import cn.superfw.crypto.gmsm.sm4.*;
import org.apache.commons.codec.binary.Base64;
import java.security.KeyPair;
public class Main {
public static void main(String[] args) {
// String str = "20201310hzx";
//
// // 以非压缩公钥模式生成SM2秘钥对(通常), 对方是其他语言(比如GO语言)实现时也能支持
// // 压缩模式仅限于数据交互的双方都使用BC库的情况
// SM2KeyPair sm2Keys = SM2.generateSm2Keys(false);
//
// String pubKey = sm2Keys.getPublicKey();
// String prvKey = sm2Keys.getPrivateKey();
//
// System.out.println("Private Key: " + prvKey);
// System.out.println("Public Key: " + pubKey);
//
// System.out.println("\n加密解密测试");
// // 加解密测试(C1C2C3模式)
// System.out.println("----- C1C2C3模式 -----");
// try {
// System.out.println("加密前:" + str);
// String enData = SM2.encrypt(pubKey, str, SM2EngineExtend.CIPHER_MODE_BC);
// System.out.println("加密后:" + enData);
// String deData = SM2.decrypt(prvKey, enData, SM2EngineExtend.CIPHER_MODE_BC);
// System.out.println("解密后:" + deData);
// } catch (Exception e) {
// e.printStackTrace();
// System.out.println("加解密测试错误");
// }
//
// // 加解密测试(C1C3C2模式)
// System.out.println("----- C1C3C2模式 -----");
// try {
// System.out.println("加密前:" + str);
// String enData = SM2.encrypt(pubKey, str, SM2EngineExtend.CIPHER_MODE_NORM);
// System.out.println("加密后:" + enData);
// String deData = SM2.decrypt(prvKey, enData, SM2EngineExtend.CIPHER_MODE_NORM);
// System.out.println("解密后:" + deData);
// } catch (Exception e) {
// e.printStackTrace();
// System.out.println("加解密测试错误");
// }
//
// System.out.println("\n签名验签测试");
// // 签名和验签测试
// try {
// System.out.println("数据:" + str);
// String signStr = SM2.sign(prvKey, str);
// System.out.println("签名:" + signStr);
// boolean verify = SM2.verify(pubKey, str, signStr);
// System.out.println("验签结果:" + verify);
// } catch (Exception e) {
// e.printStackTrace();
// System.out.println("签名和验签测试错误");
// }
//
// System.out.println("SM3 国密商密:"+ SM3.digest("国密商密"));
//
//String key = SM4.generateKey();
// String key = "12345678945687";
String key = "A85CA99B75A2CAE27A659F50260C822E";
String input = "20201310hzx 20201303zyb 20201327ljm";
//
// System.out.println("Key:" + key);
// System.out.println("原文:" + input);
//
// System.out.println();
// System.out.println("ECB模式");
//
String output = SM4.encrypt(key, input);
// System.out.println("加密:" + output);
// System.out.println("解密:" + SM4.decrypt(key, output));
//
// System.out.println();
// System.out.println("CBC模式");
String iv = "12345678123456781234567812345678";
output = "31899368C959A79CE03813E7F62DFA349F4BE1B5A6BFCBF44218BB96E44D5825FC3949D49CD0CA94C3B80259EDAC4C67";
// output = SM4.encrypt(key, input, iv);
System.out.println("收到密文:" + output);
System.out.println("解密:" + SM4.decrypt(key, output, iv));
System.out.println("======== 摘要加密算法SM3,开始 ========");
String text = "20201310hzx 20201303zyb 20201327ljm";
System.out.println("text:" + text);
String digestHex = SmUtil.sm3(text);
System.out.println("digestHex:" + digestHex);
System.out.println("收到摘要:: f83fb8fcd8a7cb1cb0957d44fff554f1019997602c33d6371d02aa5c4d80da19");
System.out.println("======== SM2解密,开始 ========");
String text1 = "A85CA99B75A2CAE27A659F50260C822E";
KeyPair pair = SecureUtil.generateKeyPair("SM2");
//
String privateKey1 = "MIGTAgEAMBMGByqGSM49AgEGCCqBHM9VAYItBHkwdwIBAQQg1u3KJYl1jOvZ9/267ugByqR+119LjG1V/ItphTeTjkqgCgYIKoEcz1UBgi2hRANCAAStZ1XXrZk3BX2j0sCyK93NGXyTn0yOi8U5NOiyBpNmJZexEKiKzCecvDmSXsg0R/fDwIjdTztIdaLJealomLb";
byte[] privateKey = Base64.decodeBase64(privateKey1);
System.out.println("私钥:"+ privateKey1);
String publicKey1 ="MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAErWdV162ZNwV9o9LAsivdzRl8k59MjovFOTTosgaTZiWXsRCoiswnnLw5kl7INEf3w8CI3U87SHWiyXmpaJi28w==";
byte[] publicKey = Base64.decodeBase64(publicKey1);
System.out.println("公钥:"+ publicKey1);
SM2 sm2 = SmUtil.sm2(privateKey, publicKey);
String encryptStr = "042A02F981A0C0D2B3D8BA2A8D4144A450FE2BFEF70EEF8CD16979962545F38452B0C3AC57D0953B6D268FFCBE75C57E8E01E1CAC187092060E375A5B923EE3897DBA5654BD633943684C278DF82347628382BC322A3C24A9C410225BC2B43945E23A6ED7DD1415A6B3D2D795834841B74ECA8878922B5DA03A8496186F64DF";
String decryptStr = StrUtil.utf8Str(sm2.decryptFromBcd(encryptStr, KeyType.PrivateKey));
System.out.println("明文:" + "A85CA99B75A2CAE27A659F50260C822E");
System.out.println("使用sm4的密钥:" + "A85CA99B75A2CAE27A659F50260C822E");
}
}