package com.security; import java.io.IOException; import org.bouncycastle.util.encoders.Base64; import org.bouncycastle.util.encoders.Hex; /** * * @ClassName: DemoMain * @Description: TODO(国密SM2签名验签/SM3报文摘要) * @date 2019年5月10日 */ public class DemoMain { // 国密规范测试用户ID private static final String userId = "ALICE123@YAHOO.COM"; // 国密规范测试私钥 private static final String prik = "128B2FA8BD433C6C068C8D803DFF79792A519A55171B1B650C23661D15897263"; //国密规范测试公钥 private static final String pubk = "040AE4C7798AA0F119471BEE11825BE46202BB79E2A5844495E97C04FF4DF2548A7C0240F88F1CD4E16352A73C17B7F16F07353E53A176D684A9FE0C6BB798E857"; public static void main(String[] arg) { String msg = "jdbc:mysql://127.0.0.1:3306/paysystem?characterEncoding=utf8&useSSL=false&zeroDateTimeBehavior=convertToNull";//原始数据 String summaryString = summary(msg); System.out.println("摘要:"+summaryString); String signString = sign(summaryString); System.out.println("摘要签名:"+signString); boolean status = verify(summaryString,signString); System.out.println("验签结果:"+status); System.out.println("加密: "); byte[] cipherText = null; try { cipherText = SM2Utils.encrypt(Base64.decode(new String(Base64.encode(Util.hexToByte(pubk))).getBytes()), msg.getBytes()); } catch (IllegalArgumentException e1) { // TODO 自动生成的 catch 块 e1.printStackTrace(); } catch (IOException e1) { // TODO 自动生成的 catch 块 e1.printStackTrace(); } System.out.println(new String(Base64.encode(cipherText))); System.out.println(""); System.out.println("解密: "); String res = null; try { res = new String(SM2Utils.decrypt(Base64.decode(new String(Base64.encode(Util.hexToByte(prik))).getBytes()), cipherText)); } catch (IllegalArgumentException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } System.out.println(res); } /** * 摘要 * @return */ public static String summary(String msg) { //1.摘要 byte[] md = new byte[32]; SM3Digest sm = new SM3Digest(); sm.update(msg.getBytes(), 0, msg.getBytes().length); sm.doFinal(md, 0); String s = new String(Hex.encode(md)); return s.toUpperCase(); } /** * 签名 * @return */ public static String sign(String summaryString) { String prikS = new String(Base64.encode(Util.hexToByte(prik))); System.out.println("prikS: " + prikS); System.out.println(""); System.out.println("ID: " + Util.getHexString(userId.getBytes())); System.out.println(""); System.out.println("签名: "); byte[] sign = null; //摘要签名 try { sign = SM2Utils.sign(userId.getBytes(), Base64.decode(prikS.getBytes()), Util.hexToByte(summaryString)); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return Util.getHexString(sign); } /** * 验签 * @return */ public static boolean verify(String summary,String sign) { String pubkS = new String(Base64.encode(Util.hexToByte(pubk))); System.out.println("pubkS: " + pubkS); System.out.println(""); System.out.println("验签 "); boolean vs = false; //验签结果 try { vs = SM2Utils.verifySign(userId.getBytes(), Base64.decode(pubkS.getBytes()), Util.hexToByte(summary), Util.hexToByte(sign)); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return vs; } }