引入依赖
>
>cn.hutool >
>hutool-crypto >
>5.8.21 >
>
>
>org.bouncycastle >
>bcprov-jdk15on >
>1.70 >
>
map加密解密 list加密解密 Set
public static void main(String[] args) {
/* Map map = new HashMap<>();
map.put("key","qwrw");
String o = JSON.toJSONString(map);
SM4 sm4 = SmUtil.sm4("0123456789ABCDEF".getBytes());
String str = sm4.encryptHex(o);
System.out.println("加密后:"+str);
String s = sm4.decryptStr("解密后:"+str);
System.out.println(s);*/
// 创建一个包含 Map 对象的 Set 集合
/* Set
/* // 创建一个示例的 List 集合
List list = new ArrayList<>();
User user = new User();
user.setName("请教的");
user.setAge("123");
User user1 = new User();
user1.setName("第二个");
user1.setAge("28");
list.add(user);
list.add(user1);
// 将 List 转换为字节数组
byte[] data;
try (
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(bos)
) {
out.writeObject(list);
data = bos.toByteArray();
} catch (IOException e) {
e.printStackTrace();
return;
}
// 使用 SM4 算法对字节数组进行加密
SM4 sm43 = SmUtil.sm4("0123456789ABCDEF".getBytes());
String encrypted = sm43.encryptHex(data);
System.out.println("Encrypted text: " + encrypted);
// 使用 SM4 算法对密文进行解密
byte[] decryptedData;
try {
SM4 sm44 = SmUtil.sm4("0123456789ABCDEF".getBytes());
decryptedData = sm44.decrypt(HexUtil.decodeHex(encrypted));
} catch (Exception e) {
e.printStackTrace();
return;
}
// 将解密后的字节数组转换为原始的 List 集合
List decryptedList;
try (
ByteArrayInputStream bis = new ByteArrayInputStream(decryptedData);
ObjectInput in = new ObjectInputStream(bis)
) {
decryptedList = (List) in.readObject();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
return;
}
// 输出解密后的集合内容
for (User users : decryptedList) {
System.out.println("Name: " + users.getName() + ", Age: " + users.getAge());
}*/
}