国密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
    }
}
相关推荐
Solis11 小时前
Raft:分布式系统的定海神针
后端·架构
程序员老申11 小时前
第三篇 5 天 12 个 commit:踩坑实录与代码演进
后端·程序员
程序员鱼皮11 小时前
提示词工程已死,Loop Engineering 称王!保姆级教程 + 项目实战
前端·后端·ai编程
Mininglamp_271811 小时前
Vibe Coding 之后是 Vibe Operating?
后端·开源·多智能体·ai agent·mano-p
星哥的编程之路12 小时前
别再调 API 就说自己会 RAG 了,看看真正的企业级 AI 智能体长什么样
后端·面试
长大198812 小时前
C++26 静态反射完整实战:告别宏代码生成,一键实现序列化
后端
yb77912 小时前
Java 21 虚拟线程最佳实践:虚拟线程如何让高并发 Java 服务更轻更快
后端
fliter12 小时前
绕过系统 ICMP:用 rawsock、Npcap 和 WMI 找到默认网卡
后端
AHRIKNOW12 小时前
AFaster:一个开箱即用的 Rust 高性能后端框架模板
后端
小强198812 小时前
C++20 协程从入门到网络服务
后端