SM3加密与解密校验

这篇具有很好参考价值的文章主要介绍了SM3加密与解密校验。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

SM3密码杂凑算法是中国国家密码管理局2010年公布的中国商用密码杂凑算法标准。具体算法标准原始文本参见参考文献[1]。该算法于2012年发布为密码行业标准(GM/T 0004-2012),2016年发布为国家密码杂凑算法标准(GB/T 32905-2016)。

SM3适用于商用密码应用中的数字签名和验证,是在[SHA-256]基础上改进实现的一种算法,其安全性和SHA-256相当。SM3和MD5的迭代过程类似,也采用Merkle-Damgard结构。消息分组长度为512位,摘要值长度为256位。

整个算法的执行过程可以概括成四个步骤:消息填充、消息扩展、迭代压缩、输出结果。

sm3加密,JAVA,java,算法,安全

消息填充

SM3的消息扩展步骤是以512位的数据分组作为输入的。因此,我们需要在一开始就把数据长度填充至512位的倍数。数据填充规则和MD5一样,具体步骤如下:

1、先填充一个“1”,后面加上k个“0”。其中k是满足(n+1+k) mod 512 = 448的最小正整数。

2、追加64位的数据长度(bit为单位,大端序存放1。观察算法标准原文附录A运算示例可以推知。)

sm3加密,JAVA,java,算法,安全

消息扩展

SM3的迭代压缩步骤没有直接使用数据分组进行运算,而是使用这个步骤产生的132个消息字。(一个消息字的长度为32位/4个字节/8个16j进制数字)概括来说,先将一个512位数据分组划分为16个消息字,并且作为生成的132个消息字的前16个。再用这16个消息字递推生成剩余的116个消息字。
sm3加密,JAVA,java,算法,安全

迭代压缩

SM3的迭代过程和MD5类似,也是Merkle-Damgard结构。但和MD5不同的是,SM3使用消息扩展得到的消息字进行运算。这个迭代过程可以用这幅图表示:
sm3加密,JAVA,java,算法,安全
初值IV被放在A、B、C、D、E、F、G、H八个32位变量中,其具体数值参见参考文献[1]。整个算法中最核心、也最复杂的地方就在于压缩函数。压缩函数将这八个变量进行64轮相同的计算,一轮的计算过程如下图所示:
sm3加密,JAVA,java,算法,安全
sm3加密,JAVA,java,算法,安全
图中不同的数据流向用不同颜色的箭头表示。

最后,再将计算完成的A、B、C、D、E、F、G、H和原来的A、B、C、D、E、F、G、H分别进行异或,就是压缩函数的输出。这个输出再作为下一次调用压缩函数时的初值。依次类推,直到用完最后一组132个消息字为止。

输出结果

将得到的A、B、C、D、E、F、G、H八个变量拼接输出,就是SM3算法的输出。
sm3加密,JAVA,java,算法,安全文章来源地址https://www.toymoban.com/news/detail-787675.html

JAVA实现SM3加密与校验

  • 导入Maven依赖
<!-- https://mvnrepository.com/artifact/org.bouncycastle/bcprov-jdk15on -->
<dependency>
    <groupId>org.bouncycastle</groupId>
    <artifactId>bcprov-jdk15on</artifactId>
    <version>1.70</version>
</dependency>
  • SM3Utils
