Commit 8e6d8012 authored by AfirSraftGarrier's avatar AfirSraftGarrier

格式并加些打印

parent dc3dc14b
......@@ -3,9 +3,13 @@ package iot.sixiang.license.controller;
import com.alibaba.fastjson2.JSON;
import iot.sixiang.license.object.data.AuthData;
import iot.sixiang.license.util.HmacUtil;
import iot.sixiang.license.util.sm4.SM4Utils;
import iot.sixiang.license.util.sm4.SM4;
import iot.sixiang.license.util.sm4.SM4Context;
import org.junit.jupiter.api.Test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
/**
* Created by M=54G
* Date 11/11/22 9:54 AM
......@@ -19,7 +23,6 @@ public class DeviceControllerTest {
String sn = "ERE54S619LNYMPKVN9";
String appKey = "110801";
//showSendData(appId, sn, appKey);
goDo("hellogo");
}
private void showSendData(String appId, String sn, String appKey) {
......@@ -55,22 +58,37 @@ public class DeviceControllerTest {
System.out.println(bytes2Hex(allBytes));
}
private void goDo(String string) {
SM4Utils sm4 = new SM4Utils();
String secret = "9ec55e355f1e829e";
// 设置 密钥 16长度的字符
sm4.setSecretKey(secret);
// 设置 向量 16长度的字符
// sm4.setIv("ee6855f0ea29e9e2");
// 设置待加密的文本
String plainText = string;
// 声明密钥和向量是否是32长度的十六进制的字符串,如果true则需要设置密钥向量都是十六进制的32长度字符串。Util.byteToHex("b7b3gSMFWd9a67i1".getBytes())
// sm4.setHexString(false);
// 进行加密
String encryptString = sm4.encryptData_ECB(plainText);
System.out.println("原文:" + string);
System.out.println("密文: " + encryptString);
public byte[] encryptData_ECB(String string, byte[] keyBytes) {
try {
SM4Context ctx = new SM4Context();
ctx.mode = SM4.SM4_ENCRYPT;
SM4 sm4 = new SM4();
sm4.sm4_setkey_enc(ctx, keyBytes);
byte[] encrypted = sm4_crypt_ecb(sm4, ctx, string.getBytes("UTF-8"));
return encrypted;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public byte[] sm4_crypt_ecb(SM4 sm4, SM4Context ctx, byte[] input) throws Exception {
int length = input.length;
ByteArrayInputStream bins = new ByteArrayInputStream(input);
ByteArrayOutputStream bous = new ByteArrayOutputStream();
for (; length > 0; length -= 16) {
byte[] in = new byte[16];
byte[] out = new byte[16];
bins.read(in);
sm4.sm4_one_round(ctx.sk, in, out);
bous.write(out);
}
byte[] output = bous.toByteArray();
bins.close();
bous.close();
return output;
}
@Test
......
......@@ -143,7 +143,7 @@ public class SM4 {
}
}
private void sm4_one_round(long[] sk, byte[] input, byte[] output) {
public void sm4_one_round(long[] sk, byte[] input, byte[] output) {
int i = 0;
long[] ulbuf = new long[36];
ulbuf[0] = GET_ULONG_BE(input, 0);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment