• Java国密加密SM3代码


    Java国密加密SM3代码


    前言

    提示:使用国密算法的意义:
    随着金融安全上升到国家安全高度,近年来国家有关机关和监管机构站在国家安全和长远战略的高度提出了推动国密算法应用实施、加强行业安全可控的要求。摆脱对国外技术和产品的过度依赖,建设行业网络安全环境,增强我国行业信息系统的“安全可控”能力显得尤为必要和迫切。


    提示:以下是本篇文章正文内容,下面案例可供参考

    一、SM3是什么?

    SM3杂凑算法(vs SHA-256)

    SM3密码杂凑(哈希、散列)算法给出了杂凑函数算法的计算方法和计算步骤,并给出了运算示例。此算法适用于商用密码应用中的数字签名和验证,消息认证码的生成与验证以及随机数的生成,可满足多种密码应用的安全需求。在SM2,SM9标准中使用。

    此算法对输入长度小于2的64次方的比特消息,经过填充和迭代压缩,生成长度为256比特的杂凑值,其中使用了异或,模,模加,移位,与,或,非运算,由填充,迭代过程,消息扩展和压缩函数所构成。具体算法及运算示例见SM3标准。

    2005年,Wang等人给出了MD5算法和SHA-1算法的碰撞攻击方法,现今被广泛应用的MD5算法和SHA-1算法不再是安全的算法。

    SM3密码摘要算法是中国国家密码管理局2010年公布的中国商用密码杂凑算法标准。SM3算法适用于商用密码应用中的数字签名和验证,是在SHA-256基础上改进实现的一种算法。SM3算法采用Merkle-Damgard结构,消息分组长度为512位,摘要值长度为256位。

    SM3算法的压缩函数与SHA-256的压缩函数具有相似的结构,但是SM3算法的设计更加复杂,比如压缩函数的每一轮都使用2个消息字。

    现今为止,SM3算法的安全性相对较高。

    SM3主要用于数字签名及验证、消息认证码生成及验证、随机数生成等,其算法公开。据国家密码管理局表示,其安全性及效率与SHA-256相当。

    二、使用步骤

    1.引入库

    代码如下(示例):

    <!--国密-->
    <dependency>
        <groupId>org.bouncycastle</groupId>
        <artifactId>bcprov-jdk15on</artifactId>
        <version>1.56</version>
    </dependency>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    2.SM3工具类

    代码如下(示例):

    /**
     * @author SongWei
     * @version 1.0.0
     * @ClassName spring-cloud.com.boot.cloud.sm.Sm3Tool.java
     * @Description TODO
     * @createTime 2018年10月28日 14:29:00
     */
    public class Sm3Tool {
        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};
    
        public static int[] Tj = new int[64];
    
        static {
            for (int i = 0; i < 16; i++) {
                Tj[i] = 0x79cc4519;
            }
    
            for (int i = 16; i < 64; i++) {
                Tj[i] = 0x7a879d8a;
            }
        }
    
        public static byte[] CF(byte[] V, byte[] B) {
            int[] v, b;
            v = convert(V);
            b = convert(B);
            return convert(CF(v, b));
        }
    
        private static int[] convert(byte[] arr) {
            int[] out = new int[arr.length / 4];
            byte[] tmp = new byte[4];
            for (int i = 0; i < arr.length; i += 4) {
                System.arraycopy(arr, i, tmp, 0, 4);
                out[i / 4] = bigEndianByteToInt(tmp);
            }
            return out;
        }
    
        private static byte[] convert(int[] arr) {
            byte[] out = new byte[arr.length * 4];
            byte[] tmp = null;
            for (int i = 0; i < arr.length; i++) {
                tmp = bigEndianIntToByte(arr[i]);
                System.arraycopy(tmp, 0, out, i * 4, 4);
            }
            return out;
        }
    
        public static int[] CF(int[] V, int[] B) {
            int a, b, c, d, e, f, g, h;
            int ss1, ss2, tt1, tt2;
            a = V[0];
            b = V[1];
            c = V[2];
            d = V[3];
            e = V[4];
            f = V[5];
            g = V[6];
            h = V[7];
    
            int[][] arr = expand(B);
            int[] w = arr[0];
            int[] w1 = arr[1];
    
            for (int j = 0; j < 64; j++) {
                ss1 = (bitCycleLeft(a, 12) + e + bitCycleLeft(Tj[j], j));
                ss1 = bitCycleLeft(ss1, 7);
                ss2 = ss1 ^ bitCycleLeft(a, 12);
                tt1 = FFj(a, b, c, j) + d + ss2 + w1[j];
                tt2 = GGj(e, f, g, j) + h + ss1 + w[j];
                d = c;
                c = bitCycleLeft(b, 9);
                b = a;
                a = tt1;
                h = g;
                g = bitCycleLeft(f, 19);
                f = e;
                e = P0(tt2);
    
    			/*System.out.print(j+" ");
    			System.out.print(Integer.toHexString(a)+" ");
    			System.out.print(Integer.toHexString(b)+" ");
    			System.out.print(Integer.toHexString(c)+" ");
    			System.out.print(Integer.toHexString(d)+" ");
    			System.out.print(Integer.toHexString(e)+" ");
    			System.out.print(Integer.toHexString(f)+" ");
    			System.out.print(Integer.toHexString(g)+" ");
    			System.out.print(Integer.toHexString(h)+" ");
    			System.out.println("");*/
            }
    //		System.out.println("");
    
            int[] out = new int[8];
            out[0] = a ^ V[0];
            out[1] = b ^ V[1];
            out[2] = c ^ V[2];
            out[3] = d ^ V[3];
            out[4] = e ^ V[4];
            out[5] = f ^ V[5];
            out[6] = g ^ V[6];
            out[7] = h ^ V[7];
    
            return out;
        }
    
        private static int[][] expand(int[] B) {
            int W[] = new int[68];
            int W1[] = new int[64];
            for (int i = 0; i < B.length; i++) {
                W[i] = B[i];
            }
    
            for (int i = 16; i < 68; i++) {
                W[i] = P1(W[i - 16] ^ W[i - 9] ^ bitCycleLeft(W[i - 3], 15))
                        ^ bitCycleLeft(W[i - 13], 7) ^ W[i - 6];
            }
    
            for (int i = 0; i < 64; i++) {
                W1[i] = W[i] ^ W[i + 4];
            }
    
            int arr[][] = new int[][]{W, W1};
            return arr;
        }
    
        private static byte[] bigEndianIntToByte(int num) {
            return back(SecurityUtils.intToBytes(num));
        }
    
        private static int bigEndianByteToInt(byte[] bytes) {
            return SecurityUtils.byteToInt(back(bytes));
        }
    
        private static int FFj(int X, int Y, int Z, int j) {
            if (j >= 0 && j <= 15) {
                return FF1j(X, Y, Z);
            } else {
                return FF2j(X, Y, Z);
            }
        }
    
        private static int GGj(int X, int Y, int Z, int j) {
            if (j >= 0 && j <= 15) {
                return GG1j(X, Y, Z);
            } else {
                return GG2j(X, Y, Z);
            }
        }
    
        // 逻辑位运算函数
        private static int FF1j(int X, int Y, int Z) {
            int tmp = X ^ Y ^ Z;
            return tmp;
        }
    
        private static int FF2j(int X, int Y, int Z) {
            int tmp = ((X & Y) | (X & Z) | (Y & Z));
            return tmp;
        }
    
        private static int GG1j(int X, int Y, int Z) {
            int tmp = X ^ Y ^ Z;
            return tmp;
        }
    
        private static int GG2j(int X, int Y, int Z) {
            int tmp = (X & Y) | (~X & Z);
            return tmp;
        }
    
        private static int P0(int X) {
            int y = rotateLeft(X, 9);
            y = bitCycleLeft(X, 9);
            int z = rotateLeft(X, 17);
            z = bitCycleLeft(X, 17);
            int t = X ^ y ^ z;
            return t;
        }
    
        private static int P1(int X) {
            int t = X ^ bitCycleLeft(X, 15) ^ bitCycleLeft(X, 23);
            return t;
        }
    
        /**
         * 对最后一个分组字节数据padding
         *
         * @param in
         * @param bLen 分组个数
         * @return
         */
        public static byte[] padding(byte[] in, int bLen) {
            int k = 448 - (8 * in.length + 1) % 512;
            if (k < 0) {
                k = 960 - (8 * in.length + 1) % 512;
            }
            k += 1;
            byte[] padd = new byte[k / 8];
            padd[0] = (byte) 0x80;
            long n = in.length * 8 + bLen * 512;
            byte[] out = new byte[in.length + k / 8 + 64 / 8];
            int pos = 0;
            System.arraycopy(in, 0, out, 0, in.length);
            pos += in.length;
            System.arraycopy(padd, 0, out, pos, padd.length);
            pos += padd.length;
            byte[] tmp = back(SecurityUtils.longToBytes(n));
            System.arraycopy(tmp, 0, out, pos, tmp.length);
            return out;
        }
    
        /**
         * 字节数组逆序
         *
         * @param in
         * @return
         */
        private static byte[] back(byte[] in) {
            byte[] out = new byte[in.length];
            for (int i = 0; i < out.length; i++) {
                out[i] = in[out.length - i - 1];
            }
    
            return out;
        }
    
        public static int rotateLeft(int x, int n) {
            return (x << n) | (x >> (32 - n));
        }
    
        private static int bitCycleLeft(int n, int bitLen) {
            bitLen %= 32;
            byte[] tmp = bigEndianIntToByte(n);
            int byteLen = bitLen / 8;
            int len = bitLen % 8;
            if (byteLen > 0) {
                tmp = byteCycleLeft(tmp, byteLen);
            }
    
            if (len > 0) {
                tmp = bitSmall8CycleLeft(tmp, len);
            }
    
            return bigEndianByteToInt(tmp);
        }
    
        private static byte[] bitSmall8CycleLeft(byte[] in, int len) {
            byte[] tmp = new byte[in.length];
            int t1, t2, t3;
            for (int i = 0; i < tmp.length; i++) {
                t1 = (byte) ((in[i] & 0x000000ff) << len);
                t2 = (byte) ((in[(i + 1) % tmp.length] & 0x000000ff) >> (8 - len));
                t3 = (byte) (t1 | t2);
                tmp[i] = (byte) t3;
            }
    
            return tmp;
        }
    
        private static byte[] byteCycleLeft(byte[] in, int byteLen) {
            byte[] tmp = new byte[in.length];
            System.arraycopy(in, byteLen, tmp, 0, in.length - byteLen);
            System.arraycopy(in, 0, tmp, in.length - byteLen, byteLen);
            return tmp;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272

    3、Sm3Digest

    /**
     * @author SongWei
     * @version 1.0.0
     * @ClassName spring-cloud.com.boot.cloud.sm.Sm3Digest.java
     * @Description TODO
     * @createTime 2018年10月28日 14:29:00
     */
    public class Sm3Digest {
        /**
         * SM3值的长度
         */
        private static final int BYTE_LENGTH = 32;
    
        /**
         * SM3分组长度
         */
        private static final int BLOCK_LENGTH = 64;
    
        /**
         * 缓冲区长度
         */
        private static final int BUFFER_LENGTH = BLOCK_LENGTH * 1;
    
        /**
         * 缓冲区
         */
        private byte[] xBuf = new byte[BUFFER_LENGTH];
    
        /**
         * 缓冲区偏移量
         */
        private int xBufOff;
    
        /**
         * 初始向量
         */
        private byte[] V = Sm3Tool.iv.clone();
    
        private int cntBlock = 0;
    
        public Sm3Digest() {
        }
    
        public Sm3Digest(Sm3Digest t) {
            System.arraycopy(t.xBuf, 0, this.xBuf, 0, t.xBuf.length);
            this.xBufOff = t.xBufOff;
            System.arraycopy(t.V, 0, this.V, 0, t.V.length);
        }
    
        /**
         * SM3结果输出
         *
         * @param out    保存SM3结构的缓冲区
         * @param outOff 缓冲区偏移量
         * @return
         */
        public int doFinal(byte[] out, int outOff) {
            byte[] tmp = doFinal();
            System.arraycopy(tmp, 0, out, 0, tmp.length);
            return BYTE_LENGTH;
        }
    
        public void reset() {
            xBufOff = 0;
            cntBlock = 0;
            V = Sm3Tool.iv.clone();
        }
    
        /**
         * 明文输入
         *
         * @param in    明文输入缓冲区
         * @param inOff 缓冲区偏移量
         * @param len   明文长度
         */
        public void update(byte[] in, int inOff, int len) {
            int partLen = BUFFER_LENGTH - xBufOff;
            int inputLen = len;
            int dPos = inOff;
            if (partLen < inputLen) {
                System.arraycopy(in, dPos, xBuf, xBufOff, partLen);
                inputLen -= partLen;
                dPos += partLen;
                doUpdate();
                while (inputLen > BUFFER_LENGTH) {
                    System.arraycopy(in, dPos, xBuf, 0, BUFFER_LENGTH);
                    inputLen -= BUFFER_LENGTH;
                    dPos += BUFFER_LENGTH;
                    doUpdate();
                }
            }
    
            System.arraycopy(in, dPos, xBuf, xBufOff, inputLen);
            xBufOff += inputLen;
        }
    
        private void doUpdate() {
            byte[] B = new byte[BLOCK_LENGTH];
            for (int i = 0; i < BUFFER_LENGTH; i += BLOCK_LENGTH) {
                System.arraycopy(xBuf, i, B, 0, B.length);
                doHash(B);
            }
            xBufOff = 0;
        }
    
        private void doHash(byte[] B) {
            byte[] tmp = Sm3Tool.CF(V, B);
            System.arraycopy(tmp, 0, V, 0, V.length);
            cntBlock++;
        }
    
        private byte[] doFinal() {
            byte[] B = new byte[BLOCK_LENGTH];
            byte[] buffer = new byte[xBufOff];
            System.arraycopy(xBuf, 0, buffer, 0, buffer.length);
            byte[] tmp = Sm3Tool.padding(buffer, cntBlock);
            for (int i = 0; i < tmp.length; i += BLOCK_LENGTH) {
                System.arraycopy(tmp, i, B, 0, B.length);
                doHash(B);
            }
            return V;
        }
    
        public void update(byte in) {
            byte[] buffer = new byte[]{in};
            update(buffer, 0, 1);
        }
    
        public int getDigestSize() {
            return BYTE_LENGTH;
        }
    
        public static void main(String[] args) {
            byte[] md = new byte[32];
            byte[] msg1 = "ererfeiisgod".getBytes();
            Sm3Digest sm3 = new Sm3Digest();
            sm3.update(msg1, 0, msg1.length);
            sm3.doFinal(md, 0);
            String s = new String(Hex.encode(md));
            System.out.println(s.toUpperCase());
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143

    总结

  • 相关阅读:
    LeetCode 周赛上分之旅 #48 一道简单的树上动态规划问题
    1个G的视频能压缩到几M?视频过大这样压缩
    2.10 - 存储管理 2.11 - 页式存储 2.12 - 段式存储 2.13 - 段页式存储
    【日常训练】207. 课程表
    Ubuntu上安装和配置MySQL
    从零开发短视频电商 数据库大表分页问题
    2022年中纪实 -- 一个普通人的经历
    php中识别url被篡改并阻止访问的实现方式是什么
    800G时代来临,千兆光模块万兆光模块还有用吗?
    使用共享内存进行通信的代码和运行情况分析,共享内存的特点(拷贝次数,访问控制),加入命名管道进行通信的代码和运行情况分析
  • 原文地址:https://blog.csdn.net/qq_20197983/article/details/126036489