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
f207d003
Unverified
Commit
f207d003
authored
Apr 27, 2021
by
Tsln
Committed by
GitHub
Apr 27, 2021
Browse files
bugfix: #627 (#634)
Emmm...解密是没有长度限制的
parent
19214fd5
Changes
1
Hide whitespace changes
Inline
Side-by-side
eladmin-common/src/main/java/me/zhengjie/utils/RsaUtils.java
View file @
f207d003
...
@@ -81,7 +81,7 @@ public class RsaUtils {
...
@@ -81,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
=
doLongerCipherFinal
(
cipher
,
Base64
.
decodeBase64
(
text
));
byte
[]
result
=
doLongerCipherFinal
(
Cipher
.
DECRYPT_MODE
,
cipher
,
Base64
.
decodeBase64
(
text
));
return
new
String
(
result
);
return
new
String
(
result
);
}
}
...
@@ -99,7 +99,7 @@ public class RsaUtils {
...
@@ -99,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
=
doLongerCipherFinal
(
cipher
,
text
.
getBytes
());
byte
[]
result
=
doLongerCipherFinal
(
Cipher
.
ENCRYPT_MODE
,
cipher
,
text
.
getBytes
());
return
Base64
.
encodeBase64String
(
result
);
return
Base64
.
encodeBase64String
(
result
);
}
}
...
@@ -117,7 +117,7 @@ public class RsaUtils {
...
@@ -117,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
=
doLongerCipherFinal
(
cipher
,
Base64
.
decodeBase64
(
text
));
byte
[]
result
=
doLongerCipherFinal
(
Cipher
.
DECRYPT_MODE
,
cipher
,
Base64
.
decodeBase64
(
text
));
return
new
String
(
result
);
return
new
String
(
result
);
}
}
...
@@ -134,18 +134,22 @@ public class RsaUtils {
...
@@ -134,18 +134,22 @@ 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
=
doLongerCipherFinal
(
cipher
,
text
.
getBytes
());
byte
[]
result
=
doLongerCipherFinal
(
Cipher
.
ENCRYPT_MODE
,
cipher
,
text
.
getBytes
());
return
Base64
.
encodeBase64String
(
result
);
return
Base64
.
encodeBase64String
(
result
);
}
}
private
static
byte
[]
doLongerCipherFinal
(
Cipher
cipher
,
byte
[]
source
)
throws
Exception
{
private
static
byte
[]
doLongerCipherFinal
(
int
opMode
,
Cipher
cipher
,
byte
[]
source
)
throws
Exception
{
int
offset
=
0
;
int
totalSize
=
source
.
length
;
ByteArrayOutputStream
out
=
new
ByteArrayOutputStream
();
ByteArrayOutputStream
out
=
new
ByteArrayOutputStream
();
while
(
totalSize
-
offset
>
0
)
{
if
(
opMode
==
Cipher
.
DECRYPT_MODE
)
{
int
size
=
Math
.
min
(
1024
/
8
-
11
,
totalSize
-
offset
);
out
.
write
(
cipher
.
doFinal
(
source
));
out
.
write
(
cipher
.
doFinal
(
source
,
offset
,
size
));
}
else
{
offset
+=
size
;
int
offset
=
0
;
int
totalSize
=
source
.
length
;
while
(
totalSize
-
offset
>
0
)
{
int
size
=
Math
.
min
(
cipher
.
getOutputSize
(
0
)
-
11
,
totalSize
-
offset
);
out
.
write
(
cipher
.
doFinal
(
source
,
offset
,
size
));
offset
+=
size
;
}
}
}
out
.
close
();
out
.
close
();
return
out
.
toByteArray
();
return
out
.
toByteArray
();
...
...
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