|
@@ -7,6 +7,7 @@ import cn.hutool.crypto.SecureUtil;
|
|
import cn.hutool.crypto.SmUtil;
|
|
import cn.hutool.crypto.SmUtil;
|
|
import cn.hutool.crypto.asymmetric.KeyType;
|
|
import cn.hutool.crypto.asymmetric.KeyType;
|
|
import cn.hutool.crypto.asymmetric.SM2;
|
|
import cn.hutool.crypto.asymmetric.SM2;
|
|
|
|
+import com.jiayue.ssi.constant.SecretKeyConstants;
|
|
import com.jiayue.ssi.util.SM2CryptUtils;
|
|
import com.jiayue.ssi.util.SM2CryptUtils;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.bouncycastle.asn1.gm.GMNamedCurves;
|
|
import org.bouncycastle.asn1.gm.GMNamedCurves;
|
|
@@ -40,11 +41,18 @@ import java.util.Base64;
|
|
public class BcSm2Util {
|
|
public class BcSm2Util {
|
|
public static void main(String[] args) {
|
|
public static void main(String[] args) {
|
|
//需要加密的明文
|
|
//需要加密的明文
|
|
- String text = "测试";
|
|
|
|
- String pv = "6155d63ee27cbeca07f3e40c4f8856f1be8119fcbda1aadc7e0e595e52bad7bd";
|
|
|
|
- String pu = "041967638ca43d4577d8dba166bff4437fde944270101f398a95b846ec2f8177d09f8abc5d62b6cd2c7216274d7abe0c8e04b0bb691207a32dd2e12d6bd2798672";
|
|
|
|
|
|
+// String text = "测试";
|
|
|
|
+// String pv = "6155d63ee27cbeca07f3e40c4f8856f1be8119fcbda1aadc7e0e595e52bad7bd";
|
|
|
|
+// String pu = "041967638ca43d4577d8dba166bff4437fde944270101f398a95b846ec2f8177d09f8abc5d62b6cd2c7216274d7abe0c8e04b0bb691207a32dd2e12d6bd2798672";
|
|
|
|
+//
|
|
|
|
+
|
|
|
|
+// String a = SM2CryptUtils.decrypt("8fe02b3e2bd96292691c899e1e3008ec076708a56c509ff8572a96f7da45f7d649f441d988479e14d6f0230520b1c02f6b2df95f49f4833d791757fc5a6aee6d46ed445c60856d8e625aa92fa64d09a7c3f22705d82f0a3bfe5e91b99ee013587fc6c4240c7269989e", "6155d63ee27cbeca07f3e40c4f8856f1be8119fcbda1aadc7e0e595e52bad7bd");
|
|
|
|
+// System.out.println(a);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
//创建sm2 对象
|
|
//创建sm2 对象
|
|
- SM2 sm2 = SmUtil.sm2(pv,pu);
|
|
|
|
|
|
+// SM2 sm2 = SmUtil.sm2(pv,pu);
|
|
//这里会自动生成对应的随机秘钥对 , 注意! 这里一定要强转,才能得到对应有效的秘钥信息
|
|
//这里会自动生成对应的随机秘钥对 , 注意! 这里一定要强转,才能得到对应有效的秘钥信息
|
|
// byte[] privateKey = BCUtil.encodeECPrivateKey(sm2.getPrivateKey());
|
|
// byte[] privateKey = BCUtil.encodeECPrivateKey(sm2.getPrivateKey());
|
|
// //这里公钥不压缩 公钥的第一个字节用于表示是否压缩 可以不要
|
|
// //这里公钥不压缩 公钥的第一个字节用于表示是否压缩 可以不要
|
|
@@ -55,21 +63,41 @@ public class BcSm2Util {
|
|
// System.out.println("私钥: " + HexUtil.encodeHexStr(pv.getBytes()));
|
|
// System.out.println("私钥: " + HexUtil.encodeHexStr(pv.getBytes()));
|
|
// System.out.println("公钥: " + HexUtil.encodeHexStr(pu.getBytes()));
|
|
// System.out.println("公钥: " + HexUtil.encodeHexStr(pu.getBytes()));
|
|
//得到明文对应的字节数组
|
|
//得到明文对应的字节数组
|
|
- byte[] dateBytes = text.getBytes();
|
|
|
|
- System.out.println("数据: " + HexUtil.encodeHexStr(dateBytes));
|
|
|
|
- //这里需要手动设置,sm2 对象的默认值与我们期望的不一致
|
|
|
|
- sm2.setMode(SM2Engine.Mode.C1C3C2);
|
|
|
|
- sm2.setEncoding(new PlainDSAEncoding());
|
|
|
|
- //计算签名
|
|
|
|
- byte[] sign = sm2.sign(dateBytes, null);
|
|
|
|
- System.out.println("签名: " + HexUtil.encodeHexStr(sign));
|
|
|
|
- // 校验 验签
|
|
|
|
- boolean verify = sm2.verify(dateBytes, sign);
|
|
|
|
- System.out.println(verify);
|
|
|
|
|
|
+// byte[] dateBytes = text.getBytes();
|
|
|
|
+// System.out.println("数据: " + HexUtil.encodeHexStr(dateBytes));
|
|
|
|
+// //这里需要手动设置,sm2 对象的默认值与我们期望的不一致
|
|
|
|
+// sm2.setMode(SM2Engine.Mode.C1C3C2);
|
|
|
|
+// sm2.setEncoding(new PlainDSAEncoding());
|
|
|
|
+// //计算签名
|
|
|
|
+// byte[] sign = sm2.sign(dateBytes, null);
|
|
|
|
+// System.out.println("签名: " + HexUtil.encodeHexStr(sign));
|
|
|
|
+// // 校验 验签
|
|
|
|
+// boolean verify = sm2.verify(dateBytes, sign);
|
|
|
|
+// System.out.println(verify);
|
|
|
|
+//
|
|
|
|
+//
|
|
|
|
+// String signold = SM2CryptUtils.sign(pv,text);
|
|
|
|
+// System.out.println(signold);
|
|
|
|
+
|
|
|
|
+// // SM3加密密码
|
|
|
|
+// String sm3password = SmUtil.sm3("Xsl1234567890abcdefg").toUpperCase();
|
|
|
|
+ // 再对密码sm2
|
|
|
|
+
|
|
|
|
+ String keystore = SM2CryptUtils.encrypt("s37d1gxm",SecretKeyConstants.SERVER_PUBLIC_KEY);
|
|
|
|
+ System.out.println("keystore:"+keystore);
|
|
|
|
+
|
|
|
|
+ System.out.println("==========mysql配置==========");
|
|
|
|
+ String sm2dbusername = SM2CryptUtils.encrypt("root",SecretKeyConstants.SERVER_PUBLIC_KEY);
|
|
|
|
+ System.out.println("数据库用户名:"+sm2dbusername);
|
|
|
|
+ String sm2dbpwd = SM2CryptUtils.encrypt("!QAZ2root",SecretKeyConstants.SERVER_PUBLIC_KEY);
|
|
|
|
+ System.out.println("数据库密码:"+sm2dbpwd);
|
|
|
|
|
|
|
|
+ System.out.println("==========mail配置==========");
|
|
|
|
+ String mailUserName = SM2CryptUtils.encrypt("xushilong@jiayuepowertech.com",SecretKeyConstants.SERVER_PUBLIC_KEY);
|
|
|
|
+ System.out.println("mailUserName:"+mailUserName);
|
|
|
|
+ String mailPassWord = SM2CryptUtils.encrypt("jiayue123456",SecretKeyConstants.SERVER_PUBLIC_KEY);
|
|
|
|
+ System.out.println("mailPassWord:"+mailPassWord);
|
|
|
|
|
|
- String signold = SM2CryptUtils.sign(pv,text);
|
|
|
|
- System.out.println(signold);
|
|
|
|
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|