Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
open-source
/
glsx-smart-message-channel
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit fced48a0
authored
Apr 22, 2021
by
111-PC\111
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
[+]pulsar消费服务
1 parent
94625a6d
Show whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
513 additions
and
7 deletions
pom.xml
src/main/java/cn/com/glsx/consumer/TestController.java
src/main/java/cn/com/glsx/consumer/common/Contants.java
src/main/java/cn/com/glsx/consumer/common/EventType.java
src/main/java/cn/com/glsx/consumer/controller/NoticeController.java
src/main/java/cn/com/glsx/consumer/entity/AqaraEvent.java
src/main/java/cn/com/glsx/consumer/entity/AqaraEventDetail.java
src/main/java/cn/com/glsx/consumer/entity/SmartDevideStatus.java
src/main/java/cn/com/glsx/consumer/redis/RedisService.java
src/main/java/cn/com/glsx/consumer/service/DeviceEventService.java
src/main/java/cn/com/glsx/consumer/service/InfoService.java
src/main/java/cn/com/glsx/consumer/utils/AESUtil.java
pom.xml
View file @
fced48a
...
@@ -90,6 +90,11 @@
...
@@ -90,6 +90,11 @@
<artifactId>
mapper
</artifactId>
<artifactId>
mapper
</artifactId>
<version>
3.4.1
</version>
<version>
3.4.1
</version>
</dependency>
</dependency>
<dependency>
<groupId>
com.glsx
</groupId>
<artifactId>
rest-admin-redis
</artifactId>
<version>
1.0-SNAPSHOT
</version>
</dependency>
</dependencies>
</dependencies>
<executions>
<executions>
<execution>
<execution>
...
...
src/main/java/cn/com/glsx/consumer/TestController.java
View file @
fced48a
package
cn
.
com
.
glsx
.
consumer
;
package
cn
.
com
.
glsx
.
consumer
;
import
cn.com.glsx.consumer.service.InfoService
;
import
com.alibaba.nacos.common.utils.MD5Utils
;
import
com.alibaba.nacos.common.utils.MD5Utils
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.web.bind.annotation.RequestBody
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMethod
;
import
org.springframework.web.bind.annotation.ResponseBody
;
/**
/**
* @author zhouhaibao
* @author zhouhaibao
...
@@ -14,8 +19,13 @@ import org.springframework.web.bind.annotation.RequestMapping;
...
@@ -14,8 +19,13 @@ import org.springframework.web.bind.annotation.RequestMapping;
@Slf4j
@Slf4j
public
class
TestController
{
public
class
TestController
{
@Autowired
private
InfoService
infoService
;
public
static
void
main
(
String
[]
args
)
{
@RequestMapping
(
value
=
"/test"
,
method
=
{
RequestMethod
.
POST
})
System
.
out
.
println
(
MD5Utils
.
md5Hex
(
"13476670746"
,
"utf-8"
));
@ResponseBody
public
void
test
(
@RequestBody
String
json
)
{
infoService
.
processMessage
(
json
);
}
}
}
}
src/main/java/cn/com/glsx/consumer/common/Contants.java
View file @
fced48a
...
@@ -6,7 +6,14 @@ package cn.com.glsx.consumer.common;
...
@@ -6,7 +6,14 @@ package cn.com.glsx.consumer.common;
*/
*/
public
class
Contants
{
public
class
Contants
{
public
static
final
int
STATUS_APPEAR
=
4
;
public
static
final
int
STATUS_APPEAR
=
4
;
public
static
final
int
SCENE_CONDITION_ALL
=
2
;
public
static
final
int
SCENE_CONDITION_ALL
=
2
;
public
static
final
int
SCENE_CONDITION_ANY
=
1
;
public
static
final
int
SCENE_CONDITION_ANY
=
1
;
public
static
final
String
MSG_TYPE_RESOURCE
=
"resource"
;
public
static
final
String
MSG_TYPE_DEIVCE
=
"device"
;
public
static
final
String
GATEWAY_BIND
=
"gateway_bind"
;
public
static
final
String
SUBDEVICE_BIND
=
"subdevice_bind"
;
public
static
final
String
GATEWAY_UNBIND
=
"gateway_unbind"
;
public
static
final
String
SUBDEVICE_UNBIND
=
"subdevice_unbind"
;
public
static
final
String
SCENE_STATUS
=
"scene_status"
;
}
}
src/main/java/cn/com/glsx/consumer/common/EventType.java
0 → 100644
View file @
fced48a
package
cn
.
com
.
glsx
.
consumer
.
common
;
/**
* @author zhouhaibao
* @date 2021/4/19 15:15
*/
public
enum
EventType
{
gateway_bind
(
"gateway_bind"
),
subdevice_bind
(
"subdevice_bind"
),
gateway_unbind
(
"gateway_unbind"
),
subdevice_unbind
(
"subdevice_unbind"
);
EventType
(
String
type
)
{
}
}
src/main/java/cn/com/glsx/consumer/controller/NoticeController.java
0 → 100644
View file @
fced48a
package
cn
.
com
.
glsx
.
consumer
.
controller
;
import
cn.com.glsx.consumer.common.Contants
;
import
cn.com.glsx.consumer.entity.AqaraEvent
;
import
cn.com.glsx.consumer.service.DeviceEventService
;
import
cn.com.glsx.consumer.service.InfoService
;
import
com.alibaba.fastjson.JSONObject
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.web.bind.annotation.RequestBody
;
import
org.springframework.web.bind.annotation.RequestMapping
;
/**
* @author zhouhaibao
* @date 2021/4/18 10:39
*/
@Controller
@RequestMapping
(
"/notice"
)
@Slf4j
public
class
NoticeController
{
@Autowired
private
InfoService
infoService
;
@Autowired
private
DeviceEventService
deviceEventService
;
@RequestMapping
(
"/deviceinfo"
)
public
String
deviceStatusInfo
(
@RequestBody
String
requestBody
){
// 解密请求体信息
try
{
log
.
info
(
"消息内容{}"
,
requestBody
);
// 解密传过来的状态值
AqaraEvent
aqaraEvent
=
JSONObject
.
parseObject
(
requestBody
,
AqaraEvent
.
class
);
if
(
Contants
.
MSG_TYPE_RESOURCE
.
equals
(
aqaraEvent
.
getMsgType
())){
deviceEventService
.
processResMessage
(
aqaraEvent
);
}
else
if
(
Contants
.
MSG_TYPE_DEIVCE
.
equals
(
aqaraEvent
.
getMsgType
())){
deviceEventService
.
processDevMessage
(
aqaraEvent
);
}
return
"glsxtest"
;
}
catch
(
Exception
e
){
e
.
printStackTrace
();
return
null
;
}
}
}
src/main/java/cn/com/glsx/consumer/entity/AqaraEvent.java
0 → 100644
View file @
fced48a
package
cn
.
com
.
glsx
.
consumer
.
entity
;
import
lombok.Data
;
import
java.util.List
;
/**
* @author zhouhaibao
* @date 2021/4/18 14:37
*/
@Data
public
class
AqaraEvent
{
/**
* 消息类型
*/
private
String
msgType
;
private
List
<
AqaraEventDetail
>
data
;
}
src/main/java/cn/com/glsx/consumer/entity/AqaraEventDetail.java
0 → 100644
View file @
fced48a
package
cn
.
com
.
glsx
.
consumer
.
entity
;
import
lombok.Data
;
import
java.util.List
;
/**
* @author zhouhaibao
* @date 2021/4/19 15:27
*/
@Data
public
class
AqaraEventDetail
{
// 用户openId
private
String
openId
;
// 设备模型
private
String
model
;
// 设备名称
private
String
name
;
// 设备id
private
String
did
;
// 设备父id
private
String
parentId
;
// 属性名
private
String
attr
;
// 属性值
private
String
value
;
private
String
attach
;
// 事件类型
private
String
event
;
private
String
time
;
private
String
freq
;
private
String
key
;
private
String
ircode
;
}
src/main/java/cn/com/glsx/consumer/entity/SmartDevideStatus.java
View file @
fced48a
...
@@ -43,6 +43,10 @@ public class SmartDevideStatus implements Serializable {
...
@@ -43,6 +43,10 @@ public class SmartDevideStatus implements Serializable {
@Column
(
name
=
"product_key"
)
@Column
(
name
=
"product_key"
)
private
String
productKey
;
private
String
productKey
;
@Transient
private
Boolean
flag
=
true
;
private
static
final
long
serialVersionUID
=
1L
;
private
static
final
long
serialVersionUID
=
1L
;
/**
/**
...
@@ -168,4 +172,11 @@ public class SmartDevideStatus implements Serializable {
...
@@ -168,4 +172,11 @@ public class SmartDevideStatus implements Serializable {
this
.
productKey
=
productKey
==
null
?
null
:
productKey
.
trim
();
this
.
productKey
=
productKey
==
null
?
null
:
productKey
.
trim
();
}
}
public
Boolean
getFlag
()
{
return
flag
;
}
public
void
setFlag
(
Boolean
flag
)
{
this
.
flag
=
flag
;
}
}
}
\ No newline at end of file
\ No newline at end of file
src/main/java/cn/com/glsx/consumer/redis/RedisService.java
0 → 100644
View file @
fced48a
package
cn
.
com
.
glsx
.
consumer
.
redis
;
import
org.apache.commons.logging.Log
;
import
org.apache.commons.logging.LogFactory
;
import
org.springframework.data.redis.core.HashOperations
;
import
org.springframework.data.redis.core.ListOperations
;
import
org.springframework.data.redis.core.RedisTemplate
;
import
org.springframework.data.redis.core.SetOperations
;
import
org.springframework.data.redis.core.ValueOperations
;
import
org.springframework.stereotype.Component
;
import
javax.annotation.Resource
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Set
;
import
java.util.concurrent.TimeUnit
;
/**
*
* redis 操作工具类
*/
@Component
public
class
RedisService
{
@Resource
(
name
=
"redisTemplate"
)
protected
RedisTemplate
<
Object
,
Object
>
redisTemplate
;
protected
Log
logger
=
LogFactory
.
getLog
(
getClass
());
public
void
set
(
String
key
,
Object
value
)
{
ValueOperations
valueOperations
=
redisTemplate
.
opsForValue
();
valueOperations
.
set
(
key
,
value
);
}
public
Object
get
(
String
key
)
{
return
redisTemplate
.
opsForValue
().
get
(
key
);
}
public
void
setList
(
String
key
,
List
<?>
value
)
{
ListOperations
listOperations
=
redisTemplate
.
opsForList
();
listOperations
.
leftPush
(
key
,
value
);
}
public
void
setOutTime
(
String
key
,
Object
value
,
long
timeout
,
TimeUnit
unit
)
{
ValueOperations
valueOperations
=
redisTemplate
.
opsForValue
();
valueOperations
.
set
(
key
,
value
,
timeout
,
unit
);
}
public
Object
getList
(
String
key
)
{
return
redisTemplate
.
opsForList
().
leftPop
(
key
);
}
public
void
setSet
(
String
key
,
Set
<?>
value
)
{
SetOperations
setOperations
=
redisTemplate
.
opsForSet
();
setOperations
.
add
(
key
,
value
);
}
public
Object
getSet
(
String
key
)
{
return
redisTemplate
.
opsForSet
().
members
(
key
);
}
public
void
setHash
(
String
key
,
Map
<
String
,
?>
value
)
{
HashOperations
hashOperations
=
redisTemplate
.
opsForHash
();
hashOperations
.
putAll
(
key
,
value
);
}
public
Object
getHash
(
String
key
)
{
return
redisTemplate
.
opsForHash
().
entries
(
key
);
}
public
void
delete
(
String
key
)
{
redisTemplate
.
delete
(
key
);
}
}
\ No newline at end of file
\ No newline at end of file
src/main/java/cn/com/glsx/consumer/service/DeviceEventService.java
0 → 100644
View file @
fced48a
package
cn
.
com
.
glsx
.
consumer
.
service
;
import
cn.com.glsx.consumer.entity.AqaraEvent
;
import
cn.com.glsx.consumer.entity.AqaraEventDetail
;
import
cn.com.glsx.consumer.entity.DeviceStatus
;
import
cn.com.glsx.consumer.entity.DeviceStatusDetail
;
import
cn.com.glsx.consumer.utils.HttpUtil
;
import
com.alibaba.fastjson.JSONObject
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.commons.collections.CollectionUtils
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
java.util.ArrayList
;
import
java.util.HashMap
;
import
java.util.List
;
/**
* @author zhouhaibao
* @date 2021/4/18 14:42
*/
@Slf4j
@Service
public
class
DeviceEventService
{
@Autowired
private
InfoService
infoService
;
//@Value("${addUserDevUrl}")
private
String
addUserDevUrl
;
//@Value("${deleteUserDevUrl}")
private
String
deleteUserDevUrl
;
/**
* 处理事件消息
*
* @param aqaraEvent
*/
public
void
processDevMessage
(
AqaraEvent
aqaraEvent
)
{
List
<
AqaraEventDetail
>
data
=
aqaraEvent
.
getData
();
if
(
CollectionUtils
.
isNotEmpty
(
data
))
{
data
.
forEach
(
index
->
{
String
event
=
index
.
getEvent
();
switch
(
event
)
{
case
"GW_BIND"
:
devBind
(
index
);
break
;
case
"SUB_DEV_BIND"
:
devBind
(
index
);
break
;
case
"GW_UN_BIND"
:
devUnBind
(
index
);
break
;
case
"SUB_DEV_UN_BIND"
:
devUnBind
(
index
);
break
;
default
:
break
;
}
});
}
}
/**
* 设备绑定
*
* @param index 入参
*/
private
void
devBind
(
AqaraEventDetail
index
)
{
try
{
String
responseBody
=
HttpUtil
.
sendPost
(
addUserDevUrl
,
JSONObject
.
toJSONString
(
index
),
new
HashMap
<>());
log
.
info
(
"设备id{}保存结果{}"
,
index
.
getDid
(),
responseBody
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
log
.
info
(
"设备id{}保存异常,{}"
,
index
.
getDid
(),
e
);
}
}
/**
* 设备解绑
*
* @param index 入参
*/
private
void
devUnBind
(
AqaraEventDetail
index
)
{
try
{
String
responseBody
=
HttpUtil
.
sendPost
(
deleteUserDevUrl
,
JSONObject
.
toJSONString
(
index
),
new
HashMap
<>());
log
.
info
(
"设备id{}删除结果{}"
,
index
.
getDid
(),
responseBody
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
log
.
info
(
"设备id{}删除异常,{}"
,
index
.
getDid
(),
e
);
}
}
/**
* 处理属性消息
*
* @param aqaraEvent
*/
public
void
processResMessage
(
AqaraEvent
aqaraEvent
)
{
List
<
AqaraEventDetail
>
data
=
aqaraEvent
.
getData
();
if
(
CollectionUtils
.
isNotEmpty
(
data
))
{
data
.
forEach
(
index
->
{
DeviceStatus
deviceStatus
=
new
DeviceStatus
();
deviceStatus
.
setDevId
(
index
.
getDid
());
deviceStatus
.
setProductKey
(
index
.
getModel
());
List
<
DeviceStatusDetail
>
list
=
new
ArrayList
<>();
DeviceStatusDetail
deviceStatusDetail
=
new
DeviceStatusDetail
();
deviceStatusDetail
.
setCode
(
index
.
getAttr
());
deviceStatusDetail
.
setValue
(
index
.
getValue
());
list
.
add
(
deviceStatusDetail
);
deviceStatus
.
setStatus
(
list
);
infoService
.
processMessage
(
JSONObject
.
toJSONString
(
deviceStatus
));
});
}
}
}
src/main/java/cn/com/glsx/consumer/service/InfoService.java
View file @
fced48a
package
cn
.
com
.
glsx
.
consumer
.
service
;
package
cn
.
com
.
glsx
.
consumer
.
service
;
import
cn.com.glsx.consumer.common.Contants
;
import
cn.com.glsx.consumer.common.EDay
;
import
cn.com.glsx.consumer.common.EDay
;
import
cn.com.glsx.consumer.common.ELogic
;
import
cn.com.glsx.consumer.common.ELogic
;
import
cn.com.glsx.consumer.entity.DayInfo
;
import
cn.com.glsx.consumer.entity.DayInfo
;
...
@@ -11,6 +12,7 @@ import cn.com.glsx.consumer.entity.SmartSceneData;
...
@@ -11,6 +12,7 @@ import cn.com.glsx.consumer.entity.SmartSceneData;
import
cn.com.glsx.consumer.mapper.DayInfoMapper
;
import
cn.com.glsx.consumer.mapper.DayInfoMapper
;
import
cn.com.glsx.consumer.mapper.SmartDevideStatusMapper
;
import
cn.com.glsx.consumer.mapper.SmartDevideStatusMapper
;
import
cn.com.glsx.consumer.mapper.SmartSceneDataMapper
;
import
cn.com.glsx.consumer.mapper.SmartSceneDataMapper
;
import
cn.com.glsx.consumer.redis.RedisService
;
import
cn.com.glsx.consumer.utils.DateUtils
;
import
cn.com.glsx.consumer.utils.DateUtils
;
import
cn.com.glsx.consumer.utils.HttpUtil
;
import
cn.com.glsx.consumer.utils.HttpUtil
;
import
com.alibaba.fastjson.JSON
;
import
com.alibaba.fastjson.JSON
;
...
@@ -27,6 +29,7 @@ import java.util.Date;
...
@@ -27,6 +29,7 @@ import java.util.Date;
import
java.util.HashMap
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Map
;
import
java.util.concurrent.TimeUnit
;
import
java.util.stream.Collectors
;
import
java.util.stream.Collectors
;
/**
/**
...
@@ -52,6 +55,9 @@ public class InfoService {
...
@@ -52,6 +55,9 @@ public class InfoService {
@Value
(
"${issueOrderUrl}"
)
@Value
(
"${issueOrderUrl}"
)
private
String
issueOrderUrl
;
private
String
issueOrderUrl
;
@Autowired
private
RedisService
redisService
;
/**
/**
* 处理消息
* 处理消息
*
*
...
@@ -76,6 +82,7 @@ public class InfoService {
...
@@ -76,6 +82,7 @@ public class InfoService {
return
;
return
;
}
}
smartSceneDataList
.
forEach
(
index
->
{
smartSceneDataList
.
forEach
(
index
->
{
String
key
=
index
.
getSceneId
()+
"_"
+
Contants
.
SCENE_STATUS
;
List
<
SmartSceneData
>
sceneDataList
=
null
;
List
<
SmartSceneData
>
sceneDataList
=
null
;
Integer
executeCycle
=
index
.
getExecuteCycle
();
Integer
executeCycle
=
index
.
getExecuteCycle
();
// 判断场景是否触发
// 判断场景是否触发
...
@@ -92,6 +99,20 @@ public class InfoService {
...
@@ -92,6 +99,20 @@ public class InfoService {
}
}
boolean
isTrigger
=
conditionStrategy
.
isTrigger
(
scene_condition_time
,
index
.
getConditionRule
(),
boolean
isTrigger
=
conditionStrategy
.
isTrigger
(
scene_condition_time
,
index
.
getConditionRule
(),
(
SmartSceneData
condition
)
->
getTriggerResult
(
condition
));
(
SmartSceneData
condition
)
->
getTriggerResult
(
condition
));
// 温度控制
Object
o
=
redisService
.
get
(
key
);
if
(
isTrigger
&&!
in
.
getFlag
()){
if
(
o
==
null
){
redisService
.
setOutTime
(
key
,
key
,
1
,
TimeUnit
.
DAYS
);
}
else
{
log
.
info
(
"场景id{}温湿度已经触发过一次"
,
index
.
getSceneId
());
return
;
}
}
// 重新触发的话
if
(!
isTrigger
&&!
in
.
getFlag
()){
redisService
.
delete
(
key
);
}
log
.
info
(
"场景id{}---{}触发条件"
,
index
.
getSceneId
(),
isTrigger
?
"符合"
:
"不符合"
);
log
.
info
(
"场景id{}---{}触发条件"
,
index
.
getSceneId
(),
isTrigger
?
"符合"
:
"不符合"
);
if
(
isTrigger
)
{
if
(
isTrigger
)
{
// 下发场景
// 下发场景
...
@@ -116,15 +137,15 @@ public class InfoService {
...
@@ -116,15 +137,15 @@ public class InfoService {
List
<
DeviceStatusDetail
>
status
=
deviceStatus
.
getStatus
();
List
<
DeviceStatusDetail
>
status
=
deviceStatus
.
getStatus
();
if
(
CollectionUtils
.
isNotEmpty
(
status
))
{
if
(
CollectionUtils
.
isNotEmpty
(
status
))
{
status
.
forEach
(
index
->
{
status
.
forEach
(
index
->
{
SmartDevideStatus
smartDevideStatus
=
smartDevideStatusMapper
.
findDevStatusByIdAndCode
(
deviceStatus
.
getDevId
(),
index
.
getCode
());
// 温度特殊处理
// 温度特殊处理
SmartDevideStatus
smartDevideStatus
=
smartDevideStatusMapper
.
findDevStatusByIdAndCode
(
deviceStatus
.
getDevId
(),
index
.
getCode
());
if
(
smartDevideStatus
!=
null
){
if
(
smartDevideStatus
!=
null
){
String
code
=
index
.
getCode
();
String
code
=
index
.
getCode
();
String
deviceOrderValue
=
smartDevideStatus
.
getDeviceOrderValue
();
String
deviceOrderValue
=
smartDevideStatus
.
getDeviceOrderValue
();
if
(
code
.
equals
(
"va_temperature"
)
||
code
.
equals
(
"va_humidity"
)){
if
(
code
.
equals
(
"va_temperature"
)
||
code
.
equals
(
"va_humidity"
)){
if
(
deviceOrderValue
.
substring
(
0
,
deviceOrderValue
.
length
()-
1
).
equals
(
index
.
getValue
().
substring
(
0
,
deviceOrderValue
.
length
()-
1
))){
if
(
deviceOrderValue
.
substring
(
0
,
deviceOrderValue
.
length
()-
1
).
equals
(
index
.
getValue
().
substring
(
0
,
deviceOrderValue
.
length
()-
1
))){
log
.
info
(
"温湿度误差在0.1之内,不用触发场景"
);
log
.
info
(
"温湿度误差在0.1之内,不用触发场景"
);
return
;
smartDevideStatus
.
setFlag
(
false
)
;
}
}
}
}
}
}
...
...
src/main/java/cn/com/glsx/consumer/utils/AESUtil.java
0 → 100644
View file @
fced48a
package
cn
.
com
.
glsx
.
consumer
.
utils
;
import
javax.crypto.Cipher
;
import
javax.crypto.spec.IvParameterSpec
;
import
javax.crypto.spec.SecretKeySpec
;
import
java.io.UnsupportedEncodingException
;
import
java.security.spec.AlgorithmParameterSpec
;
import
java.util.Base64
;
/**
* AES PKCS5
*/
public
class
AESUtil
{
private
static
final
String
ENCRYPT_AES
=
"AES/CBC/PKCS5Padding"
;
public
static
final
String
CHARSET_UTF_8
=
"utf-8"
;
private
static
final
int
SECURE_KEY_LENGTH
=
16
;
/**
* AES ECB 128加密
* iv偏移量,ECB不用设置
* 加密结果再进行Base64加密
* @param src
* @param key
* @throws Exception
*/
public
static
String
encryptEcb
(
String
src
,
byte
[]
key
)
throws
Exception
{
SecretKeySpec
skeySpec
=
new
SecretKeySpec
(
key
,
"AES"
);
Cipher
cipher
=
Cipher
.
getInstance
(
"AES"
);
cipher
.
init
(
Cipher
.
ENCRYPT_MODE
,
skeySpec
);
byte
[]
content
=
src
.
getBytes
(
CHARSET_UTF_8
);
byte
[]
ret
=
cipher
.
doFinal
(
content
);
return
Base64
.
getEncoder
().
encodeToString
(
ret
);
}
/**
* ECB解密 结果含Base64加密
* @param src
* @param key
* @return
* @throws Exception
*/
public
static
String
decryptEcb
(
String
src
,
byte
[]
key
)
throws
Exception
{
byte
[]
srcByte
=
Base64
.
getDecoder
().
decode
(
src
);
SecretKeySpec
skeySpec
=
new
SecretKeySpec
(
key
,
"AES"
);
Cipher
cipher
=
Cipher
.
getInstance
(
"AES"
);
cipher
.
init
(
Cipher
.
DECRYPT_MODE
,
skeySpec
);
byte
[]
ret
=
cipher
.
doFinal
(
srcByte
);
return
new
String
(
ret
,
CHARSET_UTF_8
);
}
/**
* AES CBC 128加密
* 加密结果再进行Base64编码
* 动态偏移量
* @param src 加密字符串
* @param key 密码 16位
* @throws Exception
*/
public
static
String
encryptCbc
(
String
src
,
byte
[]
key
)
throws
Exception
{
SecretKeySpec
skeySpec
=
new
SecretKeySpec
(
key
,
"AES"
);
AlgorithmParameterSpec
params
=
new
IvParameterSpec
(
getIv
(
key
));
Cipher
cipher
=
Cipher
.
getInstance
(
ENCRYPT_AES
);
cipher
.
init
(
Cipher
.
ENCRYPT_MODE
,
skeySpec
,
params
);
byte
[]
content
=
src
.
getBytes
(
CHARSET_UTF_8
);
byte
[]
ret
=
cipher
.
doFinal
(
content
);
return
Base64
.
getEncoder
().
encodeToString
(
ret
);
}
/**
* AES CBC 解密
* 结果含Base64编码
* 动态偏移量
* @param src 加密字符串
* @param key 密码 16位
* @return
* @throws Exception
*/
public
static
String
decryptCbc
(
String
src
,
byte
[]
key
)
throws
Exception
{
byte
[]
srcByte
=
Base64
.
getDecoder
().
decode
(
src
);
SecretKeySpec
skeySpec
=
new
SecretKeySpec
(
key
,
"AES"
);
AlgorithmParameterSpec
params
=
new
IvParameterSpec
(
getIv
(
key
));
Cipher
cipher
=
Cipher
.
getInstance
(
ENCRYPT_AES
);
cipher
.
init
(
Cipher
.
DECRYPT_MODE
,
skeySpec
,
params
);
byte
[]
ret
=
cipher
.
doFinal
(
srcByte
);
return
new
String
(
ret
,
CHARSET_UTF_8
);
}
/**
* AES 128获取16位密码
* @param key
* @return
* @throws UnsupportedEncodingException
*/
public
static
byte
[]
getAESKey
(
String
key
)
throws
UnsupportedEncodingException
{
byte
[]
keyBytes
=
key
.
getBytes
(
CHARSET_UTF_8
);
// Use the first 16 bytes (or even less if key is shorter)
byte
[]
keyBytes128
=
new
byte
[
SECURE_KEY_LENGTH
];
System
.
arraycopy
(
keyBytes
,
0
,
keyBytes128
,
0
,
Math
.
min
(
keyBytes
.
length
,
SECURE_KEY_LENGTH
));
return
keyBytes128
;
}
/**
* 生成IV偏移量
* @param key
* @return
*/
public
static
byte
[]
getIv
(
byte
[]
key
){
byte
[]
iv
=
new
byte
[
SECURE_KEY_LENGTH
];
for
(
int
i
=
0
;
i
<
SECURE_KEY_LENGTH
&&
i
<
key
.
length
;
i
++){
iv
[
i
]
=
key
[
i
];
}
if
(
key
.
length
<
SECURE_KEY_LENGTH
){
for
(
int
i
=
key
.
length
;
i
<
SECURE_KEY_LENGTH
;
i
++){
iv
[
i
]
=
'0'
;
}
}
return
iv
;
}
public
static
void
main
(
String
[]
args
)
throws
Exception
{
String
aa
=
"{\"did\":\"lumi.158d0001b12fdb\",\"msgId\":\"AC1F000E676355F9630284AC6BD36AF93\",\"property\":\"system_volume\",\"time\":\"1561544292194\",\"value\":\"40\"}"
;
System
.
out
.
println
(
encryptEcb
(
aa
,
"H1rhtzCNw8HGS5zwCDaLf4XJl0Tsvijg"
.
getBytes
()));
}
}
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment