From f207d003c2c1ef16f0aa5126c8a1cbb3c4b9456c Mon Sep 17 00:00:00 2001 From: Tsln Date: Tue, 27 Apr 2021 14:29:33 +0800 Subject: [PATCH] bugfix: #627 (#634) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Emmm...解密是没有长度限制的 --- .../main/java/me/zhengjie/utils/RsaUtils.java | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/eladmin-common/src/main/java/me/zhengjie/utils/RsaUtils.java b/eladmin-common/src/main/java/me/zhengjie/utils/RsaUtils.java index 7c1f6d15..8123ff38 100644 --- a/eladmin-common/src/main/java/me/zhengjie/utils/RsaUtils.java +++ b/eladmin-common/src/main/java/me/zhengjie/utils/RsaUtils.java @@ -81,7 +81,7 @@ public class RsaUtils { PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec); Cipher cipher = Cipher.getInstance("RSA"); 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); } @@ -99,7 +99,7 @@ public class RsaUtils { PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec); Cipher cipher = Cipher.getInstance("RSA"); 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); } @@ -117,7 +117,7 @@ public class RsaUtils { PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec5); Cipher cipher = Cipher.getInstance("RSA"); 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); } @@ -134,18 +134,22 @@ public class RsaUtils { PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec2); Cipher cipher = Cipher.getInstance("RSA"); 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); } - private static byte[] doLongerCipherFinal(Cipher cipher, byte[] source) throws Exception { - int offset = 0; - int totalSize = source.length; + private static byte[] doLongerCipherFinal(int opMode,Cipher cipher, byte[] source) throws Exception { 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; + if (opMode == Cipher.DECRYPT_MODE) { + out.write(cipher.doFinal(source)); + } else { + 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(); return out.toByteArray();