DemoMain.java 3.5 KB
Newer Older
梦里藍天's avatar
创建  
梦里藍天 已提交
1 2 3 4 5 6
package com.security;

import java.io.IOException;

import org.bouncycastle.util.encoders.Base64;
import org.bouncycastle.util.encoders.Hex;
梦里藍天's avatar
优化  
梦里藍天 已提交
7

梦里藍天's avatar
创建  
梦里藍天 已提交
8 9 10 11 12 13 14
/**
 * 
 * @ClassName: DemoMain 
 * @Description: TODO(国密SM2签名验签/SM3报文摘要) 
 * @date 2019年5月10日
 */
public class DemoMain {
梦里藍天's avatar
优化  
梦里藍天 已提交
15 16 17 18 19 20
	// 国密规范测试用户ID
	private static final String userId = "ALICE123@YAHOO.COM";
	// 国密规范测试私钥
	private static final String prik = "128B2FA8BD433C6C068C8D803DFF79792A519A55171B1B650C23661D15897263";
	//国密规范测试公钥
	private static final String pubk = "040AE4C7798AA0F119471BEE11825BE46202BB79E2A5844495E97C04FF4DF2548A7C0240F88F1CD4E16352A73C17B7F16F07353E53A176D684A9FE0C6BB798E857";
梦里藍天's avatar
创建  
梦里藍天 已提交
21 22
	
	public static void main(String[] arg) {
梦里藍天's avatar
优化  
梦里藍天 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
		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) {
梦里藍天's avatar
创建  
梦里藍天 已提交
65 66 67 68 69 70
		//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));
梦里藍天's avatar
优化  
梦里藍天 已提交
71 72 73 74 75 76 77 78
		return s.toUpperCase();
	}
	
	/**
	  * 签名
	 * @return
	 */
	public static String sign(String summaryString) {
梦里藍天's avatar
创建  
梦里藍天 已提交
79 80 81
		String prikS = new String(Base64.encode(Util.hexToByte(prik)));
		System.out.println("prikS: " + prikS);
		System.out.println("");
梦里藍天's avatar
优化  
梦里藍天 已提交
82
		
梦里藍天's avatar
创建  
梦里藍天 已提交
83 84 85 86 87
		System.out.println("ID: " + Util.getHexString(userId.getBytes()));
		System.out.println("");
		System.out.println("签名: ");
		byte[] sign = null; //摘要签名
		try {
梦里藍天's avatar
优化  
梦里藍天 已提交
88
			sign = SM2Utils.sign(userId.getBytes(), Base64.decode(prikS.getBytes()), Util.hexToByte(summaryString));
梦里藍天's avatar
创建  
梦里藍天 已提交
89 90 91 92 93
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
梦里藍天's avatar
优化  
梦里藍天 已提交
94 95 96 97 98 99 100 101
		return Util.getHexString(sign);
	}
	
	/**
	 * 验签
	 * @return 
	 */
	public static boolean verify(String summary,String sign) {
梦里藍天's avatar
创建  
梦里藍天 已提交
102 103 104 105 106 107 108
		String pubkS = new String(Base64.encode(Util.hexToByte(pubk)));
		System.out.println("pubkS: " + pubkS);
		System.out.println("");
		
		System.out.println("验签 ");
		boolean vs = false; //验签结果
		try {
梦里藍天's avatar
优化  
梦里藍天 已提交
109
			vs = SM2Utils.verifySign(userId.getBytes(), Base64.decode(pubkS.getBytes()), Util.hexToByte(summary), Util.hexToByte(sign));
梦里藍天's avatar
创建  
梦里藍天 已提交
110 111 112 113 114
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
梦里藍天's avatar
优化  
梦里藍天 已提交
115
		return vs;
梦里藍天's avatar
创建  
梦里藍天 已提交
116
	}
梦里藍天's avatar
优化  
梦里藍天 已提交
117 118
	
	
梦里藍天's avatar
创建  
梦里藍天 已提交
119
}