Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
Y
yzg-util
Project
Project
Details
Activity
Releases
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
YZG
yzg-util
Commits
b02d655d
Commit
b02d655d
authored
Sep 13, 2021
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修改实例化关系
parent
38be1e4e
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
103 additions
and
2 deletions
+103
-2
DESHelper.java
.../src/main/java/com/yanzuoguang/util/helper/DESHelper.java
+83
-0
RsaHelper.java
.../src/main/java/com/yanzuoguang/util/helper/RsaHelper.java
+2
-2
TestDes.java
yzg-util-base/src/test/java/helper/TestDes.java
+18
-0
No files found.
yzg-util-base/src/main/java/com/yanzuoguang/util/helper/DESHelper.java
0 → 100644
View file @
b02d655d
package
com
.
yanzuoguang
.
util
.
helper
;
import
javax.crypto.Cipher
;
import
javax.crypto.SecretKey
;
import
javax.crypto.SecretKeyFactory
;
import
javax.crypto.spec.DESKeySpec
;
import
java.security.Key
;
import
java.security.SecureRandom
;
/**
* DES加密
*
* @author 颜佐光
*/
public
class
DESHelper
{
private
static
final
String
ALGORITHM_DES
=
"des"
;
/**
* DES加密
*
* @param key 秘钥key
* @param content 待加密内容
* @return byte[]
*/
public
static
String
DESEncrypt
(
final
String
key
,
final
String
content
)
{
return
RsaHelper
.
encodeBase64
(
processCipher
(
content
.
getBytes
(),
getSecretKey
(
key
),
Cipher
.
ENCRYPT_MODE
,
ALGORITHM_DES
));
}
/**
* DES解密
*
* @param key 秘钥key
* @param encoderContent 已加密内容
* @return byte[]
*/
public
static
byte
[]
DESDecrypt
(
final
String
key
,
final
String
encoderContent
)
{
return
processCipher
(
RsaHelper
.
decodeBase64
(
encoderContent
),
getSecretKey
(
key
),
Cipher
.
DECRYPT_MODE
,
ALGORITHM_DES
);
}
/**
* 根据key生成秘钥
*
* @param key 给定key,要求key至少长度为8个字符
* @return SecretKey
*/
public
static
SecretKey
getSecretKey
(
final
String
key
)
{
try
{
DESKeySpec
desKeySpec
=
new
DESKeySpec
(
key
.
getBytes
());
SecretKeyFactory
instance
=
SecretKeyFactory
.
getInstance
(
ALGORITHM_DES
);
SecretKey
secretKey
=
instance
.
generateSecret
(
desKeySpec
);
return
secretKey
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
null
;
}
/**
* 加密/解密处理
*
* @param processData 待处理的数据
* @param key 提供的密钥
* @param opsMode 工作模式
* @param algorithm 使用的算法
* @return byte[]
*/
private
static
byte
[]
processCipher
(
final
byte
[]
processData
,
final
Key
key
,
final
int
opsMode
,
final
String
algorithm
)
{
try
{
SecureRandom
secureRandom
=
new
SecureRandom
();
Cipher
cipher
=
Cipher
.
getInstance
(
algorithm
);
//初始化
cipher
.
init
(
opsMode
,
key
,
secureRandom
);
return
cipher
.
doFinal
(
processData
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
null
;
}
}
yzg-util-base/src/main/java/com/yanzuoguang/util/helper/RsaHelper.java
View file @
b02d655d
...
...
@@ -295,7 +295,7 @@ public final class RsaHelper {
* @return
* @throws Exception
*/
p
rivate
static
String
encodeBase64
(
byte
[]
source
)
{
p
ublic
static
String
encodeBase64
(
byte
[]
source
)
{
try
{
byte
[]
to
=
Base64Utils
.
encode
(
source
);
return
new
String
(
to
,
SystemContants
.
UTF8
);
...
...
@@ -311,7 +311,7 @@ public final class RsaHelper {
* @return
* @throws Exception
*/
p
rivate
static
byte
[]
decodeBase64
(
String
target
)
{
p
ublic
static
byte
[]
decodeBase64
(
String
target
)
{
try
{
byte
[]
from
=
target
.
getBytes
(
SystemContants
.
UTF8
);
return
Base64Utils
.
decode
(
from
);
...
...
yzg-util-base/src/test/java/helper/TestDes.java
0 → 100644
View file @
b02d655d
package
helper
;
import
com.yanzuoguang.util.helper.DESHelper
;
import
org.junit.Test
;
public
class
TestDes
{
String
pwd
=
"tubida@yanzuoguang@good@boy@!@#^%$"
;
// String str = "{\"c\":\"64711099423332\",\"l\":1631524416,\"s\":\"671ec2998053b6de9b76bc8d09e00f1b\",\"t\":\"company-pangding-0000001\"}";
String
str
=
"64711099423332,1631524416,company-pangding-0000001,671ec2998053b6de9b76bc8d09e00f1b"
;
@Test
public
void
test
()
{
System
.
out
.
println
(
str
.
length
());
String
des
=
DESHelper
.
DESEncrypt
(
pwd
,
str
);
System
.
out
.
println
(
des
.
length
());
System
.
out
.
println
(
des
);
}
}
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