private static final String ENCODING = "UTF-8";
    /**
     * 加密
     *
     * @param src 明文
     * @param key 密钥
     * @return
     * @throws Exception
     */
    public static String encrypt(String src, String key) throws Exception {
        return ByteUtils.toHexString(getEncryptByKey(src, key));
    }


    /**
     * SM3加密方式之: 根据自定义密钥进行加密,返回加密后长度为32位的16进制字符串
     *
     * @param src 源数据
     * @param key 密钥
     * @return 加密后长度为32位的16进制字符串
     * @throws Exception
     */
    public static byte[] getEncryptByKey(String src, String key) throws Exception {
        byte[] srcByte = src.getBytes(ENCODING);
        byte[] keyByte = key.getBytes(ENCODING);
        KeyParameter keyParameter = new KeyParameter(keyByte);
        SM3Digest sm3 = new SM3Digest();
        HMac hMac = new HMac(sm3);
        hMac.init(keyParameter);
        hMac.update(srcByte, 0, srcByte.length);
        byte[] result = new byte[hMac.getMacSize()];
        hMac.doFinal(result, 0);
        return result;
    }


    /**
     * 利用源数据+密钥校验与密文是否一致
     *
     * @param src       源数据
     * @param key       密钥
     * @param sm3HexStr 密文
     * @return
     * @throws Exception
     */
    public static boolean verify(String src, String key, String sm3HexStr) throws Exception {
        byte[] sm3HashCode = ByteUtils.fromHexString(sm3HexStr);
        byte[] newHashCode = getEncryptByKey(src, key);
        return Arrays.equals(newHashCode, sm3HashCode);
    }



    /**
     * SM3加密方式之:不提供密钥的方式 SM3加密,返回加密后长度为64位的16进制字符串
     *
     * @param src 明文
     * @return 加密后长度为64位的16进制字符串
     */
    public static String encrypt(String src) {
        return ByteUtils.toHexString(getEncryptBySrcByte(src.getBytes()));
    }

    /**
     * 返回长度为32位的加密后的byte数组
     *
     * @param srcByte
     * @return
     */
    public static byte[] getEncryptBySrcByte(byte[] srcByte) {
        SM3Digest sm3 = new SM3Digest();
        sm3.update(srcByte, 0, srcByte.length);
        byte[] encryptByte = new byte[sm3.getDigestSize()];
        sm3.doFinal(encryptByte, 0);
        return encryptByte;
    }


    /**
     * 校验源数据与加密数据是否一致
     *
     * @param src       源数据
     * @param sm3HexStr 16进制的加密数据
     * @return
     * @throws Exception
     */
    public static boolean verify(String src, String sm3HexStr) throws Exception {
        byte[] sm3HashCode = ByteUtils.fromHexString(sm3HexStr);
        byte[] newHashCode = getEncryptBySrcByte(src.getBytes(ENCODING));
        return Arrays.equals(newHashCode, sm3HashCode);
    }



public static void main(String[] args) throws Exception {
        String srcStr = "今天天气很晴朗";
        String key = "zjqzjq";
        // ******************************自定义密钥加密及校验*****************************************
        String hexStrByKey = SM3Utils.encrypt(srcStr, key);
        System.out.println("带密钥加密后的密文:" + hexStrByKey);

        System.out.println("明文(带密钥)与密文校验结果:" + SM3Utils.verify(srcStr, key, hexStrByKey));

        // ******************************无密钥的加密及校验******************************************
        String hexStrNoKey = SM3Utils.encrypt(srcStr);
        System.out.println("不带密钥加密后的密文:" + hexStrNoKey);

        System.out.println("明文(不带密钥)与密文校验结果:" + SM3Utils.verify(srcStr, hexStrNoKey));

    }

注:SM3算法的实现

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.util.Arrays;

import ltd.snowland.utils.NumberTool;
import ltd.snowland.utils.StreamTool;

/**
 * SM3杂凑算法实现
 * 
 * @author Potato
 *
 */
public class SM3 {

