/*** 国密SM3,消息摘要** @author Luke*/
@Slf4j
public class SM3Util {private static char[] chars = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};public static final byte[] IV = {0x73, (byte) 0x80, 0x16, 0x6f, 0x49, 0x14, (byte) 0xb2, (byte) 0xb9, 0x17, 0x24, 0x42,(byte) 0xd7, (byte) 0xda, (byte) 0x8a, 0x06, 0x00, (byte) 0xa9, 0x6f, 0x30, (byte) 0xbc, (byte) 0x16, 0x31,0x38, (byte) 0xaa, (byte) 0xe3, (byte) 0x8d, (byte) 0xee, 0x4d, (byte) 0xb0, (byte) 0xfb, 0x0e, 0x4e};private static final Integer TJ_15 = Integer.valueOf("79cc4519", 16);private static final Integer TJ_63 = Integer.valueOf("7a879d8a", 16);private static final byte[] FirstPadding = {(byte) 0x80};private static final byte[] ZeroPadding = {(byte) 0x00};private static int T(int j) {if (j >= 0 && j <= 15) {return TJ_15.intValue();} else if (j >= 16 && j <= 63) {return TJ_63.intValue();} else {throw new RuntimeException("data invalid");}}private static Integer FF(Integer x, Integer y, Integer z, int j) {if (j >= 0 && j <= 15) {return Integer.valueOf(x.intValue() ^ y.intValue() ^ z.intValue());} else if (j >= 16 && j <= 63) {return Integer.valueOf((x.intValue() & y.intValue()) | (x.intValue() & z.intValue()) | (y.intValue() & z.intValue()));} else {throw new RuntimeException("data invalid");}}private static Integer GG(Integer x, Integer y, Integer z, int j) {if (j >= 0 && j <= 15) {return Integer.valueOf(x.intValue() ^ y.intValue() ^ z.intValue());} else if (j >= 16 && j <= 63) {return Integer.valueOf((x.intValue() & y.intValue()) | (~x.intValue() & z.intValue()));} else {throw new RuntimeException("data invalid");}}private static Integer P0(Integer x) {return Integer.valueOf(x.intValue() ^ Integer.rotateLeft(x.intValue(), 9) ^ Integer.rotateLeft(x.intValue(), 17));}private static Integer P1(Integer x) {return Integer.valueOf(x.intValue() ^ Integer.rotateLeft(x.intValue(), 15) ^ Integer.rotateLeft(x.intValue(), 23));}private static byte[] padding(byte[] source) throws IOException {if (source.length >= 0x2000000000000000L) {throw new RuntimeException("src data invalid.");}long l = source.length * 8;long k = 448 - (l + 1) % 512;if (k < 0) {k = k + 512;}if (log.isDebugEnabled()) {log.debug("k = " + k);}try (ByteArrayOutputStream baos = new ByteArrayOutputStream();) {baos.write(source);baos.write(FirstPadding);long i = k - 7;while (i > 0) {baos.write(ZeroPadding);i -= 8;}baos.write(long2bytes(l));if (log.isDebugEnabled()) {log.debug("paded size = " + baos.size());}return baos.toByteArray();}}private static byte[] long2bytes(long l) {byte[] bytes = new byte[8];for (int i = 0; i < 8; i++) {bytes[i] = (byte) (l >>> ((7 - i) * 8));}return bytes;}public static String encodeSM3(String source) throws IOException {byte[] b = encodeSM3(source.getBytes());return ConvertUtil.byteToHex(b);}public static byte[] encodeSM3(byte[] source) throws IOException {byte[] m1 = padding(source);int n = m1.length / (512 / 8);if (log.isDebugEnabled()) {log.debug("n = " + n);}byte[] b;byte[] vi = IV.clone();byte[] vi1 = null;for (int i = 0; i < n; i++) {b = Arrays.copyOfRange(m1, i * 64, (i + 1) * 64);vi1 = CF(vi, b);vi = vi1;}return vi1;}private static byte[] CF(byte[] vi, byte[] bi) throws IOException {int a, b, c, d, e, f, g, h;a = toInteger(vi, 0);b = toInteger(vi, 1);c = toInteger(vi, 2);d = toInteger(vi, 3);e = toInteger(vi, 4);f = toInteger(vi, 5);g = toInteger(vi, 6);h = toInteger(vi, 7);int[] w = new int[68];int[] w1 = new int[64];for (int i = 0; i < 16; i++) {w[i] = toInteger(bi, i);}for (int j = 16; j < 68; j++) {w[j] = P1(w[j - 16] ^ w[j - 9] ^ Integer.rotateLeft(w[j - 3], 15)) ^ Integer.rotateLeft(w[j - 13], 7)^ w[j - 6];}for (int j = 0; j < 64; j++) {w1[j] = w[j] ^ w[j + 4];}int ss1, ss2, tt1, tt2;for (int j = 0; j < 64; j++) {ss1 = Integer.rotateLeft(Integer.rotateLeft(a, 12) + e + Integer.rotateLeft(T(j), j), 7);ss2 = ss1 ^ Integer.rotateLeft(a, 12);tt1 = FF(a, b, c, j) + d + ss2 + w1[j];tt2 = GG(e, f, g, j) + h + ss1 + w[j];d = c;c = Integer.rotateLeft(b, 9);b = a;a = tt1;h = g;g = Integer.rotateLeft(f, 19);f = e;e = P0(tt2);}byte[] v = toByteArray(a, b, c, d, e, f, g, h);for (int i = 0; i < v.length; i++) {v[i] = (byte) (v[i] ^ vi[i]);}return v;}private static int toInteger(byte[] source, int index) {StringBuilder valueStr = new StringBuilder("");for (int i = 0; i < 4; i++) {valueStr.append(chars[(byte) ((source[index * 4 + i] & 0xF0) >> 4)]);valueStr.append(chars[(byte) (source[index * 4 + i] & 0x0F)]);}return Long.valueOf(valueStr.toString(), 16).intValue();}private static byte[] toByteArray(int a, int b, int c, int d, int e, int f, int g, int h) throws IOException {try (ByteArrayOutputStream baos = new ByteArrayOutputStream(32);) {baos.write(toByteArray(a));baos.write(toByteArray(b));baos.write(toByteArray(c));baos.write(toByteArray(d));baos.write(toByteArray(e));baos.write(toByteArray(f));baos.write(toByteArray(g));baos.write(toByteArray(h));return baos.toByteArray();}}private static byte[] toByteArray(int i) {byte[] byteArray = new byte[4];byteArray[0] = (byte) (i >>> 24);byteArray[1] = (byte) ((i & 0xFFFFFF) >>> 16);byteArray[2] = (byte) ((i & 0xFFFF) >>> 8);byteArray[3] = (byte) (i & 0xFF);return byteArray;}private SM3Util() {}
}

