From 990d9f4851537ab2551ccf13a443ddce9d868951 Mon Sep 17 00:00:00 2001 From: FongMi Date: Sun, 8 Feb 2026 23:47:37 +0800 Subject: [PATCH] Fix bug --- .../java/com/fongmi/quickjs/utils/Crypto.java | 35 +++---------------- 1 file changed, 5 insertions(+), 30 deletions(-) diff --git a/quickjs/src/main/java/com/fongmi/quickjs/utils/Crypto.java b/quickjs/src/main/java/com/fongmi/quickjs/utils/Crypto.java index 809c2d840..8b9c4297d 100644 --- a/quickjs/src/main/java/com/fongmi/quickjs/utils/Crypto.java +++ b/quickjs/src/main/java/com/fongmi/quickjs/utils/Crypto.java @@ -7,9 +7,6 @@ import com.github.catvod.utils.Util; import java.nio.charset.StandardCharsets; import java.security.Key; import java.security.KeyFactory; -import java.security.PublicKey; -import java.security.interfaces.RSAPrivateKey; -import java.security.interfaces.RSAPublicKey; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; import java.util.Arrays; @@ -50,21 +47,13 @@ public class Crypto { public static String rsa(String mode, boolean pub, boolean encrypt, String input, boolean inBase64, String key, boolean outBase64) { try { Key rsaKey = generateKey(pub, key); - int len = getModulusLength(rsaKey); - byte[] outBytes = new byte[0]; byte[] inBytes = inBase64 ? Base64.decode(input.replaceAll("_", "/").replaceAll("-", "+"), Base64.DEFAULT) : input.getBytes(StandardCharsets.UTF_8); - Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); + String tranformation = "RSA/ECB/PKCS1Padding"; + if ("RSA/PKCS1".equals(mode)) tranformation = "RSA/ECB/PKCS1Padding"; + else if ("RSA/None/NoPadding".equals(mode)) tranformation = "RSA/None/NoPadding"; + Cipher cipher = Cipher.getInstance(tranformation); cipher.init(encrypt ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, rsaKey); - int blockLen = encrypt ? len / 8 - 11 : len / 8; - int bufIdx = 0; - while (bufIdx < inBytes.length) { - int bufEndIdx = Math.min(bufIdx + blockLen, inBytes.length); - byte[] tmpInBytes = new byte[bufEndIdx - bufIdx]; - System.arraycopy(inBytes, bufIdx, tmpInBytes, 0, tmpInBytes.length); - byte[] tmpBytes = cipher.doFinal(tmpInBytes); - bufIdx = bufEndIdx; - outBytes = concatArrays(outBytes, tmpBytes); - } + byte[] outBytes = cipher.doFinal(inBytes); return outBase64 ? Base64.encodeToString(outBytes, Base64.NO_WRAP) : new String(outBytes, StandardCharsets.UTF_8); } catch (Exception e) { e.printStackTrace(); @@ -77,18 +66,4 @@ public class Crypto { else key = key.replaceAll(System.lineSeparator(), "").replace("-----BEGIN PRIVATE KEY-----", "").replace("-----END PRIVATE KEY-----", ""); return pub ? KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(Base64.decode(key, Base64.DEFAULT))) : KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(Base64.decode(key, Base64.DEFAULT))); } - - private static int getModulusLength(Key key) { - if (key instanceof PublicKey) return ((RSAPublicKey) key).getModulus().bitLength(); - else return ((RSAPrivateKey) key).getModulus().bitLength(); - } - - private static byte[] concatArrays(byte[] a, byte[] b) { - int aLen = a.length; - int bLen = b.length; - byte[] result = new byte[aLen + bLen]; - System.arraycopy(a, 0, result, 0, aLen); - System.arraycopy(b, 0, result, aLen, bLen); - return result; - } }