国密SM3算法加密

描述

国密SM3算法类似哈希加密(SHA-256),用于传递api调用的时候作为参数加密的一种方式,是不可逆向的;当然用户一些特殊数据,也可以用此种方式来实现

代码

java 复制代码
package org.example.test;

import org.bouncycastle.jce.provider.BouncyCastleProvider;
import java.security.MessageDigest;
import java.security.Security;


public class SM3Test {

        static {
            Security.addProvider(new BouncyCastleProvider()); // 注册BouncyCastle
        }

        public static void main(String[] args) {
            String input = "Hello, SM3!";
            String hash = sm3Hash(input);
            System.out.println("原文: " + input);
            System.out.println("SM3哈希值: " + hash);
        }

        public static String sm3Hash(String input) {
            try {

                // 创建MessageDigest实例
                MessageDigest digest = MessageDigest.getInstance("SM3", "BC");

                // 计算哈希值
                byte[] hashBytes = digest.digest(input.getBytes("UTF-8"));

                // 将字节数组转换为十六进制字符串
                return bytesToHex(hashBytes);
            } catch (Exception e) {
                throw new RuntimeException("SM3哈希计算失败", e);
            }
        }

        private static String bytesToHex(byte[] bytes) {
            StringBuilder hexString = new StringBuilder();
            for (byte b : bytes) {
                String hex = Integer.toHexString(0xff & b);
                if (hex.length() == 1) hexString.append('0');
                hexString.append(hex);
            }
            return hexString.toString();
        }

}

文件传输的时候,加密文件,加密文件的结果可以配合SM4或者SM2进行传输

java 复制代码
package org.example.test;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Security;

public class SM3TestFile {

    public static void main(String[] args) throws IOException, NoSuchAlgorithmException, NoSuchProviderException {



        File file = new File("E:\Test.txt");

        String s = sm3FileHash(file);
        System.out.println(s);


    }

    public static String sm3FileHash(File filePath) throws IOException, NoSuchAlgorithmException, NoSuchProviderException {
        try (InputStream inputStream = new FileInputStream(filePath)) {
            MessageDigest digest = MessageDigest.getInstance("SM3", "BC");
            byte[] buffer = new byte[8192];
            int length;
            while ((length = inputStream.read(buffer)) != -1) {
                digest.update(buffer, 0, length);
            }
            return bytesToHex(digest.digest());
        }
    }



    private static String bytesToHex(byte[] bytes) {
        StringBuilder hexString = new StringBuilder();
        for (byte b : bytes) {
            String hex = Integer.toHexString(0xff & b);
            if (hex.length() == 1) hexString.append('0');
            hexString.append(hex);
        }
        return hexString.toString();
    }

    static {
        Security.addProvider(new BouncyCastleProvider()); // 注册BouncyCastle
    }
}
相关推荐
小华同学ai26 分钟前
千万别错过!这个国产开源项目彻底改变了你的域名资产管理方式,收藏它相当于多一个安全专家!
前端·后端·github
Vowwwwwww30 分钟前
GIT历史存在大文件的解决办法
前端·git·后端
捡田螺的小男孩42 分钟前
京东一面:接口性能优化,有哪些经验和手段
java·后端·面试
艾露z1 小时前
深度解析Mysql中MVCC的工作机制
java·数据库·后端·mysql
前端付豪1 小时前
揭秘网易统一日志采集与故障定位平台揭秘:如何在亿级请求中1分钟定位线上异常
前端·后端·架构
陈随易1 小时前
Lodash 杀手来了!es-toolkit v1.39.0 已完全兼容4年未更新的 Lodash
前端·后端·程序员
未来影子2 小时前
SpringAI(GA):Nacos3下的分布式MCP
后端·架构·ai编程
Hockor2 小时前
写给前端的 Python 教程三(字符串驻留和小整数池)
前端·后端·python
码农之王2 小时前
记录一次,利用AI DeepSeek,解决工作中算法和无限级树模型问题
后端·算法
Wo3Shi4七2 小时前
消息不丢失:生产者收到写入成功响应后消息一定不会丢失吗?
后端·kafka·消息队列