Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
open-source
/
rpc-openplatform
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 96ee03a3
authored
Dec 19, 2020
by
杨林龙
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
优化RSA公秘钥生成和使用方式 yangll
1 parent
76c1aa41
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
158 additions
and
334 deletions
rpc-openplatform-api/src/main/java/cn/com/glsx/openplatform/service/CustomerMgrService.java
rpc-openplatform-container/src/main/java/cn/com/glsx/openplatform/mapper/PlatformCustomerMapper.java
rpc-openplatform-container/src/main/java/cn/com/glsx/openplatform/service/CustomerMgrServiceImpl.java
rpc-openplatform-container/src/main/java/cn/com/glsx/openplatform/service/IceOpenApiServiceImpl.java
rpc-openplatform-container/src/main/java/cn/com/glsx/openplatform/util/RSAUtil.java
rpc-openplatform-container/src/main/java/cn/com/glsx/openplatform/util/RSAUtils.java
rpc-openplatform-container/src/main/resources/META-INF/orm/PlatformCustomerMapper.xml
rpc-openplatform-container/src/test/java/cn/com/glsx/openplatform/service/CustomerMgrServiceImplTest.java
rpc-openplatform-api/src/main/java/cn/com/glsx/openplatform/service/CustomerMgrService.java
View file @
96ee03a
...
@@ -4,6 +4,7 @@ import cn.com.glsx.openplatform.model.PlatformCustomer;
...
@@ -4,6 +4,7 @@ import cn.com.glsx.openplatform.model.PlatformCustomer;
import
cn.com.glsx.openplatform.vo.R
;
import
cn.com.glsx.openplatform.vo.R
;
import
cn.hutool.db.Page
;
import
cn.hutool.db.Page
;
import
java.security.NoSuchAlgorithmException
;
import
java.util.List
;
import
java.util.List
;
public
interface
CustomerMgrService
{
public
interface
CustomerMgrService
{
...
@@ -15,7 +16,7 @@ public interface CustomerMgrService {
...
@@ -15,7 +16,7 @@ public interface CustomerMgrService {
int
getCustomerCount
();
int
getCustomerCount
();
int
register
(
PlatformCustomer
customer
);
int
register
(
PlatformCustomer
customer
)
throws
NoSuchAlgorithmException
;
PlatformCustomer
login
(
PlatformCustomer
customer
);
PlatformCustomer
login
(
PlatformCustomer
customer
);
...
...
rpc-openplatform-container/src/main/java/cn/com/glsx/openplatform/mapper/PlatformCustomerMapper.java
View file @
96ee03a
...
@@ -28,4 +28,6 @@ public interface PlatformCustomerMapper extends OreMapper<PlatformCustomer> {
...
@@ -28,4 +28,6 @@ public interface PlatformCustomerMapper extends OreMapper<PlatformCustomer> {
List
<
String
>
getAddressOptionList
(
@Param
(
"id"
)
int
id
);
List
<
String
>
getAddressOptionList
(
@Param
(
"id"
)
int
id
);
String
getAddressStr
(
@Param
(
"id"
)
Integer
id
);
String
getAddressStr
(
@Param
(
"id"
)
Integer
id
);
String
getPrivateSecret
(
@Param
(
"appId"
)
int
appId
);
}
}
\ No newline at end of file
\ No newline at end of file
rpc-openplatform-container/src/main/java/cn/com/glsx/openplatform/service/CustomerMgrServiceImpl.java
View file @
96ee03a
...
@@ -2,6 +2,7 @@ package cn.com.glsx.openplatform.service;
...
@@ -2,6 +2,7 @@ package cn.com.glsx.openplatform.service;
import
cn.com.glsx.openplatform.mapper.PlatformCustomerMapper
;
import
cn.com.glsx.openplatform.mapper.PlatformCustomerMapper
;
import
cn.com.glsx.openplatform.model.PlatformCustomer
;
import
cn.com.glsx.openplatform.model.PlatformCustomer
;
import
cn.com.glsx.openplatform.util.RSAUtils
;
import
cn.com.glsx.openplatform.vo.R
;
import
cn.com.glsx.openplatform.vo.R
;
import
cn.hutool.core.util.RandomUtil
;
import
cn.hutool.core.util.RandomUtil
;
import
cn.hutool.db.Page
;
import
cn.hutool.db.Page
;
...
@@ -13,9 +14,11 @@ import org.slf4j.LoggerFactory;
...
@@ -13,9 +14,11 @@ import org.slf4j.LoggerFactory;
import
sun.misc.BASE64Encoder
;
import
sun.misc.BASE64Encoder
;
import
javax.annotation.Resource
;
import
javax.annotation.Resource
;
import
java.security.NoSuchAlgorithmException
;
import
java.security.PrivateKey
;
import
java.security.PrivateKey
;
import
java.security.PublicKey
;
import
java.security.PublicKey
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Map
;
@Service
@Service
public
class
CustomerMgrServiceImpl
implements
CustomerMgrService
{
public
class
CustomerMgrServiceImpl
implements
CustomerMgrService
{
...
@@ -57,15 +60,20 @@ public class CustomerMgrServiceImpl implements CustomerMgrService {
...
@@ -57,15 +60,20 @@ public class CustomerMgrServiceImpl implements CustomerMgrService {
}
}
@Override
@Override
public
int
register
(
PlatformCustomer
customer
)
{
public
int
register
(
PlatformCustomer
customer
)
throws
NoSuchAlgorithmException
{
String
modulus
=
RandomUtil
.
randomNumbers
(
512
);
// String modulus = RandomUtil.randomNumbers(512);
String
exponent
=
RandomUtil
.
randomNumbers
(
64
);
// String exponent = RandomUtil.randomNumbers(64);
//
// PrivateKey privateKey = RsaUtil.generatePrivateKey(modulus, exponent);
// PublicKey publicKey = RsaUtil.generatePublicKey(modulus, exponent);
// BASE64Encoder enc = new BASE64Encoder();
// customer.setPrivateKey(enc.encode(privateKey.getEncoded()));
// customer.setPublicKey(enc.encode(publicKey.getEncoded()));
PrivateKey
privateKey
=
RsaUtil
.
generatePrivateKey
(
modulus
,
exponent
);
Map
<
Integer
,
String
>
map
=
RSAUtils
.
genKeyPair
();
PublicKey
publicKey
=
RsaUtil
.
generatePublicKey
(
modulus
,
exponent
);
BASE64Encoder
enc
=
new
BASE64Encoder
();
customer
.
setPublicKey
(
map
.
get
(
0
));
customer
.
setPrivateKey
(
enc
.
encode
(
privateKey
.
getEncoded
()));
customer
.
setPrivateKey
(
map
.
get
(
1
));
customer
.
setPublicKey
(
enc
.
encode
(
publicKey
.
getEncoded
()));
String
province
=
customerMapper
.
getAddressStr
(
customer
.
getProvince
());
String
province
=
customerMapper
.
getAddressStr
(
customer
.
getProvince
());
String
city
=
customerMapper
.
getAddressStr
(
customer
.
getCity
());
String
city
=
customerMapper
.
getAddressStr
(
customer
.
getCity
());
...
...
rpc-openplatform-container/src/main/java/cn/com/glsx/openplatform/service/IceOpenApiServiceImpl.java
View file @
96ee03a
...
@@ -8,8 +8,8 @@ import cn.hutool.db.Page;
...
@@ -8,8 +8,8 @@ import cn.hutool.db.Page;
import
com.alibaba.dubbo.config.annotation.Service
;
import
com.alibaba.dubbo.config.annotation.Service
;
import
com.github.pagehelper.PageHelper
;
import
com.github.pagehelper.PageHelper
;
import
com.github.pagehelper.PageInfo
;
import
com.github.pagehelper.PageInfo
;
import
lombok.Data
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
javax.annotation.Resource
;
import
javax.annotation.Resource
;
import
java.util.Date
;
import
java.util.Date
;
import
java.util.HashMap
;
import
java.util.HashMap
;
...
@@ -20,7 +20,6 @@ import java.util.Map;
...
@@ -20,7 +20,6 @@ import java.util.Map;
* 开放平台对外接口API相关服务
* 开放平台对外接口API相关服务
*/
*/
@Service
@Service
@Data
@Slf4j
@Slf4j
public
class
IceOpenApiServiceImpl
implements
IceOpenApiService
{
public
class
IceOpenApiServiceImpl
implements
IceOpenApiService
{
@Resource
@Resource
...
@@ -34,6 +33,7 @@ public class IceOpenApiServiceImpl implements IceOpenApiService {
...
@@ -34,6 +33,7 @@ public class IceOpenApiServiceImpl implements IceOpenApiService {
/**
/**
* 获取商户下的数据概况信息
* 获取商户下的数据概况信息
*
* @param customerId 客户id
* @param customerId 客户id
* @return Map 数据信息
* @return Map 数据信息
*/
*/
...
@@ -68,22 +68,22 @@ public class IceOpenApiServiceImpl implements IceOpenApiService {
...
@@ -68,22 +68,22 @@ public class IceOpenApiServiceImpl implements IceOpenApiService {
/**
/**
* 获取私钥
* 获取私钥
*
* @param appId 用户id
* @param appId 用户id
* @return String 私钥
* @return String 私钥
*/
*/
@Override
@Override
public
String
getPrivateSecret
(
int
appId
)
{
public
String
getPrivateSecret
(
int
appId
)
{
log
.
info
(
"rpc==getPrivateSecret开始==appid:"
+
appId
);
log
.
info
(
"rpc==getPrivateSecret开始==appid:"
+
appId
);
//查询客户信息
//查询客户信息
PlatformCustomer
platformCustomer
=
getPlatformCustomer
(
appId
);
String
privateKey
=
customerMapper
.
getPrivateSecret
(
appId
);
//返回客户私钥
log
.
info
(
"rpc==getPrivateSecret开始==appid:"
+
privateKey
);
String
privateKey
=
platformCustomer
.
getPrivateKey
();
log
.
info
(
"rpc==getPrivateSecret开始==appid:"
+
privateKey
);
return
privateKey
;
return
privateKey
;
}
}
/**
/**
* 查询客户信息
* 查询客户信息
*
* @param appId 客户id
* @param appId 客户id
* @return PlatformCustomer 客户信息实体类
* @return PlatformCustomer 客户信息实体类
*/
*/
...
@@ -95,34 +95,35 @@ public class IceOpenApiServiceImpl implements IceOpenApiService {
...
@@ -95,34 +95,35 @@ public class IceOpenApiServiceImpl implements IceOpenApiService {
/**
/**
* 获取客户数据概况信息
* 获取客户数据概况信息
*
* @param appId 用户id
* @param appId 用户id
* @return Map 用户信息
* @return Map 用户信息
*/
*/
@Override
@Override
public
Map
<
String
,
Object
>
getCustomerInfo
(
int
appId
)
{
public
Map
<
String
,
Object
>
getCustomerInfo
(
int
appId
)
{
log
.
info
(
"rpc==getCustomerInfo开始==appid:"
+
appId
);
log
.
info
(
"rpc==getCustomerInfo开始==appid:"
+
appId
);
//查询客户信息
//查询客户信息
PlatformCustomer
platformCustomer
=
getPlatformCustomer
(
appId
);
PlatformCustomer
platformCustomer
=
getPlatformCustomer
(
appId
);
//封装返回数据
//封装返回数据
Map
<
String
,
Object
>
res
=
new
HashMap
<>();
Map
<
String
,
Object
>
res
=
new
HashMap
<>();
//客户ID
//客户ID
res
.
put
(
"appid"
,
appId
);
res
.
put
(
"appid"
,
appId
);
//客户名称
//客户名称
res
.
put
(
"customer_name"
,
platformCustomer
.
getCustomerName
());
res
.
put
(
"customer_name"
,
platformCustomer
.
getCustomerName
());
//联系电话
//联系电话
res
.
put
(
"contact_number"
,
platformCustomer
.
getContactNumber
());
res
.
put
(
"contact_number"
,
platformCustomer
.
getContactNumber
());
//渠道号
//渠道号
res
.
put
(
"channel_number"
,
platformCustomer
.
getChannelNumber
());
res
.
put
(
"channel_number"
,
platformCustomer
.
getChannelNumber
());
log
.
info
(
"rpc==getCustomerInfo结果==appid:"
+
res
);
log
.
info
(
"rpc==getCustomerInfo结果==appid:"
+
res
);
return
res
;
return
res
;
}
}
@Override
@Override
public
Map
<
String
,
Object
>
getDeviceList
(
Integer
appid
,
Page
page
,
Date
start_time
,
Date
end_time
)
{
public
Map
<
String
,
Object
>
getDeviceList
(
Integer
appid
,
Page
page
,
Date
start_time
,
Date
end_time
)
{
log
.
info
(
"rpc==getDeviceList开始==appid:"
+
appid
,
"==page:"
+
page
+
"==start_time:"
+
start_time
,
"==end_time:"
+
start_time
);
log
.
info
(
"rpc==getDeviceList开始==appid:"
+
appid
,
"==page:"
+
page
+
"==start_time:"
+
start_time
,
"==end_time:"
+
start_time
);
//分页查询
//分页查询
PageHelper
.
startPage
(
page
.
getPageNumber
(),
page
.
getPageSize
(),
true
);
PageHelper
.
startPage
(
page
.
getPageNumber
(),
page
.
getPageSize
(),
true
);
List
<
Map
<
String
,
Object
>>
deviceList
=
deviceMapper
.
getDeviceByTimeRangeAndCustID
(
start_time
,
end_time
,
appid
+
""
);
List
<
Map
<
String
,
Object
>>
deviceList
=
deviceMapper
.
getDeviceByTimeRangeAndCustID
(
start_time
,
end_time
,
appid
+
""
);
//分页结果
//分页结果
PageInfo
pageInfo
=
new
PageInfo
<>(
deviceList
);
PageInfo
pageInfo
=
new
PageInfo
<>(
deviceList
);
...
@@ -130,13 +131,13 @@ public class IceOpenApiServiceImpl implements IceOpenApiService {
...
@@ -130,13 +131,13 @@ public class IceOpenApiServiceImpl implements IceOpenApiService {
HashMap
<
String
,
Object
>
hashMap
=
new
HashMap
<>();
HashMap
<
String
,
Object
>
hashMap
=
new
HashMap
<>();
//是否有更多用户数据
//是否有更多用户数据
boolean
hasMore
=
page
.
getPageNumber
()
<
pageInfo
.
getPages
();
boolean
hasMore
=
page
.
getPageNumber
()
<
pageInfo
.
getPages
();
hashMap
.
put
(
"has_more"
,
hasMore
);
hashMap
.
put
(
"has_more"
,
hasMore
);
//显示该appid下所有的用户数
//显示该appid下所有的用户数
int
total
=
userMapper
.
getUserCountByCustomerId
(
appid
);
int
total
=
userMapper
.
getUserCountByCustomerId
(
appid
);
hashMap
.
put
(
"total"
,
total
);
hashMap
.
put
(
"total"
,
total
);
//设备数据
//设备数据
hashMap
.
put
(
"list"
,
pageInfo
.
getList
());
hashMap
.
put
(
"list"
,
pageInfo
.
getList
());
log
.
info
(
"rpc==getDeviceList结果=="
+
hashMap
);
log
.
info
(
"rpc==getDeviceList结果=="
+
hashMap
);
return
hashMap
;
return
hashMap
;
}
}
}
}
rpc-openplatform-container/src/main/java/cn/com/glsx/openplatform/util/RSAUtil.java
deleted
100644 → 0
View file @
76c1aa4
package
cn
.
com
.
glsx
.
openplatform
.
util
;
import
sun.misc.BASE64Decoder
;
import
sun.misc.BASE64Encoder
;
import
javax.crypto.BadPaddingException
;
import
javax.crypto.Cipher
;
import
javax.crypto.IllegalBlockSizeException
;
import
javax.crypto.NoSuchPaddingException
;
import
java.io.*
;
import
java.security.*
;
import
java.security.spec.PKCS8EncodedKeySpec
;
import
java.security.spec.X509EncodedKeySpec
;
import
java.util.HashMap
;
import
java.util.Map
;
/**
* RSA算法,实现数据的加密解密。
*/
public
class
RSAUtil
{
private
static
Cipher
cipher
;
static
{
try
{
cipher
=
Cipher
.
getInstance
(
"RSA"
);
}
catch
(
NoSuchAlgorithmException
e
)
{
e
.
printStackTrace
();
}
catch
(
NoSuchPaddingException
e
)
{
e
.
printStackTrace
();
}
}
/**
* 生成密钥对
*
* @param filePath 生成密钥的路径
* @return
*/
public
static
Map
<
String
,
String
>
generateKeyPair
(
String
filePath
)
{
try
{
KeyPairGenerator
keyPairGen
=
KeyPairGenerator
.
getInstance
(
"RSA"
);
// 密钥位数
keyPairGen
.
initialize
(
2048
);
// 密钥对
KeyPair
keyPair
=
keyPairGen
.
generateKeyPair
();
// 公钥
PublicKey
publicKey
=
keyPair
.
getPublic
();
// 私钥
PrivateKey
privateKey
=
keyPair
.
getPrivate
();
//得到公钥字符串
String
publicKeyString
=
getKeyString
(
publicKey
);
//得到私钥字符串
String
privateKeyString
=
getKeyString
(
privateKey
);
//将密钥对写入到文件
FileWriter
pubfw
=
new
FileWriter
(
filePath
+
"/publicKey.keystore"
);
FileWriter
prifw
=
new
FileWriter
(
filePath
+
"/privateKey.keystore"
);
BufferedWriter
pubbw
=
new
BufferedWriter
(
pubfw
);
BufferedWriter
pribw
=
new
BufferedWriter
(
prifw
);
pubbw
.
write
(
publicKeyString
);
pribw
.
write
(
privateKeyString
);
pubbw
.
flush
();
pubbw
.
close
();
pubfw
.
close
();
pribw
.
flush
();
pribw
.
close
();
prifw
.
close
();
//将生成的密钥对返回
Map
<
String
,
String
>
map
=
new
HashMap
<
String
,
String
>();
map
.
put
(
"publicKey"
,
publicKeyString
);
map
.
put
(
"privateKey"
,
privateKeyString
);
return
map
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
null
;
}
/**
* 得到公钥
*
* @param key 密钥字符串(经过base64编码)
* @throws Exception
*/
public
static
PublicKey
getPublicKey
(
String
key
)
throws
Exception
{
byte
[]
keyBytes
;
keyBytes
=
(
new
BASE64Decoder
()).
decodeBuffer
(
key
);
X509EncodedKeySpec
keySpec
=
new
X509EncodedKeySpec
(
keyBytes
);
KeyFactory
keyFactory
=
KeyFactory
.
getInstance
(
"RSA"
);
PublicKey
publicKey
=
keyFactory
.
generatePublic
(
keySpec
);
return
publicKey
;
}
/**
* 得到私钥
*
* @param key 密钥字符串(经过base64编码)
* @throws Exception
*/
public
static
PrivateKey
getPrivateKey
(
String
key
)
throws
Exception
{
byte
[]
keyBytes
;
keyBytes
=
(
new
BASE64Decoder
()).
decodeBuffer
(
key
);
PKCS8EncodedKeySpec
keySpec
=
new
PKCS8EncodedKeySpec
(
keyBytes
);
KeyFactory
keyFactory
=
KeyFactory
.
getInstance
(
"RSA"
);
PrivateKey
privateKey
=
keyFactory
.
generatePrivate
(
keySpec
);
return
privateKey
;
}
/**
* 得到密钥字符串(经过base64编码)
*
* @return
*/
public
static
String
getKeyString
(
Key
key
)
throws
Exception
{
byte
[]
keyBytes
=
key
.
getEncoded
();
String
s
=
(
new
BASE64Encoder
()).
encode
(
keyBytes
);
return
s
;
}
/**
* 使用公钥对明文进行加密,返回BASE64编码的字符串
*
* @param publicKey
* @param plainText
* @return
*/
public
static
String
encrypt
(
PublicKey
publicKey
,
String
plainText
)
{
try
{
cipher
.
init
(
Cipher
.
ENCRYPT_MODE
,
publicKey
);
byte
[]
enBytes
=
cipher
.
doFinal
(
plainText
.
getBytes
());
return
(
new
BASE64Encoder
()).
encode
(
enBytes
);
}
catch
(
InvalidKeyException
e
)
{
e
.
printStackTrace
();
}
catch
(
IllegalBlockSizeException
e
)
{
e
.
printStackTrace
();
}
catch
(
BadPaddingException
e
)
{
e
.
printStackTrace
();
}
return
null
;
}
/**
* 使用keystore对明文进行加密
*
* @param publicKeystore 公钥文件路径
* @param plainText 明文
* @return
*/
public
static
String
fileEncrypt
(
String
publicKeystore
,
String
plainText
)
{
try
{
FileReader
fr
=
new
FileReader
(
publicKeystore
);
BufferedReader
br
=
new
BufferedReader
(
fr
);
String
publicKeyString
=
""
;
String
str
;
while
((
str
=
br
.
readLine
())
!=
null
)
{
publicKeyString
+=
str
;
}
br
.
close
();
fr
.
close
();
cipher
.
init
(
Cipher
.
ENCRYPT_MODE
,
getPublicKey
(
publicKeyString
));
byte
[]
enBytes
=
cipher
.
doFinal
(
plainText
.
getBytes
());
return
(
new
BASE64Encoder
()).
encode
(
enBytes
);
}
catch
(
InvalidKeyException
e
)
{
e
.
printStackTrace
();
}
catch
(
IllegalBlockSizeException
e
)
{
e
.
printStackTrace
();
}
catch
(
BadPaddingException
e
)
{
e
.
printStackTrace
();
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
null
;
}
/**
* 使用公钥对明文进行加密
*
* @param publicKey 公钥
* @param plainText 明文
* @return
*/
public
static
String
encrypt
(
String
publicKey
,
String
plainText
)
{
try
{
cipher
.
init
(
Cipher
.
ENCRYPT_MODE
,
getPublicKey
(
publicKey
));
byte
[]
enBytes
=
cipher
.
doFinal
(
plainText
.
getBytes
());
return
(
new
BASE64Encoder
()).
encode
(
enBytes
);
}
catch
(
InvalidKeyException
e
)
{
e
.
printStackTrace
();
}
catch
(
IllegalBlockSizeException
e
)
{
e
.
printStackTrace
();
}
catch
(
BadPaddingException
e
)
{
e
.
printStackTrace
();
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
null
;
}
/**
* 使用私钥对明文密文进行解密
*
* @param privateKey
* @param enStr
* @return
*/
public
static
String
decrypt
(
PrivateKey
privateKey
,
String
enStr
)
{
try
{
cipher
.
init
(
Cipher
.
DECRYPT_MODE
,
privateKey
);
byte
[]
deBytes
=
cipher
.
doFinal
((
new
BASE64Decoder
()).
decodeBuffer
(
enStr
));
return
new
String
(
deBytes
);
}
catch
(
InvalidKeyException
e
)
{
e
.
printStackTrace
();
}
catch
(
IllegalBlockSizeException
e
)
{
e
.
printStackTrace
();
}
catch
(
BadPaddingException
e
)
{
e
.
printStackTrace
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
return
null
;
}
/**
* 使用私钥对密文进行解密
*
* @param privateKey 私钥
* @param enStr 密文
* @return
*/
public
static
String
decrypt
(
String
privateKey
,
String
enStr
)
{
try
{
cipher
.
init
(
Cipher
.
DECRYPT_MODE
,
getPrivateKey
(
privateKey
));
byte
[]
deBytes
=
cipher
.
doFinal
((
new
BASE64Decoder
()).
decodeBuffer
(
enStr
));
return
new
String
(
deBytes
);
}
catch
(
InvalidKeyException
e
)
{
e
.
printStackTrace
();
}
catch
(
IllegalBlockSizeException
e
)
{
e
.
printStackTrace
();
}
catch
(
BadPaddingException
e
)
{
e
.
printStackTrace
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
null
;
}
/**
* 使用keystore对密文进行解密
*
* @param privateKeystore 私钥路径
* @param enStr 密文
* @return
*/
public
static
String
fileDecrypt
(
String
privateKeystore
,
String
enStr
)
{
try
{
FileReader
fr
=
new
FileReader
(
privateKeystore
);
BufferedReader
br
=
new
BufferedReader
(
fr
);
String
privateKeyString
=
""
;
String
str
;
while
((
str
=
br
.
readLine
())
!=
null
)
{
privateKeyString
+=
str
;
}
br
.
close
();
fr
.
close
();
cipher
.
init
(
Cipher
.
DECRYPT_MODE
,
getPrivateKey
(
privateKeyString
));
byte
[]
deBytes
=
cipher
.
doFinal
((
new
BASE64Decoder
()).
decodeBuffer
(
enStr
));
return
new
String
(
deBytes
);
}
catch
(
InvalidKeyException
e
)
{
e
.
printStackTrace
();
}
catch
(
IllegalBlockSizeException
e
)
{
e
.
printStackTrace
();
}
catch
(
BadPaddingException
e
)
{
e
.
printStackTrace
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
null
;
}
public
static
void
main
(
String
[]
args
)
{
// generateKeyPair("D:/RSA");
String
publicKey
;
String
privateKey
;
publicKey
=
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAviSuCu4Yg/WAyjp06qiaE/ioI2M/ACT9UTUVxWtM7IZlXMQZPjLn0H1x0zmJ/VLIhnBliyb06QLvtrrBFRt4jnOJR5LjoTg/g8XYdVXN6a+XFjqFvOUPgzZ7OdywOoXxiO+M7WrvT0XgqyBqCnDADpY1eucDqfIDYYOBHKbtMkh0N4ZVBcfULb1Sm+Q7ed+jUa8eXPQPhMrWvhQkIeZJh+hCIrNjXUxyfZPh1tSvqoJYArbyHZs8LnbUtjIQCx9OlR9+xJTx3L9h89I4D+hqA4CZqxUzfibsu5XgYKnoSri2OCR2FefSfYlCd8Fysp0wET/r1L141qnhoMQtrUs8jwIDAQAB"
;
privateKey
=
"MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC+JK4K7hiD9YDKOnTqqJoT+KgjYz8AJP1RNRXFa0zshmVcxBk+MufQfXHTOYn9UsiGcGWLJvTpAu+2usEVG3iOc4lHkuOhOD+Dxdh1Vc3pr5cWOoW85Q+DNns53LA6hfGI74ztau9PReCrIGoKcMAOljV65wOp8gNhg4Ecpu0ySHQ3hlUFx9QtvVKb5Dt536NRrx5c9A+Eyta+FCQh5kmH6EIis2NdTHJ9k+HW1K+qglgCtvIdmzwudtS2MhALH06VH37ElPHcv2Hz0jgP6GoDgJmrFTN+Juy7leBgqehKuLY4JHYV59J9iUJ3wXKynTARP+vUvXjWqeGgxC2tSzyPAgMBAAECggEAMhFkhtpFOFIoFJgp+zRkRgf+9jqG91nGHmEVF4P2oH2PKUs1vmwXII43r8AB9uOai9QC2Q5sBQNR7dLlTtKJ/zCrIF6sc+JkzyUEp3jtnLAw35iPaLsER6/L6OOUwARPIpi5ijbTRxOGYmlJovAnkm+5K2CzVUe13jKLh+joool/ReZk0Rsr4tVLSLmvzDA/sRwYun0x0+jl5EZSQfwsVyN9bD5rY/In/EuvH9yj5R4lPe+mimF4Os6IgTsP5LzqDTAiFx5NNioFRJ2SkcTmM0CZQeMIBuvvF2HCtJlDEfCytD7wYup3GBvar2ccOe9T3YhJdsj5bfAJHVJtamxQwQKBgQDnYReMMzqAh2HOFL8QymzOjImsrOz6NCZatq38TU1hSe9PK+C0sFGhkd788y4AuURS1Btu4i7F+hOYcj1z3L+NSPGE3yLHVjakMrrNbA9rwG/t7oU0cG7d0WWM9bcTQiCSNcUyt69BGH3dZdqee1tITzqghE7+gh9RYiVcI6/8LwKBgQDSYEuWFLMUsR/s7unSHCucuEXjwbYrvknv8Y81sjvrWktNXrJoYlbGy/7HYA6lxzchtSxhPuUSjopwQ5scgMhqf8Gxz7jsDN9ak2dErF7cWRFYfh6aKhkbEw9oG01jIX15MK0TbMafoJslDhPQF1cP9i0+ZGg+gPbASdeUVRTNoQKBgQCOjwDOLgYeiMtXCOtL8hymCmsNDCKaaiUzgRijuhEyHzamJhe13Gj/TnwAh+hRI9UX333jjNJawqDuLXz1dQ5Eg6vjPQQVo2XZNzRnOuwpbJDKHUrPK3Lzkn+qIP6ii/y7eQu+GvSM/AUYsxfGy6RLYh1yJvLw1sVrBDiWk5prmwKBgFvgrmI3XBa3XKgPl5KptupVGEDmAveLvaLLLq5WzxB0eNqrduNbv2ZHBVhxvTPtk0hnZaB65XR7SD7LZ9zE6cKJVUCg5bRB0vIt2jYFydAWHhs1yYuuwxQt+NaQxfV7VN8uwQfww7ZHYDqIsWJ6Lw3Lh+rt0xEpJZrJJRulJNbBAoGBAK1OEnfBpSB99N8gdhp+ZGLsDfwFCQ2Cd4Jpsd4hxdwbXevNuA1OiE20sHPuKqEqfOKocgTMobCwbSfnymatRydVoeUumkEc4Ja+XDgH+P1eXQLdIuRCwh0AXl+vkuOCBDMw367Zp/j6vwPlNKh9ZmOBPwhV0Syv2Z8uGkTZ6g+f"
;
System
.
err
.
println
(
"公钥加密——私钥解密"
);
String
source
=
"高可用架构对于互联网服务基本是标配。"
;
System
.
out
.
println
(
"\r加密前文字:\r\n"
+
source
);
String
aData
=
RSAUtil
.
encrypt
(
publicKey
,
source
);
System
.
out
.
println
(
"加密后文字:\r\n"
+
aData
);
String
dData
=
RSAUtil
.
decrypt
(
privateKey
,
aData
);
System
.
out
.
println
(
"解密后文字: \r\n"
+
dData
);
}
}
\ No newline at end of file
\ No newline at end of file
rpc-openplatform-container/src/main/java/cn/com/glsx/openplatform/util/RSAUtils.java
0 → 100644
View file @
96ee03a
package
cn
.
com
.
glsx
.
openplatform
.
util
;
import
org.apache.commons.codec.binary.Base64
;
import
org.springframework.stereotype.Component
;
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
;
import
java.util.HashMap
;
import
java.util.Map
;
/**
* @author fengzhi
* @version V1.0
* @Description: RSA工具类
*/
@Component
public
class
RSAUtils
{
private
static
Map
<
Integer
,
String
>
keyMap
=
new
HashMap
<
Integer
,
String
>();
//用于封装随机产生的公钥与私钥
public
static
void
main
(
String
[]
args
)
throws
Exception
{
//生成公钥和私钥
genKeyPair
();
//加密字符串
String
message
=
"appId4grantType1time123456"
;
System
.
out
.
println
(
"随机生成的公钥为:"
+
keyMap
.
get
(
0
));
System
.
out
.
println
(
"随机生成的公钥为:"
+
keyMap
.
get
(
0
).
length
());
System
.
out
.
println
(
"随机生成的私钥为:"
+
keyMap
.
get
(
1
));
System
.
out
.
println
(
"随机生成的私钥为:"
+
keyMap
.
get
(
1
).
length
());
//String messageEn = encrypt(message, OpenAPIConstant.TEST_PUBLIC_KEY);
String
messageEn
=
encrypt
(
message
,
keyMap
.
get
(
0
));
System
.
out
.
println
(
message
+
"\t加密后的字符串为:"
+
messageEn
);
String
messageDe
=
decrypt
(
messageEn
,
keyMap
.
get
(
1
));
System
.
out
.
println
(
"还原后的字符串为:"
+
messageDe
);
}
/**
* 随机生成密钥对
*
* @throws NoSuchAlgorithmException
*/
public
static
Map
<
Integer
,
String
>
genKeyPair
()
throws
NoSuchAlgorithmException
{
// KeyPairGenerator类用于生成公钥和私钥对,基于RSA算法生成对象
KeyPairGenerator
keyPairGen
=
KeyPairGenerator
.
getInstance
(
"RSA"
);
// 初始化密钥对生成器,密钥大小为96-1024位
keyPairGen
.
initialize
(
512
,
new
SecureRandom
());
// 生成一个密钥对,保存在keyPair中
KeyPair
keyPair
=
keyPairGen
.
generateKeyPair
();
RSAPrivateKey
privateKey
=
(
RSAPrivateKey
)
keyPair
.
getPrivate
();
// 得到私钥
RSAPublicKey
publicKey
=
(
RSAPublicKey
)
keyPair
.
getPublic
();
// 得到公钥
String
publicKeyString
=
new
String
(
Base64
.
encodeBase64
(
publicKey
.
getEncoded
()));
// 得到私钥字符串
String
privateKeyString
=
new
String
(
Base64
.
encodeBase64
((
privateKey
.
getEncoded
())));
// 将公钥和私钥保存到Map
keyMap
.
put
(
0
,
publicKeyString
);
//0表示公钥
keyMap
.
put
(
1
,
privateKeyString
);
//1表示私钥
return
keyMap
;
}
/**
* RSA公钥加密
*
* @param str 加密字符串
* @param publicKey 公钥
* @return 密文
* @throws Exception 加密过程中的异常信息
*/
public
static
String
encrypt
(
String
str
,
String
publicKey
)
throws
Exception
{
//base64编码的公钥
byte
[]
decoded
=
Base64
.
decodeBase64
(
publicKey
);
RSAPublicKey
pubKey
=
(
RSAPublicKey
)
KeyFactory
.
getInstance
(
"RSA"
).
generatePublic
(
new
X509EncodedKeySpec
(
decoded
));
//RSA加密
Cipher
cipher
=
Cipher
.
getInstance
(
"RSA"
);
cipher
.
init
(
Cipher
.
ENCRYPT_MODE
,
pubKey
);
String
outStr
=
Base64
.
encodeBase64String
(
cipher
.
doFinal
(
str
.
getBytes
(
"UTF-8"
)));
return
outStr
;
}
/**
* RSA私钥解密
*
* @param str 加密字符串
* @param privateKey 私钥
* @return 铭文
* @throws Exception 解密过程中的异常信息
*/
public
static
String
decrypt
(
String
str
,
String
privateKey
)
throws
Exception
{
//64位解码加密后的字符串
byte
[]
inputByte
=
Base64
.
decodeBase64
(
str
.
getBytes
(
"UTF-8"
));
//base64编码的私钥
byte
[]
decoded
=
Base64
.
decodeBase64
(
privateKey
);
RSAPrivateKey
priKey
=
(
RSAPrivateKey
)
KeyFactory
.
getInstance
(
"RSA"
).
generatePrivate
(
new
PKCS8EncodedKeySpec
(
decoded
));
//RSA解密
Cipher
cipher
=
Cipher
.
getInstance
(
"RSA"
);
cipher
.
init
(
Cipher
.
DECRYPT_MODE
,
priKey
);
String
outStr
=
new
String
(
cipher
.
doFinal
(
inputByte
));
return
outStr
;
}
}
rpc-openplatform-container/src/main/resources/META-INF/orm/PlatformCustomerMapper.xml
View file @
96ee03a
...
@@ -98,4 +98,13 @@
...
@@ -98,4 +98,13 @@
from t_sys_area
from t_sys_area
where id = #{id,jdbcType=INTEGER}
where id = #{id,jdbcType=INTEGER}
</select>
</select>
<select
id=
"getPrivateSecret"
resultType=
"java.lang.String"
>
select
private_key
from
t_platform_secretkey a
left join t_platform_customer b on a.id = b.key_id
where b.id = #{appId,jdbcType=INTEGER}
</select>
</mapper>
</mapper>
rpc-openplatform-container/src/test/java/cn/com/glsx/openplatform/service/CustomerMgrServiceImplTest.java
View file @
96ee03a
...
@@ -6,11 +6,11 @@ import cn.hutool.core.util.RandomUtil;
...
@@ -6,11 +6,11 @@ import cn.hutool.core.util.RandomUtil;
import
net.dreamlu.mica.core.utils.RsaUtil
;
import
net.dreamlu.mica.core.utils.RsaUtil
;
import
org.junit.Test
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner
;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner
;
import
javax.annotation.Resource
;
import
javax.annotation.Resource
;
import
java.security.NoSuchAlgorithmException
;
import
java.security.PrivateKey
;
import
java.security.PrivateKey
;
import
java.security.PublicKey
;
import
java.security.PublicKey
;
...
@@ -20,9 +20,10 @@ public class CustomerMgrServiceImplTest {
...
@@ -20,9 +20,10 @@ public class CustomerMgrServiceImplTest {
@Resource
@Resource
private
CustomerMgrService
customerMgrService
;
private
CustomerMgrService
customerMgrService
;
@Test
@Test
public
void
register
()
{
public
void
register
()
throws
NoSuchAlgorithmException
{
String
modulus
=
RandomUtil
.
randomNumbers
(
512
);
String
modulus
=
RandomUtil
.
randomNumbers
(
512
);
String
exponent
=
RandomUtil
.
randomNumbers
(
64
);
String
exponent
=
RandomUtil
.
randomNumbers
(
64
);
PlatformCustomer
customer
=
new
PlatformCustomer
();
PlatformCustomer
customer
=
new
PlatformCustomer
();
...
...
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