国密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
    }
}
相关推荐
星浩AI8 分钟前
Google 官方发布:让你的 AI 编程助手"边写、边看、边调",像人类开发者一样工作
人工智能·后端·开源
喵了个Code30 分钟前
Spring Boot 3 + Spring Security + OAuth2 + Gateway企业级认证授权平台实现
后端
开心猴爷35 分钟前
除了 Perfdog,如何在 Windows 环境中完成 iOS App 的性能测试工作
后端
桦说编程1 小时前
简单方法实现子任务耗时统计
java·后端·监控
盖世英雄酱581362 小时前
物品超领取损失1万事故复盘(一)
java·后端
凌览2 小时前
别再死磕 Nginx!http-proxy-middleware 低配置起飞
前端·后端
拾玖不会code2 小时前
简单分表场景下的业务发散思考:分表如何保证丝滑?
后端
CryptoRzz2 小时前
印度尼西亚(IDX)股票数据对接开发
java·后端·websocket·web3·区块链
咕白m6252 小时前
通过 C# 快速生成二维码 (QR code)
后端·.net
踏浪无痕3 小时前
架构师如何学习 AI:三个月掌握核心能力的务实路径
人工智能·后端·程序员