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
70f336e8
Commit
70f336e8
authored
Nov 18, 2020
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修改实体位置
parent
03bc58c5
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
26 additions
and
4 deletions
+26
-4
RsaHelper.java
.../src/main/java/com/yanzuoguang/util/helper/RsaHelper.java
+26
-4
No files found.
yzg-util-base/src/main/java/com/yanzuoguang/util/helper/RsaHelper.java
View file @
70f336e8
...
...
@@ -7,6 +7,7 @@ import org.slf4j.LoggerFactory;
import
org.springframework.util.Base64Utils
;
import
javax.crypto.Cipher
;
import
java.io.ByteArrayOutputStream
;
import
java.security.*
;
import
java.security.interfaces.RSAPrivateKey
;
import
java.security.interfaces.RSAPublicKey
;
...
...
@@ -27,6 +28,7 @@ public final class RsaHelper {
private
static
final
String
ALGORITHM_SIGN
=
"MD5withRSA"
;
private
static
final
int
KEYPAIR_LEN
=
1024
;
public
static
final
int
MAX_ENCRYPT_BLOCK
=
64
;
private
RsaHelper
()
{
super
();
...
...
@@ -100,10 +102,30 @@ public final class RsaHelper {
PublicKey
publicKey
=
getPublicKey
(
publicKeyStr
);
Cipher
cipher
=
Cipher
.
getInstance
(
publicKey
.
getAlgorithm
());
cipher
.
init
(
Cipher
.
ENCRYPT_MODE
,
publicKey
);
cipher
.
update
(
source
.
getBytes
(
SystemContants
.
UTF8
));
byte
[]
bytes
=
cipher
.
doFinal
();
String
target
=
encodeBase64
(
bytes
);
return
target
;
byte
[]
bytesFrom
=
source
.
getBytes
(
SystemContants
.
UTF8
);
ByteArrayOutputStream
out
=
new
ByteArrayOutputStream
();
int
inputLen
=
bytesFrom
.
length
;
int
offSet
=
0
;
byte
[]
cache
;
int
i
=
0
;
// 对数据分段加密
while
(
inputLen
-
offSet
>
0
)
{
if
(
inputLen
-
offSet
>
MAX_ENCRYPT_BLOCK
)
{
cache
=
cipher
.
doFinal
(
bytesFrom
,
offSet
,
MAX_ENCRYPT_BLOCK
);
}
else
{
cache
=
cipher
.
doFinal
(
bytesFrom
,
offSet
,
inputLen
-
offSet
);
}
out
.
write
(
cache
,
0
,
cache
.
length
);
i
++;
offSet
=
i
*
MAX_ENCRYPT_BLOCK
;
}
byte
[]
encryptedData
=
out
.
toByteArray
();
out
.
close
();
return
encodeBase64
(
encryptedData
);
}
catch
(
Exception
ex
)
{
throw
new
RuntimeException
(
ex
);
}
...
...
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