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
03bc58c5
Commit
03bc58c5
authored
Nov 18, 2020
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修改实体位置
parent
26394ccf
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
93 additions
and
55 deletions
+93
-55
RsaHelper.java
.../src/main/java/com/yanzuoguang/util/helper/RsaHelper.java
+93
-55
No files found.
yzg-util-base/src/main/java/com/yanzuoguang/util/helper/RsaHelper.java
View file @
03bc58c5
...
...
@@ -37,18 +37,22 @@ public final class RsaHelper {
*
* @throws Exception
*/
public
static
void
generatorKeyPair
()
throws
Exception
{
KeyPairGenerator
keyPairGen
=
KeyPairGenerator
.
getInstance
(
ALGORITHM_RSA
);
keyPairGen
.
initialize
(
KEYPAIR_LEN
);
KeyPair
keyPair
=
keyPairGen
.
generateKeyPair
();
RSAPublicKey
rsaPublicKey
=
(
RSAPublicKey
)
keyPair
.
getPublic
();
byte
[]
keyBs
=
rsaPublicKey
.
getEncoded
();
String
publicKey
=
encodeBase64
(
keyBs
);
logger
.
info
(
"生成的公钥:\t{}"
,
publicKey
);
RSAPrivateKey
rsaPrivateKey
=
(
RSAPrivateKey
)
keyPair
.
getPrivate
();
keyBs
=
rsaPrivateKey
.
getEncoded
();
String
privateKey
=
encodeBase64
(
keyBs
);
logger
.
info
(
"生成的私钥:\t{}"
,
privateKey
);
public
static
void
generatorKeyPair
()
{
try
{
KeyPairGenerator
keyPairGen
=
KeyPairGenerator
.
getInstance
(
ALGORITHM_RSA
);
keyPairGen
.
initialize
(
KEYPAIR_LEN
);
KeyPair
keyPair
=
keyPairGen
.
generateKeyPair
();
RSAPublicKey
rsaPublicKey
=
(
RSAPublicKey
)
keyPair
.
getPublic
();
byte
[]
keyBs
=
rsaPublicKey
.
getEncoded
();
String
publicKey
=
encodeBase64
(
keyBs
);
logger
.
info
(
"生成的公钥:\t{}"
,
publicKey
);
RSAPrivateKey
rsaPrivateKey
=
(
RSAPrivateKey
)
keyPair
.
getPrivate
();
keyBs
=
rsaPrivateKey
.
getEncoded
();
String
privateKey
=
encodeBase64
(
keyBs
);
logger
.
info
(
"生成的私钥:\t{}"
,
privateKey
);
}
catch
(
Exception
ex
)
{
throw
new
RuntimeException
(
ex
);
}
}
/**
...
...
@@ -58,9 +62,13 @@ public final class RsaHelper {
* @throws Exception
*/
private
static
PublicKey
getPublicKey
(
String
publicKey
)
throws
Exception
{
X509EncodedKeySpec
publicKeySpec
=
new
X509EncodedKeySpec
(
decodeBase64
(
publicKey
));
KeyFactory
keyFactory
=
KeyFactory
.
getInstance
(
ALGORITHM_RSA
);
return
keyFactory
.
generatePublic
(
publicKeySpec
);
try
{
X509EncodedKeySpec
publicKeySpec
=
new
X509EncodedKeySpec
(
decodeBase64
(
publicKey
));
KeyFactory
keyFactory
=
KeyFactory
.
getInstance
(
ALGORITHM_RSA
);
return
keyFactory
.
generatePublic
(
publicKeySpec
);
}
catch
(
Exception
ex
)
{
throw
new
RuntimeException
(
ex
);
}
}
/**
...
...
@@ -69,10 +77,14 @@ public final class RsaHelper {
* @return
* @throws Exception
*/
private
static
PrivateKey
getPrivateKey
(
String
privateKey
)
throws
Exception
{
PKCS8EncodedKeySpec
privateKeySpec
=
new
PKCS8EncodedKeySpec
(
decodeBase64
(
privateKey
));
KeyFactory
keyFactory
=
KeyFactory
.
getInstance
(
ALGORITHM_RSA
);
return
keyFactory
.
generatePrivate
(
privateKeySpec
);
private
static
PrivateKey
getPrivateKey
(
String
privateKey
)
{
try
{
PKCS8EncodedKeySpec
privateKeySpec
=
new
PKCS8EncodedKeySpec
(
decodeBase64
(
privateKey
));
KeyFactory
keyFactory
=
KeyFactory
.
getInstance
(
ALGORITHM_RSA
);
return
keyFactory
.
generatePrivate
(
privateKeySpec
);
}
catch
(
Exception
ex
)
{
throw
new
RuntimeException
(
ex
);
}
}
/**
...
...
@@ -89,7 +101,8 @@ public final class RsaHelper {
Cipher
cipher
=
Cipher
.
getInstance
(
publicKey
.
getAlgorithm
());
cipher
.
init
(
Cipher
.
ENCRYPT_MODE
,
publicKey
);
cipher
.
update
(
source
.
getBytes
(
SystemContants
.
UTF8
));
String
target
=
encodeBase64
(
cipher
.
doFinal
());
byte
[]
bytes
=
cipher
.
doFinal
();
String
target
=
encodeBase64
(
bytes
);
return
target
;
}
catch
(
Exception
ex
)
{
throw
new
RuntimeException
(
ex
);
...
...
@@ -102,12 +115,17 @@ public final class RsaHelper {
* @param target
* @throws Exception
*/
public
static
String
decryptionByPublicKey
(
String
target
,
String
publicKeyStr
)
throws
Exception
{
PublicKey
publicKey
=
getPublicKey
(
publicKeyStr
);
Cipher
cipher
=
Cipher
.
getInstance
(
publicKey
.
getAlgorithm
());
cipher
.
init
(
Cipher
.
DECRYPT_MODE
,
publicKey
);
cipher
.
update
(
decodeBase64
(
target
));
return
new
String
(
cipher
.
doFinal
(),
SystemContants
.
UTF8
);
public
static
String
decryptionByPublicKey
(
String
target
,
String
publicKeyStr
)
{
try
{
byte
[]
bytes
=
decodeBase64
(
target
);
PublicKey
publicKey
=
getPublicKey
(
publicKeyStr
);
Cipher
cipher
=
Cipher
.
getInstance
(
publicKey
.
getAlgorithm
());
cipher
.
init
(
Cipher
.
DECRYPT_MODE
,
publicKey
);
cipher
.
update
(
bytes
);
return
new
String
(
cipher
.
doFinal
(),
SystemContants
.
UTF8
);
}
catch
(
Exception
ex
)
{
throw
new
RuntimeException
(
ex
);
}
}
/**
...
...
@@ -116,15 +134,19 @@ public final class RsaHelper {
* @return
* @throws Exception
*/
public
static
void
verifyByPublicKey
(
String
target
,
String
sign
,
String
publicKeyStr
)
throws
Exception
{
PublicKey
publicKey
=
getPublicKey
(
publicKeyStr
);
Signature
signature
=
Signature
.
getInstance
(
ALGORITHM_SIGN
);
signature
.
initVerify
(
publicKey
);
signature
.
update
(
target
.
getBytes
(
SystemContants
.
UTF8
));
if
(
signature
.
verify
(
decodeBase64
(
sign
)))
{
logger
.
info
(
"sign true"
);
}
else
{
logger
.
info
(
"sign false"
);
public
static
void
verifyByPublicKey
(
String
target
,
String
sign
,
String
publicKeyStr
)
{
try
{
PublicKey
publicKey
=
getPublicKey
(
publicKeyStr
);
Signature
signature
=
Signature
.
getInstance
(
ALGORITHM_SIGN
);
signature
.
initVerify
(
publicKey
);
signature
.
update
(
target
.
getBytes
(
SystemContants
.
UTF8
));
if
(
signature
.
verify
(
decodeBase64
(
sign
)))
{
logger
.
info
(
"sign true"
);
}
else
{
logger
.
info
(
"sign false"
);
}
}
catch
(
Exception
ex
)
{
throw
new
RuntimeException
(
ex
);
}
}
...
...
@@ -135,13 +157,17 @@ public final class RsaHelper {
* @return
* @throws Exception
*/
public
static
String
encryptionByPrivateKey
(
String
source
,
String
privateKeyStr
)
throws
Exception
{
PrivateKey
privateKey
=
getPrivateKey
(
privateKeyStr
);
Cipher
cipher
=
Cipher
.
getInstance
(
privateKey
.
getAlgorithm
());
cipher
.
init
(
Cipher
.
ENCRYPT_MODE
,
privateKey
);
cipher
.
update
(
source
.
getBytes
(
SystemContants
.
UTF8
));
String
target
=
encodeBase64
(
cipher
.
doFinal
());
return
target
;
public
static
String
encryptionByPrivateKey
(
String
source
,
String
privateKeyStr
)
{
try
{
PrivateKey
privateKey
=
getPrivateKey
(
privateKeyStr
);
Cipher
cipher
=
Cipher
.
getInstance
(
privateKey
.
getAlgorithm
());
cipher
.
init
(
Cipher
.
ENCRYPT_MODE
,
privateKey
);
cipher
.
update
(
source
.
getBytes
(
SystemContants
.
UTF8
));
String
target
=
encodeBase64
(
cipher
.
doFinal
());
return
target
;
}
catch
(
Exception
ex
)
{
throw
new
RuntimeException
(
ex
);
}
}
/**
...
...
@@ -169,13 +195,17 @@ public final class RsaHelper {
* @return
* @throws Exception
*/
public
static
String
signByPrivateKey
(
String
target
,
String
privateKeyStr
)
throws
Exception
{
PrivateKey
privateKey
=
getPrivateKey
(
privateKeyStr
);
Signature
signature
=
Signature
.
getInstance
(
ALGORITHM_SIGN
);
signature
.
initSign
(
privateKey
);
signature
.
update
(
target
.
getBytes
(
SystemContants
.
UTF8
));
String
sign
=
encodeBase64
(
signature
.
sign
());
return
sign
;
public
static
String
signByPrivateKey
(
String
target
,
String
privateKeyStr
)
{
try
{
PrivateKey
privateKey
=
getPrivateKey
(
privateKeyStr
);
Signature
signature
=
Signature
.
getInstance
(
ALGORITHM_SIGN
);
signature
.
initSign
(
privateKey
);
signature
.
update
(
target
.
getBytes
(
SystemContants
.
UTF8
));
String
sign
=
encodeBase64
(
signature
.
sign
());
return
sign
;
}
catch
(
Exception
ex
)
{
throw
new
RuntimeException
(
ex
);
}
}
/**
...
...
@@ -185,9 +215,13 @@ public final class RsaHelper {
* @return
* @throws Exception
*/
private
static
String
encodeBase64
(
byte
[]
source
)
throws
Exception
{
byte
[]
to
=
Base64Utils
.
encode
(
source
);
return
new
String
(
to
,
SystemContants
.
UTF8
);
private
static
String
encodeBase64
(
byte
[]
source
)
{
try
{
byte
[]
to
=
Base64Utils
.
encode
(
source
);
return
new
String
(
to
,
SystemContants
.
UTF8
);
}
catch
(
Exception
ex
)
{
throw
new
RuntimeException
(
ex
);
}
}
/**
...
...
@@ -197,8 +231,12 @@ public final class RsaHelper {
* @return
* @throws Exception
*/
private
static
byte
[]
decodeBase64
(
String
target
)
throws
Exception
{
byte
[]
from
=
target
.
getBytes
(
SystemContants
.
UTF8
);
return
Base64Utils
.
decode
(
from
);
private
static
byte
[]
decodeBase64
(
String
target
)
{
try
{
byte
[]
from
=
target
.
getBytes
(
SystemContants
.
UTF8
);
return
Base64Utils
.
decode
(
from
);
}
catch
(
Exception
ex
)
{
throw
new
RuntimeException
(
ex
);
}
}
}
\ No newline at end of file
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