前言

你是否存在这样的苦恼,数据需要安全存储,但是每个系统大家自己写,很浪费时间。

encryption-local 一个离线版本的金融敏感信息加解密工具,用于数据库敏感信息存储。

本文介绍一下用户姓名的加解密+掩码源码解析。

快速开始

maven 引入

  [xml]
1
2
3
4
5
<dependency> <groupId>com.github.houbb</groupId> <artifactId>encryption-local-core</artifactId> <version>1.2.0</version> </dependency>

例子

  [java]
1
2
3
4
5
6
7
8
9
10
11
12
13
final String address = "太平洋比基尼海滩比奇堡镇贝壳街124号的波萝屋"; final String salt = "99886622"; CommonEncryptResponse response = EncryptionLocalUtil.addressEncrypt(address, salt); String cipher = response.getCipher(); String mask = response.getMask(); String hash = response.getHash(); Assert.assertEqual("D8D9E99FB8286107C2F75325C0B9046CF335EE4AC4FCD3F27E0D6BFD8B3DBF39440A3D69422A3AF933576798CF3860F330F288E196CEACB22CCCDA0623B94355",cipher); Assert.assertEquals("太平洋比基尼*************的波萝屋", mask); Assert.assertEquals("31912515337902B8A3CC1CBDB5772358", hash); // 解密 String plain = EncryptionLocalUtil.addressDecrypt(cipher, salt); Assert.assertEquals(address, plain);

源码部分

这里主要重点分析一下掩码的源码部分,我们可以直接在 encryption-local 查看源码。

  [java]
1
2
3
4
5
6
7
8
public class EncryptMaskAddress extends AbstractEncryptMask { @Override protected String doMask(EncryptMaskContext context) { return InnerMaskUtil.address(context.getPlainText()); } }

地址掩码的我们要结合自己实际的业务来脱敏,这里是前6后四的方式。

  [java]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/** * 地址信息脱敏 * 保留前6后4 * @param address 地址 * @return 脱敏结果 */ public static String address(final String address) { if(StringUtil.isEmpty(address)) { return address; } final int prefixLength = 6; int times = address.length() - 10; final String middle = StringUtil.repeat("*", times); return StringUtil.buildString(address, middle, prefixLength); }

当然,我们完全可以根据这个进行自己的实现拓展。

拓展阅读

项目推荐

下面是一些日志、加解密、脱敏安全相关的库推荐:

项目 介绍
sensitive-word 高性能敏感词核心库
sensitive-word-admin 敏感词控台,前后端分离
sensitive 高性能日志脱敏组件
auto-log 统一日志切面组件,支持全链路traceId
encryption-local 离线加密机组件
encryption 加密机标准API+本地客户端
encryption-server 加密机服务

拓展阅读

【老马】离线版金融敏感信息加解密组件开源项目encryption-local

【藏经阁】加密机服务完整解决方案,包含客户端+服务端

小结

离线版本的加解密好处是非常的方便,如果需要的话建议直接使用,可以节约很多时间。

希望这个项目可以帮助到你。喜欢的话,可以给原作者一个 star 鼓励一下!

我是老马,期待与你的下次重逢~

参考资料