Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
jinli gu
Eladmin
Commits
162cff26
"...src/main/java/git@ustchcs.com:gujinli1118/eladmin.git" did not exist on "7b91c4405c4de9f680521d0e2db31521bddff852"
Unverified
Commit
162cff26
authored
Mar 02, 2021
by
Tsln
Committed by
GitHub
Mar 02, 2021
Browse files
增强 RSAUtils 以解决 RSA 加密/解密长度限制 (#604)
parent
a0b6e85e
Changes
1
Hide whitespace changes
Inline
Side-by-side
eladmin-common/src/main/java/me/zhengjie/utils/RsaUtils.java
View file @
162cff26
...
@@ -2,6 +2,7 @@ package me.zhengjie.utils;
...
@@ -2,6 +2,7 @@ package me.zhengjie.utils;
import
org.apache.commons.codec.binary.Base64
;
import
org.apache.commons.codec.binary.Base64
;
import
javax.crypto.Cipher
;
import
javax.crypto.Cipher
;
import
java.io.ByteArrayOutputStream
;
import
java.security.*
;
import
java.security.*
;
import
java.security.interfaces.RSAPrivateKey
;
import
java.security.interfaces.RSAPrivateKey
;
import
java.security.interfaces.RSAPublicKey
;
import
java.security.interfaces.RSAPublicKey
;
...
@@ -80,7 +81,7 @@ public class RsaUtils {
...
@@ -80,7 +81,7 @@ public class RsaUtils {
PublicKey
publicKey
=
keyFactory
.
generatePublic
(
x509EncodedKeySpec
);
PublicKey
publicKey
=
keyFactory
.
generatePublic
(
x509EncodedKeySpec
);
Cipher
cipher
=
Cipher
.
getInstance
(
"RSA"
);
Cipher
cipher
=
Cipher
.
getInstance
(
"RSA"
);
cipher
.
init
(
Cipher
.
DECRYPT_MODE
,
publicKey
);
cipher
.
init
(
Cipher
.
DECRYPT_MODE
,
publicKey
);
byte
[]
result
=
c
ipher
.
do
Final
(
Base64
.
decodeBase64
(
text
));
byte
[]
result
=
doLongerC
ipherFinal
(
cipher
,
Base64
.
decodeBase64
(
text
));
return
new
String
(
result
);
return
new
String
(
result
);
}
}
...
@@ -98,7 +99,7 @@ public class RsaUtils {
...
@@ -98,7 +99,7 @@ public class RsaUtils {
PrivateKey
privateKey
=
keyFactory
.
generatePrivate
(
pkcs8EncodedKeySpec
);
PrivateKey
privateKey
=
keyFactory
.
generatePrivate
(
pkcs8EncodedKeySpec
);
Cipher
cipher
=
Cipher
.
getInstance
(
"RSA"
);
Cipher
cipher
=
Cipher
.
getInstance
(
"RSA"
);
cipher
.
init
(
Cipher
.
ENCRYPT_MODE
,
privateKey
);
cipher
.
init
(
Cipher
.
ENCRYPT_MODE
,
privateKey
);
byte
[]
result
=
c
ipher
.
do
Final
(
text
.
getBytes
());
byte
[]
result
=
doLongerC
ipherFinal
(
cipher
,
text
.
getBytes
());
return
Base64
.
encodeBase64String
(
result
);
return
Base64
.
encodeBase64String
(
result
);
}
}
...
@@ -116,7 +117,7 @@ public class RsaUtils {
...
@@ -116,7 +117,7 @@ public class RsaUtils {
PrivateKey
privateKey
=
keyFactory
.
generatePrivate
(
pkcs8EncodedKeySpec5
);
PrivateKey
privateKey
=
keyFactory
.
generatePrivate
(
pkcs8EncodedKeySpec5
);
Cipher
cipher
=
Cipher
.
getInstance
(
"RSA"
);
Cipher
cipher
=
Cipher
.
getInstance
(
"RSA"
);
cipher
.
init
(
Cipher
.
DECRYPT_MODE
,
privateKey
);
cipher
.
init
(
Cipher
.
DECRYPT_MODE
,
privateKey
);
byte
[]
result
=
c
ipher
.
do
Final
(
Base64
.
decodeBase64
(
text
));
byte
[]
result
=
doLongerC
ipherFinal
(
cipher
,
Base64
.
decodeBase64
(
text
));
return
new
String
(
result
);
return
new
String
(
result
);
}
}
...
@@ -133,10 +134,23 @@ public class RsaUtils {
...
@@ -133,10 +134,23 @@ public class RsaUtils {
PublicKey
publicKey
=
keyFactory
.
generatePublic
(
x509EncodedKeySpec2
);
PublicKey
publicKey
=
keyFactory
.
generatePublic
(
x509EncodedKeySpec2
);
Cipher
cipher
=
Cipher
.
getInstance
(
"RSA"
);
Cipher
cipher
=
Cipher
.
getInstance
(
"RSA"
);
cipher
.
init
(
Cipher
.
ENCRYPT_MODE
,
publicKey
);
cipher
.
init
(
Cipher
.
ENCRYPT_MODE
,
publicKey
);
byte
[]
result
=
c
ipher
.
do
Final
(
text
.
getBytes
());
byte
[]
result
=
doLongerC
ipherFinal
(
cipher
,
text
.
getBytes
());
return
Base64
.
encodeBase64String
(
result
);
return
Base64
.
encodeBase64String
(
result
);
}
}
private
static
byte
[]
doLongerCipherFinal
(
Cipher
cipher
,
byte
[]
source
)
throws
Exception
{
int
offset
=
0
;
int
totalSize
=
source
.
length
;
ByteArrayOutputStream
out
=
new
ByteArrayOutputStream
();
while
(
totalSize
-
offset
>
0
)
{
int
size
=
Math
.
min
(
1024
/
8
-
11
,
totalSize
-
offset
);
out
.
write
(
cipher
.
doFinal
(
source
,
offset
,
size
));
offset
+=
size
;
}
out
.
close
();
return
out
.
toByteArray
();
}
/**
/**
* 构建RSA密钥对
* 构建RSA密钥对
*
*
...
...
Write
Preview
Supports
Markdown
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