Skip to content

Commit 9e30b53

Browse files
committed
byte hex convert
1 parent b134141 commit 9e30b53

File tree

3 files changed

+138
-12
lines changed

3 files changed

+138
-12
lines changed
Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,55 @@
11
package com.github.kuangcp.util;
22

3+
import java.math.BigInteger;
4+
import java.util.stream.Collectors;
5+
import java.util.stream.IntStream;
6+
37
/**
48
* @author kuangcp on 3/14/19-2:26 PM
59
*/
610
public class ShowBinary {
711

8-
public static String toBinary(Byte value) {
9-
return Integer.toBinaryString(value);
10-
}
12+
public static String toBinary(Byte value) {
13+
return Integer.toBinaryString(value);
14+
}
15+
16+
public static String toBinary(Integer value) {
17+
return Integer.toBinaryString(value);
18+
}
19+
20+
public static String toBinary(Double value) {
21+
return Long.toBinaryString(Double.doubleToRawLongBits(value));
22+
}
23+
24+
public static String toBinary(Float value) {
25+
return Float.toHexString(value);
26+
}
1127

12-
public static String toBinary(Integer value) {
13-
return Integer.toBinaryString(value);
14-
}
28+
/**
29+
* https://stackoverflow.com/questions/9655181/java-convert-a-byte-array-to-a-hex-string
30+
* 风险:当转整数后前缀出现0,会丢失字节
31+
*/
32+
public static String byteToHex2(byte[] value) {
33+
return new BigInteger(1, value).toString(16);
34+
}
1535

16-
public static String toBinary(Double value) {
17-
return Long.toBinaryString(Double.doubleToRawLongBits(value));
18-
}
36+
public static String byteToHex(byte[] value) {
37+
return IntStream.range(0, value.length)
38+
.mapToObj(i -> String.format("%02X", value[i]))
39+
.collect(Collectors.joining());
40+
}
1941

20-
public static String toBinary(Float value) {
21-
return Float.toHexString(value);
22-
}
42+
public static byte[] hexToByte(String hex) {
43+
byte[] ans = new byte[hex.length() / 2];
44+
for (int i = 0; i < ans.length; i++) {
45+
int index = i * 2;
46+
int val = Integer.parseInt(hex.substring(index, index + 2), 16);
47+
ans[i] = (byte) val;
48+
}
49+
// for (byte b : ans) {
50+
// System.out.print(b + " ");
51+
// }
52+
// System.out.println(hex.length() + " " + ans.length);
53+
return ans;
54+
}
2355
}

class/src/main/java/security/aes/AESUtil.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package security.aes;
22

3+
import com.github.kuangcp.util.ShowBinary;
4+
35
import javax.crypto.*;
46
import javax.crypto.spec.IvParameterSpec;
57
import javax.crypto.spec.PBEKeySpec;
@@ -20,6 +22,43 @@
2022

2123
public class AESUtil {
2224

25+
26+
public static byte[] encrypt(String hexKey, byte[] data) throws Exception {
27+
return encrypt(ShowBinary.hexToByte(hexKey), data);
28+
}
29+
30+
public static byte[] decrypt(String hexKey, byte[] data) throws Exception {
31+
return decrypt(ShowBinary.hexToByte(hexKey), data);
32+
}
33+
34+
/**
35+
* 加密
36+
*
37+
* @param key 密钥
38+
* @param data 加密数据
39+
* @return 密文
40+
*/
41+
public static byte[] encrypt(byte[] key, byte[] data) throws Exception {
42+
SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
43+
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
44+
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
45+
return cipher.doFinal(data);
46+
}
47+
48+
/**
49+
* 解密
50+
*
51+
* @param key 密钥
52+
* @param data 密文
53+
* @return 解密后的数据
54+
*/
55+
public static byte[] decrypt(byte[] key, byte[] data) throws Exception {
56+
SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
57+
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
58+
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
59+
return cipher.doFinal(data);
60+
}
61+
2362
public static String encrypt(String algorithm, String input, SecretKey key, IvParameterSpec iv)
2463
throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException,
2564
InvalidKeyException, BadPaddingException, IllegalBlockSizeException {

class/src/test/java/security/aes/AESUtilTest.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package security.aes;
22

33

4+
import com.github.kuangcp.util.ShowBinary;
45
import lombok.extern.slf4j.Slf4j;
56
import org.junit.Assert;
67
import org.junit.Test;
78

9+
import javax.crypto.KeyGenerator;
810
import javax.crypto.SealedObject;
911
import javax.crypto.SecretKey;
1012
import javax.crypto.spec.IvParameterSpec;
@@ -14,13 +16,66 @@
1416
import java.nio.file.Path;
1517
import java.nio.file.Paths;
1618
import java.security.SecureRandom;
19+
import java.util.Base64;
1720

1821
import static org.hamcrest.MatcherAssert.assertThat;
1922

2023
@Slf4j
2124
public class AESUtilTest {
2225

2326

27+
@Test
28+
public void testFlow() throws Exception {
29+
// 生成随机密钥
30+
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
31+
// 128 192 256
32+
keyGenerator.init(128, new SecureRandom());
33+
SecretKey secretKey = keyGenerator.generateKey();
34+
35+
byte[] key = secretKey.getEncoded();
36+
System.out.println("AES 密钥:" + Base64.getEncoder().encodeToString(key));
37+
System.out.println("AES 密钥:" + ShowBinary.byteToHex(key));
38+
39+
String content = "KeyGenerator keyGenerator = KeyGenerator.getInstance(\"AES\");";
40+
System.out.println("原文:" + content);
41+
42+
byte[] ret = AESUtil.encrypt(key, content.getBytes());
43+
System.out.println("密文:" + Base64.getEncoder().encodeToString(ret));
44+
System.out.println("密文:" + ShowBinary.byteToHex(ret));
45+
46+
byte[] raw = AESUtil.decrypt(key, ret);
47+
System.out.println("原文:" + new String(raw));
48+
Assert.assertEquals(content, new String(raw));
49+
}
50+
51+
@Test
52+
public void testHexKeyFlow() throws Exception {
53+
// 生成随机密钥
54+
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
55+
// 128 192 256
56+
keyGenerator.init(192, new SecureRandom());
57+
SecretKey secretKey = keyGenerator.generateKey();
58+
byte[] key = secretKey.getEncoded();
59+
60+
// System.out.println("Origin: " + key.length);
61+
String hexKey = ShowBinary.byteToHex(key);
62+
63+
System.out.println("AES 密钥:" + hexKey);
64+
65+
String content = "KeyGenerator keyGenerator = KeyGenerator.getInstance(\"AES\");";
66+
System.out.println("原文:" + content);
67+
68+
System.out.println("------");
69+
byte[] ret = AESUtil.encrypt(hexKey, content.getBytes());
70+
// System.out.println("密文:" + Base64.getEncoder().encodeToString(ret));
71+
System.out.println("密文:" + ShowBinary.byteToHex(ret));
72+
73+
byte[] raw = AESUtil.decrypt(hexKey, ret);
74+
System.out.println("原文:" + new String(raw));
75+
Assert.assertEquals(content, new String(raw));
76+
}
77+
78+
2479
@Test
2580
public void testStringEncrypt() throws Exception {
2681
// given

0 commit comments

Comments
 (0)