国密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
    }
}
相关推荐
源代码•宸8 小时前
分布式缓存-GO(项目整体架构简介、Ubuntu 22.04 64位安装GoLang、安装Docker、解决Go module 的依赖问题)
经验分享·分布式·后端·ubuntu·缓存·docker·golang
一 乐8 小时前
智慧养老|基于springboot+小程序社区养老保障系统设计与实现(源码+数据库+文档)
java·前端·数据库·vue.js·spring boot·后端·小程序
ChinaRainbowSea9 小时前
Spring Boot3 + JDK21 的迁移 超详细步骤
java·spring boot·后端·spring
IT_陈寒9 小时前
Python 3.12 新特性实战:10个提升开发效率的隐藏技巧大揭秘
前端·人工智能·后端
老华带你飞9 小时前
旅游|基于Java旅游信息推荐系统(源码+数据库+文档)
java·开发语言·数据库·vue.js·spring boot·后端·旅游
kgduu10 小时前
go ethreum之Trie
开发语言·后端·golang
William_cl10 小时前
【CSDN 专栏】ASP.NET Controller 过滤器详解:ActionFilter(Action 前后逻辑)从入门到避坑
后端·asp.net
悟空码字10 小时前
SpringBoot实现消息推送:让服务器学会“主动搭讪”
java·spring boot·后端
+VX:Fegn089510 小时前
人力资源管理|基于springboot + vue人力资源管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·课程设计
GOTXX11 小时前
性能与可靠双突破:openEuler 服务器场景评测报告
运维·服务器·网络·人工智能·后端·python