国密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
    }
}
相关推荐
ZhengEnCi2 小时前
模型训练完整流程:从数据准备到模型上线的 10 个关键步骤(全文可跑)
后端·算法
王中阳Go2 小时前
后端转 Agent 岗面试怎么答:8 个高频问题与应答口径
人工智能·后端·python·大语言模型
程序猿DD2 小时前
OctaFuse Gateway 2.18.0:MCP 接入支持 OAuth,供应商粘性绑定可视化管理
后端
ZhengEnCi4 小时前
Agent 规则管理:让 Memory 做索引、让 Skill 存规则——上下文上限实测与分工方案
后端
nice_lcj5204 小时前
深入理解 Java 反射:从 Class 对象到方法调用(含性能实测 + 迷你 IOC 实战 + 面试考点)
java·开发语言·后端·面试
骑着蜗牛撵大象3275 小时前
2026 后端 AI 工程化:Spring AI 管调用、MCP 管工具、Agent 内嵌管自治
java·人工智能·后端·spring·ai·可观测性·mcp
摸爬滚打的小李6 小时前
Spring XML + WAR + Tomcat 项目启动
后端
IT_陈寒7 小时前
SpringBoot自动配置的坑,这次终于爬出来了
前端·人工智能·后端
RainbowFish7 小时前
# RAG 与智能体记忆:从原理到工程实践 > 一篇给学习者的总结。
后端
苏三说技术7 小时前
为什么越来越多人用Arthas 4 ?
后端