Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Q
qa-platform
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
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
QA
qa-platform
Commits
6a3162ac
Commit
6a3162ac
authored
Jan 25, 2021
by
黎博
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
新增kdsp解密接口
parent
f6cf2815
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
321 additions
and
0 deletions
+321
-0
KdspDecryptController.java
...qg/qaplatform/zdata/controller/KdspDecryptController.java
+28
-0
AESUtil.java
src/main/java/cn/qg/qaplatform/zdata/utils/AESUtil.java
+100
-0
ConfigureEncryptAndDecrypt.java
...qg/qaplatform/zdata/utils/ConfigureEncryptAndDecrypt.java
+9
-0
RSA.java
src/main/java/cn/qg/qaplatform/zdata/utils/RSA.java
+161
-0
EncryptRequest.java
src/main/java/cn/qg/qaplatform/zdata/vo/EncryptRequest.java
+23
-0
No files found.
src/main/java/cn/qg/qaplatform/zdata/controller/KdspDecryptController.java
0 → 100644
View file @
6a3162ac
package
cn
.
qg
.
qaplatform
.
zdata
.
controller
;
import
cn.qg.qaplatform.utils.JsonResult
;
import
cn.qg.qaplatform.zdata.utils.AESUtil
;
import
cn.qg.qaplatform.zdata.utils.RSA
;
import
cn.qg.qaplatform.zdata.vo.EncryptRequest
;
import
com.alibaba.fastjson.JSON
;
import
org.springframework.web.bind.annotation.CrossOrigin
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RestController
;
@CrossOrigin
@RestController
@RequestMapping
(
"/kdsp"
)
public
class
KdspDecryptController
{
@GetMapping
(
"/decrypt"
)
public
JsonResult
decryptKdsp
(
String
data
)
throws
Exception
{
EncryptRequest
encryptRequest
=
JSON
.
parseObject
(
data
,
EncryptRequest
.
class
);
String
privateKey
=
"MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAIZ/jCc0lcdcznjCybFPCGmI9oCnbvMzdCyta7X4Y4ygIOYAQ5vaNiPI2HAEh5X15jR/Cgr4x9YHLBuifKnJezWPmX+ZOYVFDwhlj7FkXS3TVyiuegykomdyg2LCpz06eyLAY+rMiz+ItqUGiemmqFZKfSD328VLaqCvRac5aWotAgMBAAECgYA8E1Kt3F4ee4S6Cf9/FFcklInOc3sqZEw6/IdE85q7oHaxZWLNhY1zNG4AsvVA9WILBsecmPzhy7hW66AVUqqWXA8jLIg/J3UyIRQo1sH9cX+3iERhNZluxXzxcX/UXrVQihyEFWZbehtpXFfPq1F7XzkTKy8qSodRLO79F8Tc/QJBAOF2vZ4Ah8GhiZP/KMYX7Q1fY5x/zTKmnNs1fJGDoRmgcF47Inzf2Ovjo+do/QdCf+Bzki6WG0AOpIHKksuw5r8CQQCYtttPfP8FKRxumcczNmkaKDWjrKElwRcGef5OuNBb7Crso9gUY+sL9/4kQHfmBfCS6KVbAA56RSdeSWFYZDYTAkA58pbRoTlDHw2JKC+Gmmem5Dlp478NoZz3Ckw16irht0aqMXCvv6i1GBDsTSObVhAf1BRiPNpMIHdPlI0eJR1pAkA7Oge6P0cEcFpHhrpb4UlHzLpo3QWuh8FvUJbRXxnn3KMVHcIqvkmlXauh0aLugfU4dKPUtCPpRbKr/ZeUWWEjAkAN5D5hH16RwrR6KN8W7t0gnlwOECPD8KbBQeWSja9r8C/yWMmr4RsWirBoDSYu4IG8jnrNcmYk8l1eAWmNyJWe"
;
//使用服务端私钥对AES key进行解密
String
encryptKey
=
RSA
.
decryptFromBase64
(
encryptRequest
.
getEncryptKey
(),
privateKey
);
String
result
=
AESUtil
.
decryptFromBase64
(
encryptRequest
.
getBody
(),
encryptKey
);
return
JsonResult
.
success
(
result
);
}
}
src/main/java/cn/qg/qaplatform/zdata/utils/AESUtil.java
0 → 100644
View file @
6a3162ac
package
cn
.
qg
.
qaplatform
.
zdata
.
utils
;
import
javax.crypto.Cipher
;
import
javax.crypto.spec.IvParameterSpec
;
import
javax.crypto.spec.SecretKeySpec
;
import
java.util.Base64
;
/**
* kdsp
* AES对称加密工具类
*/
public
class
AESUtil
{
private
static
final
String
CHAR_LIBRARY
=
"1234567890abcdefghijklmnopqrstuvwxyz"
;
private
static
final
int
KEY_SIZE
=
16
;
/**
* 生成16位随机密钥
*
* @return
*/
public
static
String
getRandomKey
()
{
StringBuilder
result
=
new
StringBuilder
();
for
(
int
i
=
0
;
i
<
KEY_SIZE
;
i
++)
{
int
randomIdx
=
(
int
)
(
Math
.
random
()
*
(
CHAR_LIBRARY
.
length
()
-
1
));
result
.
append
(
CHAR_LIBRARY
.
charAt
(
randomIdx
));
}
return
result
.
toString
();
}
// 加密
public
static
String
encryptToBase64
(
String
sSrc
,
String
sKey
)
throws
Exception
{
if
(
sKey
==
null
)
{
System
.
out
.
print
(
"Key为空null"
);
return
null
;
}
// 判断Key是否为16位
if
(
sKey
.
length
()
!=
16
)
{
System
.
out
.
println
(
sKey
);
System
.
out
.
print
(
"Key长度不是16位"
);
return
null
;
}
byte
[]
raw
=
sKey
.
getBytes
(
ConfigureEncryptAndDecrypt
.
CHAR_ENCODING
);
for
(
byte
b
:
raw
)
{
System
.
out
.
println
(
"======"
+
b
);
}
SecretKeySpec
skeySpec
=
new
SecretKeySpec
(
raw
,
"AES"
);
Cipher
cipher
=
Cipher
.
getInstance
(
ConfigureEncryptAndDecrypt
.
AES_ALGORITHM
);
cipher
.
init
(
Cipher
.
ENCRYPT_MODE
,
skeySpec
);
byte
[]
encrypted
=
cipher
.
doFinal
(
sSrc
.
getBytes
(
ConfigureEncryptAndDecrypt
.
CHAR_ENCODING
));
// 此处使用BASE64做转码功能,同时能起到2次加密的作用。
return
new
String
(
Base64
.
getEncoder
().
encode
(
encrypted
),
ConfigureEncryptAndDecrypt
.
CHAR_ENCODING
);
}
// 解密
public
static
String
decryptFromBase64
(
String
sSrc
,
String
sKey
)
throws
Exception
{
// 判断Key是否正确
if
(
sKey
==
null
)
{
System
.
out
.
print
(
"Key为空null"
);
return
null
;
}
// 判断Key是否为16位
if
(
sKey
.
length
()
!=
16
)
{
System
.
out
.
print
(
"Key长度不是16位"
);
return
null
;
}
// byte[] raw = sKey.getBytes(ConfigureEncryptAndDecrypt.CHAR_ENCODING);
byte
[]
raw
=
sKey
.
getBytes
();
// byte[] raw = sKey.getBytes(ConfigureEncryptAndDecrypt.CHAR_ENCODING);
SecretKeySpec
skeySpec
=
new
SecretKeySpec
(
raw
,
"AES"
);
Cipher
cipher
=
Cipher
.
getInstance
(
ConfigureEncryptAndDecrypt
.
AES_ALGORITHM
);
cipher
.
init
(
Cipher
.
DECRYPT_MODE
,
skeySpec
,
new
IvParameterSpec
(
raw
));
// 先用base64解密
byte
[]
encrypted1
=
Base64
.
getDecoder
().
decode
(
sSrc
.
getBytes
());
byte
[]
original
=
cipher
.
doFinal
(
encrypted1
);
return
new
String
(
original
);
// return new String(original, ConfigureEncryptAndDecrypt.CHAR_ENCODING);
}
public
static
void
main
(
String
[]
args
)
throws
Exception
{
for
(
int
i
=
0
;
i
<
100
;
i
++)
{
System
.
out
.
println
(
getRandomKey
());
}
/*
* 此处使用AES-128-ECB加密模式,key需要为16位。
String cKey = "12345678901a3456";
// 需要加密的字串
String cSrc = "www.gowhere.so中文";
System.out.println(cSrc);
// 加密
String enString = AES.encryptToBase64(cSrc, cKey);
System.out.println("加密后的字串是:" + enString);
// 解密
String DeString = AES.decryptFromBase64(enString, cKey);
System.out.println("解密后的字串是:" + DeString);*/
}
}
src/main/java/cn/qg/qaplatform/zdata/utils/ConfigureEncryptAndDecrypt.java
0 → 100644
View file @
6a3162ac
package
cn
.
qg
.
qaplatform
.
zdata
.
utils
;
public
class
ConfigureEncryptAndDecrypt
{
public
static
final
String
CHAR_ENCODING
=
"UTF-8"
;
// public static final String AES_ALGORITHM = "AES/ECB/PKCS5Padding"; // "算法/模式/补码方式"
public
static
final
String
AES_ALGORITHM
=
"AES/CBC/PKCS5Padding"
;
// "算法/模式/补码方式"
// public static final String AES_ALGORITHM = "AES/CBC/PKCS7Padding"; // "算法/模式/补码方式"
public
static
final
String
RSA_ALGORITHM
=
"RSA/ECB/PKCS1Padding"
;
// "算法/模式/补码方式"
}
\ No newline at end of file
src/main/java/cn/qg/qaplatform/zdata/utils/RSA.java
0 → 100644
View file @
6a3162ac
package
cn
.
qg
.
qaplatform
.
zdata
.
utils
;
import
java.security.Key
;
import
java.security.KeyFactory
;
import
java.security.KeyPair
;
import
java.security.KeyPairGenerator
;
import
java.security.PrivateKey
;
import
java.security.PublicKey
;
import
java.security.SecureRandom
;
import
java.security.Signature
;
import
java.security.spec.PKCS8EncodedKeySpec
;
import
java.security.spec.X509EncodedKeySpec
;
import
java.util.Base64
;
import
java.util.HashMap
;
import
java.util.Map
;
import
javax.crypto.Cipher
;
/**
* kdsp
* RSA非对称加密工具类
*/
public
class
RSA
{
/** 指定key的大小 */
private
static
int
KEYSIZE
=
1024
;
/**
* 生成密钥对
*/
public
static
Map
<
String
,
String
>
generateKeyPair
()
throws
Exception
{
/** RSA算法要求有一个可信任的随机数源 */
SecureRandom
sr
=
new
SecureRandom
();
/** 为RSA算法创建一个KeyPairGenerator对象 */
KeyPairGenerator
kpg
=
KeyPairGenerator
.
getInstance
(
"RSA"
);
/** 利用上面的随机数据源初始化这个KeyPairGenerator对象 */
kpg
.
initialize
(
KEYSIZE
,
sr
);
/** 生成密匙对 */
KeyPair
kp
=
kpg
.
generateKeyPair
();
/** 得到公钥 */
Key
publicKey
=
kp
.
getPublic
();
byte
[]
publicKeyBytes
=
publicKey
.
getEncoded
();
String
pub
=
new
String
(
Base64
.
getEncoder
().
encode
(
publicKeyBytes
),
ConfigureEncryptAndDecrypt
.
CHAR_ENCODING
);
/** 得到私钥 */
Key
privateKey
=
kp
.
getPrivate
();
byte
[]
privateKeyBytes
=
privateKey
.
getEncoded
();
String
pri
=
new
String
(
Base64
.
getEncoder
().
encode
(
privateKeyBytes
),
ConfigureEncryptAndDecrypt
.
CHAR_ENCODING
);
Map
<
String
,
String
>
map
=
new
HashMap
<
String
,
String
>();
map
.
put
(
"publicKey"
,
pub
);
map
.
put
(
"privateKey"
,
pri
);
return
map
;
}
/**
* 使用私钥生成签名
* @param content
* @param privateKey
* @return
* @throws Exception
*/
public
static
String
sign
(
String
content
,
String
privateKey
)
throws
Exception
{
PrivateKey
priKey
=
getPrivateKey
(
privateKey
);
Signature
signature
=
Signature
.
getInstance
(
"SHA1WithRSA"
);
signature
.
initSign
(
priKey
);
signature
.
update
(
content
.
getBytes
(
ConfigureEncryptAndDecrypt
.
CHAR_ENCODING
));
byte
[]
signed
=
signature
.
sign
();
return
new
String
(
Base64
.
getEncoder
().
encode
(
signed
),
ConfigureEncryptAndDecrypt
.
CHAR_ENCODING
);
}
/**
* 使用公钥验证签名
* @param content
* @param sign 签名
* @param publicKey
* @return
* @throws Exception
*/
public
static
boolean
checkSign
(
String
content
,
String
sign
,
String
publicKey
)
throws
Exception
{
PublicKey
pubKey
=
getPublicKey
(
publicKey
);
Signature
signature
=
Signature
.
getInstance
(
"SHA1WithRSA"
);
signature
.
initVerify
(
pubKey
);
signature
.
update
(
content
.
getBytes
(
ConfigureEncryptAndDecrypt
.
CHAR_ENCODING
));
return
signature
.
verify
(
Base64
.
getDecoder
().
decode
(
sign
));
}
/**
* 加密方法
* @param source 明文
* @param publicKey 公钥
* @return
* @throws Exception
*/
public
static
String
encryptToBase64
(
String
source
,
String
publicKey
)
throws
Exception
{
Key
key
=
getPublicKey
(
publicKey
);
/** 得到Cipher对象来实现对源数据的RSA加密 */
Cipher
cipher
=
Cipher
.
getInstance
(
ConfigureEncryptAndDecrypt
.
RSA_ALGORITHM
);
cipher
.
init
(
Cipher
.
ENCRYPT_MODE
,
key
);
byte
[]
b
=
source
.
getBytes
(
ConfigureEncryptAndDecrypt
.
CHAR_ENCODING
);
/** 执行加密操作 */
byte
[]
b1
=
cipher
.
doFinal
(
b
);
return
new
String
(
Base64
.
getEncoder
().
encode
(
b1
),
ConfigureEncryptAndDecrypt
.
CHAR_ENCODING
);
}
/**
* 解密算法
* @param cryptograph 密文
* @param privateKey 私钥
* @return
* @throws Exception
*/
public
static
String
decryptFromBase64
(
String
cryptograph
,
String
privateKey
)
throws
Exception
{
Key
key
=
getPrivateKey
(
privateKey
);
/** 得到Cipher对象对已用公钥加密的数据进行RSA解密 */
Cipher
cipher
=
Cipher
.
getInstance
(
ConfigureEncryptAndDecrypt
.
RSA_ALGORITHM
);
cipher
.
init
(
Cipher
.
DECRYPT_MODE
,
key
);
byte
[]
b1
=
Base64
.
getDecoder
().
decode
(
cryptograph
.
getBytes
());
/** 执行解密操作 */
byte
[]
b
=
cipher
.
doFinal
(
b1
);
return
new
String
(
b
);
}
/**
* 得到私钥
* @param key 密钥字符串(经过base64编码)
* @throws Exception
*/
private
static
PrivateKey
getPrivateKey
(
String
key
)
throws
Exception
{
PKCS8EncodedKeySpec
keySpec
=
new
PKCS8EncodedKeySpec
(
Base64
.
getDecoder
().
decode
(
key
.
getBytes
(
ConfigureEncryptAndDecrypt
.
CHAR_ENCODING
)));
KeyFactory
keyFactory
=
KeyFactory
.
getInstance
(
"RSA"
);
PrivateKey
privateKey
=
keyFactory
.
generatePrivate
(
keySpec
);
return
privateKey
;
}
/**
* 得到公钥
* @param key 密钥字符串(经过base64编码)
* @throws Exception
*/
private
static
PublicKey
getPublicKey
(
String
key
)
throws
Exception
{
X509EncodedKeySpec
keySpec
=
new
X509EncodedKeySpec
(
Base64
.
getDecoder
().
decode
(
key
.
getBytes
(
ConfigureEncryptAndDecrypt
.
CHAR_ENCODING
)));
KeyFactory
keyFactory
=
KeyFactory
.
getInstance
(
"RSA"
);
PublicKey
publicKey
=
keyFactory
.
generatePublic
(
keySpec
);
return
publicKey
;
}
public
static
void
main
(
String
[]
args
)
throws
Exception
{
KeyPairGenerator
kpg
=
KeyPairGenerator
.
getInstance
(
"RSA"
);
kpg
.
initialize
(
KEYSIZE
);
KeyPair
kp
=
kpg
.
generateKeyPair
();
String
privateKey
=
new
String
(
Base64
.
getEncoder
().
encode
(
kp
.
getPrivate
().
getEncoded
()));
String
publicKey
=
new
String
(
Base64
.
getEncoder
().
encode
(
kp
.
getPublic
().
getEncoded
()));
System
.
out
.
println
(
privateKey
);
System
.
out
.
println
(
publicKey
);
}
}
src/main/java/cn/qg/qaplatform/zdata/vo/EncryptRequest.java
0 → 100644
View file @
6a3162ac
package
cn
.
qg
.
qaplatform
.
zdata
.
vo
;
import
lombok.Data
;
import
java.io.Serializable
;
@Data
public
class
EncryptRequest
implements
Serializable
{
private
static
final
long
serialVersionUID
=
-
1660123609661052319L
;
private
String
appId
;
private
String
sign
;
private
String
nonce
;
private
String
timestamp
;
private
String
encryptKey
;
private
String
body
;
/**
* 下面是解密后的请求报文
*/
private
String
decBody
;
}
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