准备jar
bcprov-jdk16-1.46.jar
两个1.class
package sm3ww;
import java.io.UnsupportedEncodingException;
import java.security.Security;
import org.bouncycastle.crypto.digests.MD3Digest;
import org.bouncycastle.crypto.macs.HMac;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
public class SM3Util {
private final static String ENCODING="ISO-8859-1";
static{
Security.addProvider(new BouncyCastleProvider());
}
/**
运行测试类
*/
// public static void main(String[] args) {
// String s ="亿个色啊发额发成个额亿个额亿个色啊发额发成个额";
// String kiy="ddd";
// String s1=new String();
// String s2=new String();
// s1=encode(kiy, s);
// s2=encode(s);
// System.out.println(s1);
// System.out.println(s1.length());
// System.out.println(s2);
// System.out.println(s2.length());
// }
/**
* 校验-无密钥
* @param src
* @param hexString
* @return
*/
public static Boolean verify(String src,String hexString){
Boolean isVer=null;
byte[] bytes;
try {
bytes = src.getBytes(ENCODING);
byte[] hexToByte = HexString.hexToByte(hexString);
byte[] newhash = hash(bytes);
if(Arrays.equals(newhash, hexToByte)){
isVer=true;
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return isVer;
}
/**
* 方式1 sm3算法加密-自定义秘钥
* @param key 秘钥
* @param p 被加密数据
* @return
*/
public static String encode(String key,String p){
String byteToHex="";
try {
byte[] bytes = p.getBytes(ENCODING);
byte[] hash = hash(key.getBytes(ENCODING));
byteToHex = HexString.byteToHex(hash);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return byteToHex;
}
/**
* 方式2 sm3算法加密
* @param p 被加密数据
* @return
*/
public static String encode(String p){
String byteToHex="";
try {
byte[] bytes = p.getBytes(ENCODING);
byte[] hash = hash(bytes);
byteToHex = HexString.byteToHex(hash);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return byteToHex;
}
/**
* 返回长度=32的byte数组
* @param data 被加密的byte数组
* @return
*/
public static byte[] hash(byte[] data){
SM3Digest sm3Digest = new SM3Digest();
sm3Digest.update(data, 0, data.length);
byte[] hash =new byte[sm3Digest.getDigestSize()];
sm3Digest.doFinal(hash, 0);
return hash;
}
/**
* 自定义秘钥加密
* 通过秘钥加密
* @param key 秘钥
* @param data 被加密的byte数组
* @return
*/
public static byte[] hamc(byte[] key,byte[] data){
KeyParameter params=new KeyParameter(key);
SM3Digest sm3Digest = new SM3Digest();
HMac hm=new HMac(sm3Digest);
hm.init(params);
hm.update(data, 0, data.length);
byte[] hash =new byte[hm.getMacSize()];
hm.doFinal(hash, 0);
return hash;
}
}
2.class
package sm3ww;
import java.math.BigInteger;
public class HexString
{
/**
* 整形转换成网络传输的字节流(字节数组)型数据
*
* @param num 一个整型数据
* @return 4个字节的自己数组
*/
public static byte[] intToBytes(int num)
{
byte[] bytes = new byte[4];
bytes[0] = (byte) (0xff & (num >> 0));
bytes[1] = (byte) (0xff & (num >> 8));
bytes[2] = (byte) (0xff & (num >> 16));
bytes[3] = (byte) (0xff & (num >> 24));
return bytes;
}
/**
* 四个字节的字节数据转换成一个整形数据
*
* @param bytes 4个字节的字节数组
* @return 一个整型数据
*/
public static int byteToInt(byte[] bytes)
{
int num = 0;
int temp;
temp = (0x000000ff & (bytes[0])) << 0;
num = num | temp;
temp = (0x000000ff & (bytes[1])) << 8;
num = num | temp;
temp = (0x000000ff & (bytes[2])) << 16;
num = num | temp;
temp = (0x000000ff & (bytes[3])) << 24;
num = num | temp;
return num;
}
/**
* 长整形转换成网络传输的字节流(字节数组)型数据
*
* @param num 一个长整型数据
* @return 4个字节的自己数组
*/
public static byte[] longToBytes(int num)
{
byte[] bytes = new byte[8];
for (int i = 0; i < 8; i++)
{