Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
smileNicky
commonUtil
提交
1ffe7d10
C
commonUtil
项目概览
smileNicky
/
commonUtil
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
commonUtil
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
1ffe7d10
编写于
9月 11, 2020
作者:
N
nicky
提交者:
马增群
9月 11, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
commit
上级
02cf0577
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
280 addition
and
2 deletion
+280
-2
javaUtil/pom.xml
javaUtil/pom.xml
+5
-1
javaUtil/src/main/java/com/example/common/util/encrypt/DESUtils.java
...c/main/java/com/example/common/util/encrypt/DESUtils.java
+1
-1
javaUtil/src/main/java/com/example/common/util/encrypt/DesSupport.java
...main/java/com/example/common/util/encrypt/DesSupport.java
+129
-0
javaUtil/src/main/java/com/example/common/util/encrypt/RSAUtils.java
...c/main/java/com/example/common/util/encrypt/RSAUtils.java
+145
-0
未找到文件。
javaUtil/pom.xml
浏览文件 @
1ffe7d10
...
...
@@ -8,7 +8,11 @@
<artifactId>
javaUtil
</artifactId>
<version>
1.0-SNAPSHOT
</version>
<dependencies>
<dependency>
<groupId>
commons-codec
</groupId>
<artifactId>
commons-codec
</artifactId>
<version>
1.10
</version>
</dependency>
</dependencies>
...
...
javaUtil/src/main/java/com/example/common/util/encrypt/DESUtils.java
浏览文件 @
1ffe7d10
...
...
@@ -8,7 +8,7 @@ import java.security.spec.InvalidKeySpecException;
/**
* <pre>
*
*
DES加密工具类
* </pre>
*
* <pre>
...
...
javaUtil/src/main/java/com/example/common/util/encrypt/DesSupport.java
0 → 100644
浏览文件 @
1ffe7d10
package
com.example.common.util.encrypt
;
import
java.nio.charset.Charset
;
import
javax.crypto.Cipher
;
import
javax.crypto.SecretKey
;
import
javax.crypto.SecretKeyFactory
;
import
javax.crypto.spec.DESKeySpec
;
import
javax.crypto.spec.IvParameterSpec
;
public
class
DesSupport
{
/**
* 加密
* @param srcStr
* @param charset
* @param sKey
* @return
*/
public
String
encrypt
(
String
srcStr
,
Charset
charset
,
String
sKey
)
{
byte
[]
src
=
srcStr
.
getBytes
(
charset
);
byte
[]
buf
=
encrypt
(
src
,
sKey
);
return
parseByte2HexStr
(
buf
);
}
/**
* 解密
*
* @param hexStr
* @param sKey
* @return
* @throws Exception
*/
public
String
decrypt
(
String
hexStr
,
Charset
charset
,
String
sKey
)
throws
Exception
{
byte
[]
src
=
parseHexStr2Byte
(
hexStr
);
byte
[]
buf
=
decrypt
(
src
,
sKey
);
return
new
String
(
buf
,
charset
);
}
/**
* 加密
* @param data
* @param sKey
* @return
*/
private
static
byte
[]
encrypt
(
byte
[]
data
,
String
sKey
)
{
try
{
byte
[]
key
=
sKey
.
getBytes
();
// 初始化向量
IvParameterSpec
iv
=
new
IvParameterSpec
(
key
);
DESKeySpec
desKey
=
new
DESKeySpec
(
key
);
// 创建一个密匙工厂,然后用它把DESKeySpec转换成securekey
SecretKeyFactory
keyFactory
=
SecretKeyFactory
.
getInstance
(
"DES"
);
SecretKey
securekey
=
keyFactory
.
generateSecret
(
desKey
);
// Cipher对象实际完成加密操作
Cipher
cipher
=
Cipher
.
getInstance
(
"DES/CBC/PKCS5Padding"
);
// 用密匙初始化Cipher对象
cipher
.
init
(
Cipher
.
ENCRYPT_MODE
,
securekey
,
iv
);
// 现在,获取数据并加密
// 正式执行加密操作
return
cipher
.
doFinal
(
data
);
}
catch
(
Throwable
e
)
{
e
.
printStackTrace
();
}
return
null
;
}
/**
* 解密
* @param src
* @param sKey
* @return
* @throws Exception
*/
private
static
byte
[]
decrypt
(
byte
[]
src
,
String
sKey
)
throws
Exception
{
byte
[]
key
=
sKey
.
getBytes
();
// 初始化向量
IvParameterSpec
iv
=
new
IvParameterSpec
(
key
);
// 创建一个DESKeySpec对象
DESKeySpec
desKey
=
new
DESKeySpec
(
key
);
// 创建一个密匙工厂
SecretKeyFactory
keyFactory
=
SecretKeyFactory
.
getInstance
(
"DES"
);
// 将DESKeySpec对象转换成SecretKey对象
SecretKey
securekey
=
keyFactory
.
generateSecret
(
desKey
);
// Cipher对象实际完成解密操作
Cipher
cipher
=
Cipher
.
getInstance
(
"DES/CBC/PKCS5Padding"
);
// 用密匙初始化Cipher对象
cipher
.
init
(
Cipher
.
DECRYPT_MODE
,
securekey
,
iv
);
// 真正开始解密操作
return
cipher
.
doFinal
(
src
);
}
/**
* 将二进制转换成16进制
*
* @param buf
* @return
*/
private
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
*/
private
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
;
}
}
javaUtil/src/main/java/com/example/common/util/encrypt/RSAUtils.java
0 → 100644
浏览文件 @
1ffe7d10
package
com.example.common.util.encrypt
;
import
org.apache.commons.codec.binary.Base64
;
import
javax.crypto.Cipher
;
import
java.security.*
;
import
java.security.interfaces.RSAPrivateKey
;
import
java.security.interfaces.RSAPublicKey
;
import
java.security.spec.PKCS8EncodedKeySpec
;
import
java.security.spec.X509EncodedKeySpec
;
/**
* <pre>
* RSA加解密工具类
* </pre>
*
* <pre>
* @author mazq
* 修改记录
* 修改后版本: 修改人: 修改日期: 2020/09/08 10:05 修改内容:
* </pre>
*/
public
class
RSAUtils
{
/**
* 公钥解密
*
* @param publicKeyText
* @param text
* @return
* @throws Exception
*/
public
static
String
decryptByPublicKey
(
String
publicKeyText
,
String
text
)
throws
Exception
{
X509EncodedKeySpec
x509EncodedKeySpec
=
new
X509EncodedKeySpec
(
Base64
.
decodeBase64
(
publicKeyText
));
KeyFactory
keyFactory
=
KeyFactory
.
getInstance
(
"RSA"
);
PublicKey
publicKey
=
keyFactory
.
generatePublic
(
x509EncodedKeySpec
);
Cipher
cipher
=
Cipher
.
getInstance
(
"RSA"
);
cipher
.
init
(
Cipher
.
DECRYPT_MODE
,
publicKey
);
byte
[]
result
=
cipher
.
doFinal
(
Base64
.
decodeBase64
(
text
));
return
new
String
(
result
);
}
/**
* 私钥加密
*
* @param privateKeyText
* @param text
* @return
* @throws Exception
*/
public
static
String
encryptByPrivateKey
(
String
privateKeyText
,
String
text
)
throws
Exception
{
PKCS8EncodedKeySpec
pkcs8EncodedKeySpec
=
new
PKCS8EncodedKeySpec
(
Base64
.
decodeBase64
(
privateKeyText
));
KeyFactory
keyFactory
=
KeyFactory
.
getInstance
(
"RSA"
);
PrivateKey
privateKey
=
keyFactory
.
generatePrivate
(
pkcs8EncodedKeySpec
);
Cipher
cipher
=
Cipher
.
getInstance
(
"RSA"
);
cipher
.
init
(
Cipher
.
ENCRYPT_MODE
,
privateKey
);
byte
[]
result
=
cipher
.
doFinal
(
text
.
getBytes
());
return
Base64
.
encodeBase64String
(
result
);
}
/**
* 私钥解密
*
* @param privateKeyText
* @param text
* @return
* @throws Exception
*/
public
static
String
decryptByPrivateKey
(
String
privateKeyText
,
String
text
)
throws
Exception
{
PKCS8EncodedKeySpec
pkcs8EncodedKeySpec5
=
new
PKCS8EncodedKeySpec
(
Base64
.
decodeBase64
(
privateKeyText
));
KeyFactory
keyFactory
=
KeyFactory
.
getInstance
(
"RSA"
);
PrivateKey
privateKey
=
keyFactory
.
generatePrivate
(
pkcs8EncodedKeySpec5
);
Cipher
cipher
=
Cipher
.
getInstance
(
"RSA"
);
cipher
.
init
(
Cipher
.
DECRYPT_MODE
,
privateKey
);
byte
[]
result
=
cipher
.
doFinal
(
Base64
.
decodeBase64
(
text
));
return
new
String
(
result
);
}
/**
* 公钥加密
*
* @param publicKeyText
* @param text
* @return
*/
public
static
String
encryptByPublicKey
(
String
publicKeyText
,
String
text
)
throws
Exception
{
X509EncodedKeySpec
x509EncodedKeySpec2
=
new
X509EncodedKeySpec
(
Base64
.
decodeBase64
(
publicKeyText
));
KeyFactory
keyFactory
=
KeyFactory
.
getInstance
(
"RSA"
);
PublicKey
publicKey
=
keyFactory
.
generatePublic
(
x509EncodedKeySpec2
);
Cipher
cipher
=
Cipher
.
getInstance
(
"RSA"
);
cipher
.
init
(
Cipher
.
ENCRYPT_MODE
,
publicKey
);
byte
[]
result
=
cipher
.
doFinal
(
text
.
getBytes
());
return
Base64
.
encodeBase64String
(
result
);
}
/**
* 构建RSA密钥对
*
* @return
* @throws NoSuchAlgorithmException
*/
public
static
RSAKeyPair
generateKeyPair
()
throws
NoSuchAlgorithmException
{
KeyPairGenerator
keyPairGenerator
=
KeyPairGenerator
.
getInstance
(
"RSA"
);
keyPairGenerator
.
initialize
(
1024
);
KeyPair
keyPair
=
keyPairGenerator
.
generateKeyPair
();
RSAPublicKey
rsaPublicKey
=
(
RSAPublicKey
)
keyPair
.
getPublic
();
RSAPrivateKey
rsaPrivateKey
=
(
RSAPrivateKey
)
keyPair
.
getPrivate
();
String
publicKeyString
=
Base64
.
encodeBase64String
(
rsaPublicKey
.
getEncoded
());
String
privateKeyString
=
Base64
.
encodeBase64String
(
rsaPrivateKey
.
getEncoded
());
RSAKeyPair
rsaKeyPair
=
new
RSAKeyPair
(
publicKeyString
,
privateKeyString
);
return
rsaKeyPair
;
}
/**
* RSA密钥对对象
*/
public
static
class
RSAKeyPair
{
private
String
publicKey
;
private
String
privateKey
;
public
RSAKeyPair
(
String
publicKey
,
String
privateKey
)
{
this
.
publicKey
=
publicKey
;
this
.
privateKey
=
privateKey
;
}
public
String
getPublicKey
()
{
return
publicKey
;
}
public
String
getPrivateKey
()
{
return
privateKey
;
}
}
public
static
void
main
(
String
[]
args
)
throws
Exception
{
RSAKeyPair
keyPair
=
generateKeyPair
();
String
publicKey
=
keyPair
.
getPublicKey
();
Long
s
=
System
.
currentTimeMillis
();
String
source
=
"TEST"
+
s
;
String
text1
=
encryptByPublicKey
(
publicKey
,
source
);
System
.
out
.
println
(
text1
);
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录