	private static char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E',
			'F' };
	private static final String ivHexStr = "7380166f 4914b2b9 172442d7 da8a0600 a96f30bc 163138aa e38dee4d b0fb0e4e";
	private static final BigInteger IV = new BigInteger(ivHexStr.replaceAll(" ", ""), 16);
	private static final Integer Tj15 = Integer.valueOf("79cc4519", 16);
	private static final Integer Tj63 = 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 Tj15.intValue();
		} else if (j >= 16 && j <= 63) {
			return Tj63.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;
		}
		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));
		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 byte[] hash(byte[] source) throws IOException {
		byte[] m1 = padding(source);
		int n = m1.length / (512 / 8);
		byte[] b;
		byte[] vi = IV.toByteArray();
		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;
	}

	public static byte[] hash(String source) throws Exception {
		return hash(source.getBytes());
	}

	public static byte[] hash(File file) throws Exception {
		if (file.exists()) {
			InputStream inStream = new FileInputStream(file);
			return hash(StreamTool.readInputStream2ByteArray(inStream));
		} else {
			throw new FileNotFoundException();
		}
	}

	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(hexDigits[(byte) ((source[index * 4 + i] & 0xF0) >> 4)]);
			valueStr.append(hexDigits[(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 {
		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();
	}

	public 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 static String byteToHexString(byte b) {
		int n = b;
		if (n < 0)
			n = 256 + n;
		int d1 = n / 16;
		int d2 = n % 16;
		return "" + hexDigits[d1] + hexDigits[d2];
	}

	public static String byteArrayToHexString(byte[] b) {
		StringBuffer resultSb = new StringBuffer();
		for (int i = 0; i < b.length; i++) {
			resultSb.append(byteToHexString(b[i]));
		}
		return resultSb.toString();
	}

	public static String hashHex(byte[] b) {
		try {
			return SM3.byteArrayToHexString(SM3.hash(b));
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String hashHex(String str) {
		return hashHex(str.getBytes());
	}

	public static void main(String[] args) throws IOException {
		System.out.println(SM3.byteArrayToHexString(SM3.hash("abc".getBytes())));
	}
}

到了这里,关于SM3加密与解密校验的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • java 国密算法工具类(支持SM2 SM3 SM4)

    前言 工具采用BC库实现,支持前后端加解密,前端建议使用sm-crypto 引入pom依赖 基本使用如下 国密SM2算法 国密SM3算法 国密SM4算法使用

    2024年02月13日
    浏览(34)
  • 国密算法SM2,SM3,SM4-java实现原理

    SM2是国家密码管理局于2010年12月17日发布的椭圆曲线公钥密码算法,基于ECC。其签名速度与秘钥生成速度都快于RSA,非对称加密,该算法已公开 SM3是中华人民共和国政府采用的一种密码散列函数标准,由国家密码管理局于2010年12月17日发布。SM3主要用数字签名及验证、消息认

    2024年02月13日
    浏览(26)
  • 国密商用密码SM3杂凑算法原理分析与Java实现

    国密SM3算法是我国自研设计的商用密码杂凑算法,是在SHA-256的基础上进行改造的,其安全性与SHA-256相当。《SM3密码杂凑算法》于2010年12月份由国家密码管理局首次发布。后于2012年发布为密码行业标准《GM/T 0004-2012 SM3密码杂凑算法》,2016年发布为国家密码杂凑算法标准《GB

    2024年02月04日
    浏览(33)
  • SM1、SM2、SM3、SM4、同态加密、密态计算、隐私计算和安全多方计算的概念

    为了保障商用密码的安全性,国家密码局制定了一系列密码标准,包括:SM1(SCB2)、SM2、SM3、SM4、SM7、SM9、祖冲之密码算法(ZUC) 等。 SM1、SM4、SM7、祖冲之密码(ZUC)是 对称算法 。 SM2、SM9是 非对称算法 。 SM3是 哈希算法 。 SM1、SM7算法不公开,调用该算法时,需要通过 加

    2024年02月03日
    浏览(24)
  • 探索密码学的未来:SM1、SM2、SM3、SM4、同态加密、密态计算、隐私计算和安全多方计算

    密码算法在现代通信与信息安全中发挥着至关重要的作用,SM1、SM2、SM3、SM4、同态加密、密态计算、隐私计算和安全多方计算等密码算法被广泛应用于各种信息安全领域。本篇博客将会为大家介绍这些密码算法,以及它们在信息安全中的作用和应用。 SM1、SM2、SM3、SM4是中国国

    2024年02月08日
    浏览(31)
  • JAVA集成国密SM3

    国密算法概述:https://blog.csdn.net/qq_38254635/article/details/131801527 SM3杂凑算法 SM3 消息摘要。可以用MD5作为对比理解。该算法已公开。校验结果为256位。 SecretCommon.java Utils.java 测试类:Test.java 使用方法参考测试类即可。 SignatureCommon.java SignatureConstant.java SignatureStrategy.java SignatureS

    2024年02月07日
    浏览(24)
  • 前端国密加解密使用方法SM2、SM3、SM4

       国密算法,即国家商用密码算法。是由国家密码管理局认定和公布的密码算法标准及其应用规范,其中部分密码算法已经成为国际标准。如SM系列密码,SM代表商密,即商业密码,是指用于商业的、不涉及国家秘密的密码技术。      不多废话直接上干货 项目中 可能存在

    2023年04月09日
    浏览(30)
  • 商用密码应用与安全性评估要点笔记(SM3密码杂凑算法)

    1、杂凑密码算法         可以对任意长度的消息M进行压缩,输出定长的消息摘要/杂凑值h,表示为h = H(M)。         一般来说,H具备三个性质:         (1)单向性。已知h,试图找打M满足h=H(M)是困难的。         (2)抗第二/二次原像攻击(弱抗碰撞性)。给定M1,试图

    2024年02月01日
    浏览(38)
  • vue普通加密以及国密SM2、SM3、sm4的使用

    sm-crypto:https://www.npmjs.com/package/sm-crypto 1. SM2是非对称加密算法 它是基于椭圆曲线密码的公钥密码算法标准,其秘钥长度256bit,包含数字签名、密钥交换和公钥加密,用于替换RSA/DH/ECDSA/ECDH等国际算法。可以满足电子认证服务系统等应用需求,由国家密码管理局于2010年12月17号

    2023年04月09日
    浏览(26)
  • 国密SM2/SM3算法

    分类 1、SM1是一种分组加密算法 对称加密算法中的分组加密算法,其分组长度、秘钥长度都是128bit,算法安全保密强度跟 AES 相当,但是算法不公开,仅以IP核的形式存在于芯片中,需要通过加密芯片的接口进行调用。 采用该算法已经研制了系列芯片、智能IC卡、智能密码钥匙

    2024年02月05日
    浏览(49)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包