From 162cff269bb426a417467c3c3ab688c562ee87fc Mon Sep 17 00:00:00 2001 From: Tsln Date: Tue, 2 Mar 2021 19:04:02 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=BC=BA=20RSAUtils=20=E4=BB=A5?= =?UTF-8?q?=E8=A7=A3=E5=86=B3=20RSA=20=E5=8A=A0=E5=AF=86/=E8=A7=A3?= =?UTF-8?q?=E5=AF=86=E9=95=BF=E5=BA=A6=E9=99=90=E5=88=B6=20(#604)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/me/zhengjie/utils/RsaUtils.java | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 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 58089211..7c1f6d15 100644 --- a/eladmin-common/src/main/java/me/zhengjie/utils/RsaUtils.java +++ b/eladmin-common/src/main/java/me/zhengjie/utils/RsaUtils.java @@ -2,6 +2,7 @@ package me.zhengjie.utils; import org.apache.commons.codec.binary.Base64; import javax.crypto.Cipher; +import java.io.ByteArrayOutputStream; import java.security.*; import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; @@ -80,7 +81,7 @@ public class RsaUtils { PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec); Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.DECRYPT_MODE, publicKey); - byte[] result = cipher.doFinal(Base64.decodeBase64(text)); + byte[] result = doLongerCipherFinal(cipher, Base64.decodeBase64(text)); return new String(result); } @@ -98,7 +99,7 @@ public class RsaUtils { PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec); Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.ENCRYPT_MODE, privateKey); - byte[] result = cipher.doFinal(text.getBytes()); + byte[] result = doLongerCipherFinal(cipher, text.getBytes()); return Base64.encodeBase64String(result); } @@ -116,7 +117,7 @@ public class RsaUtils { PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec5); Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.DECRYPT_MODE, privateKey); - byte[] result = cipher.doFinal(Base64.decodeBase64(text)); + byte[] result = doLongerCipherFinal(cipher, Base64.decodeBase64(text)); return new String(result); } @@ -133,10 +134,23 @@ public class RsaUtils { PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec2); Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.ENCRYPT_MODE, publicKey); - byte[] result = cipher.doFinal(text.getBytes()); + byte[] result = doLongerCipherFinal(cipher, text.getBytes()); 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密钥对 *