|
@@ -29,6 +29,14 @@ import java.util.Base64;
|
|
|
* @author van
|
|
|
*/
|
|
|
public class Sm2Util {
|
|
|
+ /**
|
|
|
+ * 私有key
|
|
|
+ */
|
|
|
+ private static final String PRIVATE_KEY = "ebaedbccd3f730ec1985b945f10429b468c7f3f70e66da19b8657f023e6cded4";
|
|
|
+ /**
|
|
|
+ * 公有key
|
|
|
+ */
|
|
|
+ private static final String PUBLIC_KEY = "04404c634de4deda80486b4a331adf03448a449d980fe040540abe242ba275cc815aada6a63c63e24d4672e14360c72b1819914f49708f25498ededc2217384960";
|
|
|
|
|
|
/**
|
|
|
* 生成 SM2 公私钥对
|
|
@@ -128,6 +136,16 @@ public class Sm2Util {
|
|
|
return encrypt(data.getBytes(StandardCharsets.UTF_8), publicKey);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * SM2加密算法
|
|
|
+ *
|
|
|
+ * @param data 明文数据
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String encrypt(String data) {
|
|
|
+ return encrypt(data.getBytes(StandardCharsets.UTF_8), PUBLIC_KEY);
|
|
|
+ }
|
|
|
+
|
|
|
public static String encrypt(byte[] data, PublicKey publicKey) {
|
|
|
BCECPublicKey key = (BCECPublicKey) publicKey;
|
|
|
return encrypt(data, Hex.toHexString(key.getQ().getEncoded(false)));
|
|
@@ -172,6 +190,16 @@ public class Sm2Util {
|
|
|
* SM2解密算法
|
|
|
*
|
|
|
* @param cipherData hex格式密文
|
|
|
+ * @return 明文
|
|
|
+ */
|
|
|
+ public static String decrypt(String cipherData) {
|
|
|
+ return decrypt(Hex.decode(cipherData), PRIVATE_KEY);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * SM2解密算法
|
|
|
+ *
|
|
|
+ * @param cipherData hex格式密文
|
|
|
* @param privateKey 密钥PrivateKey型
|
|
|
* @return 明文
|
|
|
*/
|
|
@@ -330,7 +358,7 @@ public class Sm2Util {
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
System.out.println("====== sm2utils test ======");
|
|
|
|
|
|
- String M = "data_message";
|
|
|
+ String M = "123456";
|
|
|
System.out.println("明文:\t" + M);
|
|
|
|
|
|
System.out.println("begin 开始生成密钥对>>>");
|
|
@@ -354,6 +382,9 @@ public class Sm2Util {
|
|
|
String text = decrypt(cipherData, priKeyHexString);
|
|
|
System.out.println("解密:\t" + text);
|
|
|
|
|
|
+ text = decrypt("04294f7750ff7ed05e620923b23a9fe9477146983d981f9de135909f66265ec440b110b7ec46568148a011358050671e52af6a69585fb94d44f47d2176827151a7ae1ac1a7f5263dcbabb3eb45b5898901703a533ead697c49e222ca8717c97e6f3269727924fe", priKeyHexString);
|
|
|
+ System.out.println("解密:\t" + text);
|
|
|
+
|
|
|
String sign = sign(M, priKeyHexString);
|
|
|
System.out.println("signvalue\t" + sign);
|
|
|
sign = "3045022100d69b69421985160ac2a116a006525b0a414090d8d4b815dd98f97c74c1de68bd02201bfbddc24b3ee159faf13948f1ba1ef3368a5d019ea4137d2f69d4d5aaf2a05a";
|