Skip to content
项目
群组
代码片段
帮助
正在加载...
登录
切换导航
E
estateplat-encrypt
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
狄熙卜
estateplat-encrypt
Commits
f5c2ec91
提交
f5c2ec91
authored
3月 20, 2024
作者:
liyinqiao
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
增加AES加解密类型
上级
3d1f8258
隐藏空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
299 行增加
和
6 行删除
+299
-6
pom.xml
pom.xml
+0
-5
AesCryptExecutor.java
...n/gtmap/bdcdj/core/encrypt/executor/AesCryptExecutor.java
+59
-0
CryptExecutorFactory.java
...map/bdcdj/core/encrypt/executor/CryptExecutorFactory.java
+1
-1
AESutil.java
src/main/java/cn/gtmap/bdcdj/core/encrypt/utils/AESutil.java
+170
-0
Constants.java
...ain/java/cn/gtmap/bdcdj/core/encrypt/utils/Constants.java
+69
-0
没有找到文件。
pom.xml
浏览文件 @
f5c2ec91
...
@@ -31,11 +31,6 @@
...
@@ -31,11 +31,6 @@
<artifactId>
estateplat-common
</artifactId>
<artifactId>
estateplat-common
</artifactId>
<version>
1.2.4-SNAPSHOT
</version>
<version>
1.2.4-SNAPSHOT
</version>
</dependency>
</dependency>
<dependency>
<groupId>
org.springframework.security
</groupId>
<artifactId>
spring-security-cas-client
</artifactId>
<version>
2.0.7.RELEASE
</version>
</dependency>
<dependency>
<dependency>
<groupId>
javax.servlet
</groupId>
<groupId>
javax.servlet
</groupId>
...
...
src/main/java/cn/gtmap/bdcdj/core/encrypt/executor/AesCryptExecutor.java
0 → 100644
浏览文件 @
f5c2ec91
package
cn
.
gtmap
.
bdcdj
.
core
.
encrypt
.
executor
;
import
cn.gtmap.bdcdj.core.encrypt.CryptProperties
;
import
cn.gtmap.bdcdj.core.encrypt.enums.SaltsEnum
;
import
cn.gtmap.bdcdj.core.encrypt.utils.AESutil
;
import
cn.gtmap.bdcdj.core.encrypt.utils.CryptUtil
;
import
org.apache.commons.lang3.StringUtils
;
/**
* AES CBC 加密解密
*
*/
public
class
AesCryptExecutor
implements
CryptExecutor
{
@Override
public
String
encrypt
(
CryptProperties
cryptProperties
,
String
value
)
{
if
(
StringUtils
.
isBlank
(
value
))
{
return
value
;
}
if
(
StringUtils
.
startsWithIgnoreCase
(
value
,
SaltsEnum
.
SALTS
.
getSalt
()))
{
return
value
;
}
String
separatorChars
=
CryptUtil
.
getSeparatorChars
(
value
);
if
(
StringUtils
.
isNotEmpty
(
separatorChars
))
{
String
[]
paramArray
=
StringUtils
.
split
(
value
,
separatorChars
);
StringBuilder
encryptParam
=
new
StringBuilder
();
for
(
String
temp
:
paramArray
)
{
encryptParam
.
append
(
CryptUtil
.
matchHex
(
temp
)
?
temp
:
AESutil
.
encodeToHexString
(
temp
)).
append
(
separatorChars
);
}
return
StringUtils
.
substring
(
encryptParam
.
toString
(),
0
,
encryptParam
.
length
()
-
1
);
}
return
AESutil
.
encodeToHexString
(
value
);
}
@Override
public
String
decrypt
(
CryptProperties
cryptProperties
,
String
param
)
{
if
(
StringUtils
.
isBlank
(
param
))
{
return
param
;
}
try
{
String
separatorChars
=
CryptUtil
.
getSeparatorChars
(
param
);
if
(
StringUtils
.
isNotEmpty
(
separatorChars
))
{
String
[]
paramArray
=
StringUtils
.
split
(
param
,
separatorChars
);
StringBuilder
decryptParam
=
new
StringBuilder
();
for
(
String
temp
:
paramArray
)
{
decryptParam
.
append
(
CryptUtil
.
matchHex
(
temp
)
?
AESutil
.
decodeFromHexString
(
temp
)
:
temp
).
append
(
separatorChars
);
}
return
StringUtils
.
substring
(
decryptParam
.
toString
(),
0
,
decryptParam
.
length
()
-
1
);
}
if
(
CryptUtil
.
matchHex
(
param
))
{
return
AESutil
.
decodeFromHexString
(
param
);
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
param
;
}
}
src/main/java/cn/gtmap/bdcdj/core/encrypt/executor/CryptExecutorFactory.java
浏览文件 @
f5c2ec91
...
@@ -17,7 +17,7 @@ public class CryptExecutorFactory {
...
@@ -17,7 +17,7 @@ public class CryptExecutorFactory {
}
}
private
static
void
registerCryptExcutor
()
{
private
static
void
registerCryptExcutor
()
{
//
cryptExecutorMap.put(CryptType.AES_CBC, new AesCryptExecutor());
cryptExecutorMap
.
put
(
CryptType
.
AES_CBC
,
new
AesCryptExecutor
());
// cryptExecutorMap.put(CryptType.GM_SM2, new GmSm2CryptExecutor());
// cryptExecutorMap.put(CryptType.GM_SM2, new GmSm2CryptExecutor());
cryptExecutorMap
.
put
(
CryptType
.
GM_SM4
,
new
GmSm4CryptExecutor
());
cryptExecutorMap
.
put
(
CryptType
.
GM_SM4
,
new
GmSm4CryptExecutor
());
}
}
...
...
src/main/java/cn/gtmap/bdcdj/core/encrypt/utils/AESutil.java
0 → 100644
浏览文件 @
f5c2ec91
package
cn
.
gtmap
.
bdcdj
.
core
.
encrypt
.
utils
;
import
cn.gtmap.bdcdj.core.encrypt.enums.SaltsEnum
;
import
org.apache.commons.codec.DecoderException
;
import
org.apache.commons.codec.binary.Hex
;
import
org.apache.commons.lang3.StringUtils
;
import
javax.crypto.Cipher
;
import
javax.crypto.KeyGenerator
;
import
javax.crypto.SecretKey
;
import
javax.crypto.spec.IvParameterSpec
;
import
javax.crypto.spec.SecretKeySpec
;
import
java.security.Key
;
import
java.security.NoSuchAlgorithmException
;
import
java.security.spec.AlgorithmParameterSpec
;
/**
* @author <a href="mailto:wenyuanwu@gtmap.cn">wenyuanwu</a>
* @version 1.0 2020-4-16
* @description aes 加密解密工具类
*/
public
class
AESutil
{
private
static
final
String
CIPHER_ALGORITHM_CBC
=
"AES/CBC/PKCS5Padding"
;
private
static
final
String
KEY_ALGORITHM
=
"AES"
;
private
static
final
String
base64Key
=
"1B0EEF110D358E31409FA45A38A1E20D"
;
private
static
byte
[]
iv
=
{
0x01
,
0x23
,
0x45
,
0x67
,
0x89
-
0xFF
,
0xAB
-
0xFF
,
0xCD
-
0xFF
,
0xEF
-
0xFF
,
0x01
,
0x23
,
0x45
,
0x67
,
0x89
-
0xFF
,
0xAB
-
0xFF
,
0xCD
-
0xFF
,
0xEF
-
0xFF
};
/**
* AES 密钥生成方法
*
* @author <a href="mailto:wenyuanwu@gtmap.cn">wenyuanwu</a>
*/
public
static
String
initKey
()
{
KeyGenerator
kg
=
null
;
try
{
kg
=
KeyGenerator
.
getInstance
(
KEY_ALGORITHM
);
}
catch
(
NoSuchAlgorithmException
e
)
{
e
.
printStackTrace
();
}
kg
.
init
(
128
);
SecretKey
secretKey
=
kg
.
generateKey
();
return
parseByte2HexStr
(
secretKey
.
getEncoded
());
}
/**
* AES 加密方法
*
* @param data 待加密字符串
* @return 加密后的字节数组
* @author <a href="mailto:wenyuanwu@gtmap.cn">wenyuanwu</a>
*/
public
static
byte
[]
encrypt
(
String
data
)
{
Cipher
cipher
=
null
;
try
{
cipher
=
Cipher
.
getInstance
(
CIPHER_ALGORITHM_CBC
);
byte
[]
key
=
parseHexStr2Byte
(
base64Key
);
Key
k
=
new
SecretKeySpec
(
key
,
KEY_ALGORITHM
);
AlgorithmParameterSpec
paramSpec
=
new
IvParameterSpec
(
iv
);
cipher
.
init
(
Cipher
.
ENCRYPT_MODE
,
k
,
paramSpec
);
return
cipher
.
doFinal
(
data
.
getBytes
());
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
null
;
}
/**
* AES 解密方法
*
* @param data 待解密字节数组
* @return 解密后的字节数组
* @author <a href="mailto:wenyuanwu@gtmap.cn">wenyuanwu</a>
*/
public
static
byte
[]
decrypt
(
byte
[]
data
)
{
try
{
Cipher
cipher
=
Cipher
.
getInstance
(
CIPHER_ALGORITHM_CBC
);
byte
[]
key
=
parseHexStr2Byte
(
base64Key
);
Key
k
=
new
SecretKeySpec
(
key
,
KEY_ALGORITHM
);
AlgorithmParameterSpec
paramSpec
=
new
IvParameterSpec
(
iv
);
cipher
.
init
(
Cipher
.
DECRYPT_MODE
,
k
,
paramSpec
);
return
cipher
.
doFinal
(
data
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
null
;
}
/**
* AES 加密方法 加密成16进制字符串
* 为增加破解难度 做加盐处理
*
* @param data 待加密字符串
* @return 加密字符串16进制字符串形式输出
* @author <a href="mailto:wenyuanwu@gtmap.cn">wenyuanwu</a>
* @since :14:06 2020-5-18
*/
public
static
String
encodeToHexString
(
String
data
)
{
return
SaltsEnum
.
SALTS
.
getSalt
()
+
Hex
.
encodeHexString
(
encrypt
(
data
));
}
/**
* AES 解密方法 将加密后的16进制字符串 解密成明文
*
* @param data 待解密的加密字符串
* @return 解密后的明文字符串
* @author <a href="mailto:wenyuanwu@gtmap.cn">wenyuanwu</a>
* @since :14:09 2020-5-18
*/
public
static
String
decodeFromHexString
(
String
data
)
throws
DecoderException
{
if
(
StringUtils
.
startsWithIgnoreCase
(
data
,
SaltsEnum
.
SALTS
.
getSalt
()))
{
if
(
StringUtils
.
endsWith
(
data
,
Constants
.
DENG
))
{
//存在一种情况,在sql语句对证件号加 “等”字 截取去除前面拼接的 与后边的等字
data
=
StringUtils
.
removeEnd
(
StringUtils
.
removeStartIgnoreCase
(
data
,
SaltsEnum
.
SALTS
.
getSalt
()),
Constants
.
DENG
);
data
=
new
String
(
decrypt
(
Hex
.
decodeHex
(
data
.
toCharArray
())));
return
data
+
Constants
.
DENG
;
}
else
{
return
new
String
(
decrypt
(
Hex
.
decodeHex
((
StringUtils
.
removeStartIgnoreCase
(
data
,
SaltsEnum
.
SALTS
.
getSalt
())).
toCharArray
())));
}
}
return
new
String
(
decrypt
(
Hex
.
decodeHex
(
data
.
toCharArray
())));
}
/**
* 将二进制转换成16进制
*
* @param buf
* @return
*/
public
static
String
parseByte2HexStr
(
byte
buf
[])
{
StringBuffer
sb
=
new
StringBuffer
();
for
(
int
i
=
0
;
i
<
buf
.
length
;
i
++)
{
String
hex
=
Integer
.
toHexString
(
buf
[
i
]
&
0xFF
);
if
(
hex
.
length
()
==
1
)
{
hex
=
'0'
+
hex
;
}
sb
.
append
(
hex
.
toUpperCase
());
}
return
sb
.
toString
();
}
/**
* 将16进制转换为二进制
*
* @param hexStr
* @return
*/
public
static
byte
[]
parseHexStr2Byte
(
String
hexStr
)
{
if
(
hexStr
.
length
()
<
1
)
{
return
null
;
}
byte
[]
result
=
new
byte
[
hexStr
.
length
()
/
2
];
for
(
int
i
=
0
;
i
<
hexStr
.
length
()
/
2
;
i
++)
{
int
high
=
Integer
.
parseInt
(
hexStr
.
substring
(
i
*
2
,
i
*
2
+
1
),
16
);
int
low
=
Integer
.
parseInt
(
hexStr
.
substring
(
i
*
2
+
1
,
i
*
2
+
2
),
16
);
result
[
i
]
=
(
byte
)
(
high
*
16
+
low
);
}
return
result
;
}
}
src/main/java/cn/gtmap/bdcdj/core/encrypt/utils/Constants.java
0 → 100644
浏览文件 @
f5c2ec91
package
cn
.
gtmap
.
bdcdj
.
core
.
encrypt
.
utils
;
/**
* 静态常量类
*
* @author <a href="mailto:wenyuanwu@gtmap.cn">wenyuanwu</a>
* @version 1.0 2020-5-18
*/
public
class
Constants
{
private
Constants
()
{
}
/**
* systemVersion 配置的默认标准版本
*/
public
static
final
String
SYSTEM_VERSION_STANDARD
=
"standard"
;
/**
* 加密的加盐处理
*/
public
static
final
String
SALTS
=
"apmtg04b"
;
/**
* GM Sm2 salts
*/
public
static
final
String
GM_SM2_SALTS
=
"b2a1fdd"
;
/**
* gm sm4 salts
*/
public
static
final
String
GM_SM4_SALTS
=
"4mspamtg"
;
/**
* 等
*/
public
static
final
String
DENG
=
"等"
;
/**
* 动态参数 加密参数 识别标识
*/
public
static
final
String
DYNAMIC_ENCRYPTKEYS
=
"encryptKeys"
;
/**
* 动态参数 解密参数 识别标识
*/
public
static
final
String
DYNAMIC_DECRYPTKEYS
=
"decryptKeys"
;
/**
* 国密SM2 私钥
*/
public
static
final
String
GM2PSK
=
"AKPflclLAvlxbX8yb8okfknP6FKF4Cq2nCYXRodZF7ZV"
;
/**
* 国密SM2 公钥
*/
public
static
final
String
GM2PUK
=
"Ak3tRLNHcQCg4a1NTXldbx1c9RVE5SIUW17ETKgFq3Wa"
;
/**
* 国密 Sm4 秘钥
*/
public
static
final
String
GM_SM4_ECK
=
"645B0A010D939BD614A620BCAF645470"
;
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论