sm3加密

本文档展示了如何使用Java实现SM3加密算法,包括无密钥和自定义密钥的加密方法,以及校验方法。代码中包含了SM3摘要计算、HMAC计算等关键步骤。
摘要由CSDN通过智能技术生成

准备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++)   
        {  

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值