Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
searchhotcrawler
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
zhiwei
searchhotcrawler
Commits
9c03b231
Commit
9c03b231
authored
Sep 16, 2021
by
chenweitao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
增加竞品数据补充加解密
parent
51a4bfa2
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
91 additions
and
8 deletions
+91
-8
src/main/java/com/zhiwei/searchhotcrawler/util/AESUtils.java
+91
-8
src/test/java/weiboTest/WeiboTopInfoTest.java
+0
-0
No files found.
src/main/java/com/zhiwei/searchhotcrawler/util/AESUtils.java
View file @
9c03b231
package
com
.
zhiwei
.
searchhotcrawler
.
util
;
import
org.apache.commons.codec.binary.Base64
;
import
org.apache.commons.lang3.StringUtils
;
import
sun.misc.BASE64Decoder
;
import
java.io.UnsupportedEncodingException
;
import
java.math.BigInteger
;
import
java.security.SecureRandom
;
import
java.util.Arrays
;
import
java.util.Objects
;
import
javax.crypto.Cipher
;
import
javax.crypto.KeyGenerator
;
import
javax.crypto.spec.IvParameterSpec
;
import
javax.crypto.spec.SecretKeySpec
;
import
static
java
.
util
.
Objects
.
isNull
;
/**
* @author
* @version V1.0
...
...
@@ -14,6 +24,9 @@ import javax.crypto.spec.SecretKeySpec;
* @date 2017-12-28 14:26
**/
public
class
AESUtils
{
//默认偏移
public
static
final
String
VI_STR
=
"0102030405060708"
;
private
static
final
String
ALGORITHMSTR
=
"AES/ECB/PKCS5Padding"
;
private
AESUtils
()
{
}
...
...
@@ -25,11 +38,19 @@ public class AESUtils {
* @return 加密后的字符串
*/
public
static
String
encrypt
(
String
secret
,
String
value
)
{
return
encrypt
(
secret
,
value
,
VI_STR
);
}
public
static
String
encrypt
(
String
secret
,
String
value
,
String
ivStr
)
{
SecretKeySpec
keySpec
=
getKey
(
secret
);
IvParameterSpec
iv
=
new
IvParameterSpec
(
"0102030405060708"
.
getBytes
());
try
{
Cipher
cipher
=
Cipher
.
getInstance
(
"AES/CBC/PKCS5Padding"
);
cipher
.
init
(
Cipher
.
ENCRYPT_MODE
,
keySpec
,
iv
);
Cipher
cipher
=
Cipher
.
getInstance
(
ALGORITHMSTR
);
if
(
isNull
(
ivStr
)){
cipher
.
init
(
Cipher
.
ENCRYPT_MODE
,
keySpec
);
}
else
{
IvParameterSpec
iv
=
new
IvParameterSpec
(
ivStr
.
getBytes
());
cipher
.
init
(
Cipher
.
ENCRYPT_MODE
,
keySpec
,
iv
);
}
byte
[]
encrypted
=
cipher
.
doFinal
(
value
.
getBytes
(
"UTF-8"
));
return
parseByte2HexStr
(
encrypted
);
}
catch
(
Exception
e
)
{
...
...
@@ -37,13 +58,31 @@ public class AESUtils {
}
}
public
static
byte
[]
encrypt
(
byte
[]
secretBytes
,
byte
[]
valueBytes
,
String
ivStr
)
{
SecretKeySpec
keySpec
=
new
SecretKeySpec
(
Arrays
.
copyOf
(
secretBytes
,
16
),
"AES"
);
try
{
Cipher
cipher
=
Cipher
.
getInstance
(
ALGORITHMSTR
);
if
(
isNull
(
ivStr
)){
cipher
.
init
(
Cipher
.
ENCRYPT_MODE
,
keySpec
);
}
else
{
IvParameterSpec
iv
=
new
IvParameterSpec
(
ivStr
.
getBytes
());
cipher
.
init
(
Cipher
.
ENCRYPT_MODE
,
keySpec
,
iv
);
}
byte
[]
encrypted
=
cipher
.
doFinal
(
valueBytes
);
return
encrypted
;
// return parseByte2HexStr(encrypted);
}
catch
(
Exception
e
)
{
throw
new
RuntimeException
(
e
);
}
}
public
static
void
main
(
String
[]
args
)
{
String
jsm
=
AESUtils
.
encrypt
(
"wechat"
,
"shenjinzhu"
);
System
.
out
.
println
(
jsm
);
String
jm
=
AESUtils
.
decrypt
(
"wechat"
,
jsm
);
System
.
out
.
println
(
jm
);
}
/**
* 解密
*
...
...
@@ -52,11 +91,27 @@ public class AESUtils {
* @return 解密后的字符串
*/
public
static
String
decrypt
(
String
secret
,
String
value
)
{
return
decrypt
(
secret
,
value
,
VI_STR
);
}
/**
* 解密
*
* @param secret 密钥
* @param value 待解密字符串
* @param ivStr 偏移字符
* @return 解密后的字符串
*/
public
static
String
decrypt
(
String
secret
,
String
value
,
String
ivStr
)
{
SecretKeySpec
keySpec
=
getKey
(
secret
);
IvParameterSpec
iv
=
new
IvParameterSpec
(
"0102030405060708"
.
getBytes
());
try
{
Cipher
cipher
=
Cipher
.
getInstance
(
"AES/CBC/PKCS5Padding"
);
cipher
.
init
(
Cipher
.
DECRYPT_MODE
,
keySpec
,
iv
);
Cipher
cipher
=
Cipher
.
getInstance
(
ALGORITHMSTR
);
if
(
isNull
(
ivStr
)){
cipher
.
init
(
Cipher
.
DECRYPT_MODE
,
keySpec
);
}
else
{
IvParameterSpec
iv
=
new
IvParameterSpec
(
ivStr
.
getBytes
());
cipher
.
init
(
Cipher
.
DECRYPT_MODE
,
keySpec
,
iv
);
}
byte
[]
encrypted1
=
parseHexStr2Byte
(
value
);
byte
[]
original
=
cipher
.
doFinal
(
encrypted1
);
return
new
String
(
original
,
"UTF-8"
);
...
...
@@ -66,6 +121,32 @@ public class AESUtils {
}
/**
* 解密
*
* @param secretBytes 密钥组
* @param valueBytes 待解密字符组
* @param ivStr 偏移字符
* @return 解密后的字符串
*/
public
static
String
decrypt
(
byte
[]
secretBytes
,
byte
[]
valueBytes
,
String
ivStr
)
{
SecretKeySpec
keySpec
=
new
SecretKeySpec
(
Arrays
.
copyOf
(
secretBytes
,
16
),
"AES"
);
try
{
Cipher
cipher
=
Cipher
.
getInstance
(
ALGORITHMSTR
);
if
(
isNull
(
ivStr
)){
cipher
.
init
(
Cipher
.
DECRYPT_MODE
,
keySpec
);
}
else
{
IvParameterSpec
iv
=
new
IvParameterSpec
(
ivStr
.
getBytes
());
cipher
.
init
(
Cipher
.
DECRYPT_MODE
,
keySpec
,
iv
);
}
// byte[] encrypted1 = parseHexStr2Byte(value);
byte
[]
original
=
cipher
.
doFinal
(
valueBytes
);
return
new
String
(
original
,
"UTF-8"
);
}
catch
(
Exception
e
)
{
throw
new
RuntimeException
(
e
);
}
}
/**
* 生成加密的密钥,保证长度为16位
*
* @param secret 用户的密钥
...
...
@@ -75,6 +156,7 @@ public class AESUtils {
byte
[]
bytes
;
try
{
bytes
=
secret
.
getBytes
(
"UTF-8"
);
// return new SecretKeySpec(Arrays.copyOf(bytes, 32), "AES");
return
new
SecretKeySpec
(
Arrays
.
copyOf
(
bytes
,
16
),
"AES"
);
}
catch
(
UnsupportedEncodingException
e
)
{
e
.
printStackTrace
();
...
...
@@ -107,8 +189,9 @@ public class AESUtils {
* @return
*/
public
static
byte
[]
parseHexStr2Byte
(
String
hexStr
)
{
if
(
hexStr
.
length
()
<
1
)
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
);
...
...
src/test/java/weiboTest/WeiboTopInfoTest.java
0 → 100644
View file @
9c03b231
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
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 comment