国密SM3加密算法工具类(非对称)相关推荐

  1. 国密算法java语言的实现:利用bcprov和hutool库分别实现国密SM4算法工具类,对称密钥

    SM4算法成为行业标准: SM4分组密码算法是2012年3月21日实施的一项行业标准: 2021年6月25日,我国SM4分组密码算法作为国际标准ISO/IEC 18033-3:2010/AMD1:20 ...

  2. Java实现MD5和国密SM3摘要算法

    一.JDK提供的默认MD5算法工具 MD5算法类型枚举 /*** JDK提供的默认摘要算法.** @author linzp* @date 2022/2/8 16:46*/ public enum D ...

  3. C#国密SM2加密算法实现

    最近在做数据上报,上报数据需要使用国密SM2加密算法加密后上传,以前没接触过过这个东东,所以做个简单记录,平台提供给加密的公钥,让后我们根据公钥将数据加密后,提交给接口,以保证数据安全传输. 实现代码 ...

  4. 国密SM3密码杂凑算法原理及实现(附源码)

    相关文章: 国密SM3哈希算法原理及实现(附源码) SHA1哈希算法原理及实现(附源码) MD5哈希算法原理及实现(附源码) MD4哈希算法原理及实现(附源码) MD2哈希算法原理及实现(附源码) M ...

  5. php 国密 签名,关于php国密SM3签名算法

    推荐:<PHP视频教程> php国密SM3签名算法 代码地址 github.com/lizhichao/sm 安装 composer require lizhichao/one-sm 使用 ...

  6. C++安全方向(三):3.7 使用openssl_evp接口完成SHA3和国密SM3

    EVP接口 ·EVP系列函数主要封装了加密,摘要,编码三大类型的算法. ·EVP_MD_CTX摘要上下文 ·EVP_MD_CTX_new() ·EVP_MD_CTX_free() ·EVP_MD 摘要 ...

  7. 使用OpenSSL_EVP接口完成SHA3和国密SM3散列生成

    unsigned char data[128] = "测试EVP SHA3 国密SM3";int data_size = strlen((char*)data);//初始化EVP上 ...

  8. 实现基于国密SM3的密钥派生(KDF)功能

    实现基于国密SM3的密钥派生(KDF) 前言 KDF 标准 基于SM3的kdf实现 前言 密钥派生函数(KDF):密钥派生函数是指从一个共享的秘密比特串中派生密钥数据,在密钥协商过程中,密钥派生函数作 ...

  9. php 国密,PHP关于国密SM3加密的处理

    这几天对接中国联通物联网IoT Gateway门户API系统,其中用到国密SM3,其提供的token需使用国密SM3对源串进行加密处理,网上搜索也没找到什么函数之类的,只有c和java语言写的,看来要 ...

最新文章

  1. Linux Shell 只列出目录的方法
  2. 操作系统 实验三 进程调度模拟程序
  3. 实现在CentOS7环境下搭建个人github博客
  4. Ubuntu 14.04 更换为阿里云源
  5. oracle11g导入错误,oracle 11g导入到10g引起的错误
  6. 清空 linux 服务器,Linux服务器清理
  7. ue4 运行禁用鼠标_从零开始——三:关闭电脑无用服务提高运行速度
  8. datasnap——动态注册服务类
  9. win7仿win98电脑主题
  10. 直流电机单闭环调速matlab仿真,单闭环直流电机调速Simulink仿真.docx
  11. python 组合优化_python中的多周期投资组合优化
  12. Soul打造冬奥捏脸大赛 在创意玩法中传递奥运精神
  13. 使用u盘如何装linux系统教程视频教程,使用U盘安装Linux系统图解一
  14. V模型、W模型、H模型示意图以及优缺点对比
  15. 女朋友为我写了一个防猝死插件
  16. jquery UI 跟随学习笔记——拖拽(Draggable)
  17. python箭头向下怎么变_实现点击下箭头变上箭头来回切换的两种方法【推荐】
  18. INFJ的人格成长阶段与功能类型
  19. C语言中数据类型的相关定义与用法
  20. 宗地图绘制要求和规范_宗地图怎样绘制 宗地图绘制方法有哪些-【领仕网】

热门文章

  1. 小学的四则运算(输入结果)
  2. Flash设置(各种版本浏览器包括低版本IE)
  3. 【UE4 第一人称射击游戏】08-使用“AK47”发射子弹
  4. vr视频转换教程,怎么把普通视频转换成vr视频?
  5. try catch 资源自动释放: AutoCloseable
  6. 在做竞品分析时遇到的一些坑
  7. S03_CH02_AXI_DMA PL发送数据到PS
  8. 微信公众号开发-初学者-SAE新浪云申请及自定义菜单
  9. QuantLib 金融计算——基本组件之天数计算规则详解
  10. MySQL基础语法与JDBC